What happens when the following program is executed? Provide a rigorous justification for your answer.
// myProg.cpp // compile with // c++ myProg.cpp -o myP -std=c++11 // execute with // ./myP #include<iostream> int f(int x){ if(((x%5==1)&&(x%2==0)) || (x%3==1) ) { return 10; } return 3; } int main(){ int total=0; for(int i=0;i<30;++i){ total+=f(i); } std::cout<<total<<std::endl; return 0; }
What happens when the following program is executed? Provide a rigorous justification for your answer.
// myProg.cpp // compile with // c++ myProg.cpp -o myP -std=c++11 // execute with // ./myP #include<iostream> class Glass{ public: int w; Glass(); Glass(const Glass & ); }; Glass::Glass(){ w=7; } Glass::Glass(const Glass & c){ w=c.w+5; } int f1(Glass & x){ return x.w; } int f2(Glass x){ return x.w; } int main(){ Glass tallGlass; std::cout<<3*f1(tallGlass) + 4*f2(tallGlass)<<"\n"; return 0; }
What happens when the following program is executed? Provide a rigorous justification for your answer.
// myProg.cpp // compile with // c++ myProg.cpp -o myP -std=c++11 // execute with // ./myP #include<iostream> int g(int* s){ int* t; t = s+9; (*t) += 8; return *t; } int main(){ int* s; s=new int[40]; for(int i=0;i<40;++i){ s[i] = 10 * i + 1; } s[0]=g(s); std::cout<<s[0]+s[8]+s[9]<<std::endl; delete[] s; return 0; }
What is a stack? Provide an implementation for a stack of real numbers.
Each object of the class MovieTheater should have two private attributes. The first theaterAddress should be of type std::string. The second private attribute should contain a stack of real numbers.
Your task is to create the appropriate class and the methods that are described below. You must use the keyword const whenever possible in the declaration and implementation of the class.
The user input consists of a sequence of distinct positive real numbers that ends in a negative number. The length of the sequence is not known in advance. Find the two numbers in the sequence that are closest to each other. Print these two numbers and their distance. The complexity of your program should be at most \(O(N\log N)\), where \(N\) is the number of terms in the sequence.
Example:
Input: 9.2 5.1 11.7 5 12 -1 Output: The closest numbers are 5.1 and 5. Their distance is 0.1.