C++ Operators
Functions
Input/Output
Loops/Selection
Full Programs
100
What does this expression evaluate to? 3 % 3 * 2 + 32.5 / 4 - 3
5.12
100
You have been put in charge of implementing an earthquake warning system for your company. Assume that quakeMeasurement is a variable of type "double" containing an earthquake measurement on the Richter Scale. Based on this measurement, announce "NO ALARM" for earthquakes lower than 2.0, "BRACE YOURSELVES" for earthquakes between 2.0 and 6.0, and "TAKE COVER" for anything above 6.0. Use as few conditionals as possible.
if (quakeMeasurement < 2.0) cout << "NO ALARM"; else if (quakeMeasurement <= 6.0) cout << "BRACE YOURSELVES"; else cout << "TAKE COVER";
100
You have been put in charge of implementing an earthquake warning system for your company. Assume that quakeMeasurement is a variable of type "double" containing an earthquake measurement on the Richter Scale. Based on this measurement, announce "NO ALARM" for earthquakes lower than 2.0, "BRACE YOURSELVES" for earthquakes between 2.0 and 6.0, and "TAKE COVER" for anything above 6.0. Use as few conditionals as possible.
if (quakeMeasurement < 2.0) cout << "NO ALARM"; else if (quakeMeasurement <= 6.0) cout << "BRACE YOURSELVES"; else cout << "TAKE COVER";
100
Write the program Hello World.
wow c++
200
int y = ? cout << y%6 << endl; For what value of 'y' does this code print '6'?
It doesn't.
200
Write a function called "isOdd" which takes one integer as a parameter, returns a boolean, and returns true if and only if the integer given is odd. Your function may contain no more than 2 lines (statements) of code (not counting the header).
bool isOdd(int x) { if (x % 2) return true; else return false; }
200
char a = ‘A’; int x = 1; cin >> x >> x >> a; cout << a << “ “ << x << endl; If the user inputs 3bc, what prints?
A 3
200
Use a loop to print the following output: ***** ***** ***** *****
int x = 0; while (x < 4) { cout << "*****" << endl;; x = x + 1; }
200
Write a program that continues to asks the user to enter any number other than 5 until the user enters the number 5. Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.
#include int main () { int num = 0; while (num != 5) { std::cout << "Enter any number but 5" << endl; cin >> num; } cout << "Hey! You weren't supposed to enter 5!" << endl; }
300
int x = 10; cout << 2 + x / 3 % 2 - 1 << endl; What prints?
2
300
Write the isOdd() function in a single line of code (not counting the header). (No ternary operators, please)
bool isOdd(int x) { return x % 2; }
300
int input; cin >> input; cout << input; If 625.51 is typed from standard input, cout << input; prints what to standard output?
625
300
Use a loop to print the following: * * * * * * * * * * * * * * * * * * * * * * *
int x = 0; while (x < 5) { if (x % 2) cout << " * * * *" << endl; else cout << "* * * * *" << endl; }
300
Write a program that, using a loop, continually asks the user to "Press any key (followed by enter)" and then ignores it. After 10 iterations tell the user "Wow, you're more patient then I am, you win." and exit. Bonus points: abstract it to a function called "Windows_Vista"
blah code int main () { int count = 0; while (count++ < 10) cout << "Press any key (and enter)" << endl; cout << "Wow, you're more patient than me, you win"; }
400
bool bar = false; if (bar = true) cout << "Am I Here?" << endl; else cout << "Or Here?" << endl; What does this code print?
Am I here?
400
Write a function called "isOdd" which takes one integer as a parameter, returns a boolean, and returns true if and only if the integer given is odd. Your function may contain no more than 2 lines (statements) of code (not counting the header).
bool isOdd(int x) { if (x % 2) return true; else return false; }
400
int x, y, ijunk; char z, cjunk; If the user types in '810a8z10q' and presses return, which of the following code fragments will result in x, y, and z, having the values of 10, 8, and 'z', respectively? A) cin >> ijunk >> cjunk >> y >> z >> x >> cjunk; B) cin >> "810a" >> y >> z >> x >> 'q'; C) cin >> ijunk >> ijunk >> ijunk >> cjunk >> y >> z >> x >> cjunk; D) cin >> y >> x >> cjunk >> ijunk >> z >> ijunk >> cjunk;
A
400
Print a triangle of size 5: * ** *** **** *****
int x = 1; \n while (x <= 5) { int y = 0; while (y < x) { cout << "*" } cout << endl; }
400
Write a program that asks the user for a number "n", and then prints the first "n" odd numbers.
//input int x = 1; while (n) { cout << x << ' '; x += 2; n--; }
500
void foo( int x ) { if ( x = 0 ) cout << “x is less than 2” << endl; else if ( x = 1 ) cout << “x is less than 2” << endl; else cout << “x is 2 or greater” << endl; } What value of "x" exposes the bug in this code?
x = 2 or greater
500
Write the following function: bool isPrime(int num); // Returns true if and only if 'num' is a prime number
bool isPrime(int num) { int divisor = 2; while (divisor < num) { if (num % divisor == 0) return false; divisor++; } return true; }
500
Write a program that, given some input, will print out all of the input EXCEPT any instance of the number '3'. For example: Input: fj390ajij3jkj23 Output: fj90ajijjkj2
blah code while (cin >> c) { if (c != '3') cout << c; }
500
Write a program that asks the user to type 3 integers and prints the smallest value. Example - if the user enters: 3 7 5 Output: 3
blah min function blah Answer is on codelab/internet
500
#include using namespace std; int f( int x ); int main(void) { int x = 5; int y = f(x--); cout << y--; return 0; } int f(int x) { return (x--)-1; }
4
M
e
n
u