---------------------------------------- ** On paper portion. Points: 28 points total, 6 points #1, 8 points #2, 6 points #3, 8 points #4 ** Change: #4 worth 8 points, 28 points total. It had said #4 worth 6 points, and 26 points total. ** Grading... For most problems / is -1, and X -2. Only / or X through the problem number are points taken off. I crossed off some things you wrote down that were wrong, but only took off points if the / or X is through the problem number. The total number of points missed is written on the back page. 1) Do the algorithm. Show your work. No credit if you don't show some work. 1a) Convert 61 decimal to binary. Note: * bits are written in the same order as decimal numbers - the ones bit is on the right, not on the left * the right-most bit is the ones bit, not the twos bit. 61 = 32 + 16 + 8 + 4 + 1 = 111101b 1b) Convert 111000 binary to decimal. 111000b = 1*32 + 1*16 + 1*8 + 0*4 + 0*2 + 0*1 = 56 1c) Use the Caesar cipher to encrypt the message "blob". That is, shift each letter by 3. Note: * People who didn't show any work only got half credit. Correct answer: eore 2) Answer the questions about C/C++ syntax and data types. For each write EXACTLY what you would type in your program (including correct spelling and capitalization). 2a) List 4 different data types that can be used for numbers, and say what is different about each. Note: * strings and bools are NOT number types * int is 32 bits, not 32 bytes * int is a signed type (can be + or -) * integer types are: char (8 bits), short (16 bits), int (32 bits), long int (64 bits) * for each of those, if you put unsigned before it, then it becomes an unsigned type, with only non-negative values allowed * floating point types are: float (32 bits), double (64 bits) * 8 bits = 1 byte 2b) Given the following function definition double average(double x, double y) { double z = (x+y)/2.0; return z; } What is the return type? Answer: double Not correct: double answer - that includes the name as well Not correct: z - that is what is returned, not the type What are the parameters? Answer: x, y Also accepted: double x, double y What is the name of the function? Answer: average Not correct: double average - that includes the return type also 2c) Given the following for loop int i; for(i=-10; i < 11; i++) cout << i << endl; Note: full credit was given if you got at least 3 out of the 4 parts right. half credit was given if you got 2 out of the 4 parts right. What is the initialization? Answer: i=-10 Not correct: int i - the intialization is the name for the i=-10 part of the for loop Not correct: i What is the loop condition? Answer: i < 11 Not correct: (i=-10; i < 11; i++) - that is more than just the loop condition What is the loop increment? Answer: i++ What is the body of the for loop? Answer: cout << i << endl; 2d) List the 5 arithmetic operators in C. Answer: + - / * % Also accepted as one of the 5: ++ -- Not correct: < > <= >= != == - those are the relational operators Not correct: && || ! - those are the Boolean operators 3) Memorize program. Write down the code for the iterateWhile.cpp program. It should ask the user for an integer, save the value into a variable called num and then print "Hello!!!" num many times. If they type 10, it would print "Hello!!!" 10 times. Write the COMPLETE program, including #include and everything. Answer: look it up in the memorize directory Note: * Worth 6 points total. * If you did not have a loop the most you could get is half credit * 2/6 was given if you had something, but was nowhere near correct code, and probably had some things that made no sense. * If there was a minor but critical mistake, that was -1. 4) Be the computer. For each, keep track of the values of all the variables as the code runs - as we did in class. 4a) basic arithmetic, functions. // code double avg(double x, double y) { return (x+y)/2.0; } double min(double xx, double yy) { if (xx <= avg(xx,yy)) return xx; else return yy; } int main() { double a = 3.0, b = 5.0, c; c = min(a,b); cout << c << endl; return 0; } // variable values. // // a: 3.0 // // b: 5.0 // // c: 3.0 // // xx: 3.0 // // yy: 5.0 // // x: 3.0 // // y: 5.0 // printed on screen: 3.0 Note: * worth 3 points. * -2 if got a and b but nothing else correct (so not showing that you know what is happening with functions) * -1 if got the parameters of the functions correct but some other mistake. 4b) Boolean logic, conditionals. // code int x = 10, f = 9; if (x > f && f < 10) { if (f <= 3.14 ) x = x / 2; else x = x * 2; f = f + 1; } else { if (f > 3.0) x = x / 10; else x = x * 10; f = f + 10; } x = (x <= f * 2 && f <= 9) || (f <= 100 && x >= 0); cout << x << endl << f << endl; // variable values. // // x: 10, 20, 1 // // f: 9, 10 // // printed on screen: 1 10 Note: * Worth 2 points * Did not take off if didn't get final value of x=1 correct. * -1 if only had final values of variables and didn't show any work. * +1 if everything completely correct (including x=1 at the end). 4c) Loops/arrays. // code char msg[5] = "blob"; int i = 0; while (i < 4) { msg[i] = 'a' + i; cout << msg << endl; i = i + 1; } cout << i << endl << msg << endl; // variable values. // // i: 0, 1, 2, 3, 4 // // msg: blob // alob // abob // abcb // abcd // // printed on screen: alob abob abcb abcd 4 abcd Note: * Worth 3 points * -2 if i was right but msg was not right at all * -1 if i was right and what you had for msg was not right, but on the right track