Comp 1671 Homework 5 Mon, Oct 14 (but you don't have to turn it in) Chapter 5: 10, 11, 13, 17, 18, 22, 30 (ignore the setw outputs) Chapter 6: 1, 5 Review problems to use as a starting point for studying for the midterm. Don't forget to also review all of your old homework. 1. Count from 1101101 to 1110111 in binary 2. Practice converting between binary and decimal. Choose a decimal number and convert it to binary. Give the result to your friend, who will convert back to decimal. Check that their result matches your original number. Now reverse roles. 3. Make a list of legal identifiers, and a list of illegal identifiers 4. Write c++ code to do each of the following: a. Read an integer from the keyboard and store it in the variable num_reps (assume variable is already defined) b. Double the value stored the variable total_wealth. Think of as many ways to do this as you can. d. If x is a float variable whose value is already set, evaluate the following piece-wise defined function and store it in y: x^2 + 4, if x < 0 y = 5 , if 0 <= x <= 2 1/(x+3), if x > 2 e. Calculate the sum of 10 numbers that the user enters f. Input numbers from the user until a negative number is reached, then prints the sum of the evens and odds separately. 5. Write a flow-chart for the following, and describe what the code does float sum(0); float num; do { cout >> "Type a number"; cin >> num; if (num >= 0) sum += num; } while (num != -1); 6. Practice with flow charts as follows: Write a few lines of code with nested control structures. Take examples from lab or from the text if you like (a while loop inside an if statement, or an if/else inside a for loop, etc.), and draw it as a flow chart. Give the flow chart to your friend, and have your friend turn it into c++ code. Now reverse roles. 7. Rewrite the following using a "for" statement. Describe what the code does. counter = 0; while (counter < 100) { cout << counter; counter += 2; } 8. What is the value of c at the end of each of the following? (Treat each as a separate question) a. c = 0; for (i=1; i<5; i++) c++; b. c=0; for (i=10; i>0; i-=2) c++; d. c=0; for (k=0; k <= 100; k++) c; c. c=0 for (i=0; i<10; i++) for (j=0; j<10; j++) c++; 9. Consider the following c++ code: j= -1; if ( (j>0) && (1/(j+1) > -1) ) cout << j*j+1 << endl; Does this code produce a division by zero error? Explain. 10. What is the output of the following c++ code? Explain. x = 3; if (x=2) cout << 2*x; else cout << 3*x; 11. What is the output of the following c++ code? x=3; y=4; if ( (x>3) || (x*y > 10) ) { if ( (!(x-y < 0)) && ( y < 5) ) cout << ++x + y; else cout << --x-y; } else cout << x*x+y*y; 12. Consider the following c+ code: switch (Value) { case 1: case 2: cout << 2*Value; break; case 3: case 4: cout << 3*Value; break; default: cout << 4*Value; break; } Give the equivalent c++ code for the above that uses if/else statements 13. Convert the following to a simple statements (get rid of the compound assignment) a *= 2 * b + c; 14. Convert the following to a compound assignment statement salary = salary*1.02 + salary*bonus; 15. What is the output of the following code? int i(3), j(5); float num1(2.2); float num2(1.0); num2 *= 2*i + num1; // Be careful here that you understand cout << num2; // when and how implicit casts are done cout << i/j + num1; i = num1*j; cout << i;