Quiz 2 Solutions Suppose the computer is tasked with calculating the following expressions, what would the result be? 1. Write the value of: 5 * 5 % 10 = 5 5 * 5 % 10 //start here 25 % 10 // multiplication gets done first, as multiplication and // mod have the same precedence, so we go from left to // right 5 // then do the mod to get the answer 2. Write the value of: 5 + 5 / 10 = 5 5 + 5 / 10 //start here 5 + 0 // division has higher precedence than addition, so we do // that first. Since neither 5 nor 10 have a decimal point // we treat both as ints, so we divide and drop everything // after the decimal to get 0 5 // then add the numbers to get the answer 3. Write the value of: 5 / 5 * 10 = 10 5 / 5 * 10 // start here 1 * 10 // in C, multiplication and division have equal precedence, // so we evaulate from left to right, so the division will be // done first 10 // then multiply the values to get the final result 4. Write the value of: 5 / 10 = 0 5 / 10 //start here 0 // as neither of these numbers have a decimal point, we treat // both of these numbers as ints, a data type that must be a // whole number. since both of these numbers are ints, the // answer will also be an int, which means it cannot be a // decimal. so when we divide these two numbers, we get 0.5, // but we drop everything after the decimal point, so it just // becomes 0 5. Write the value of: 5 / 10.0 = 0.5 5 / 10.0 // start here 0.5 // unlike the above problem, one of these numbers has a // decimal point, the 10.0. We treat numbers with decimals as // floats, a data type that can handle decimals. If you have a // float anywhere in your expression, the answer will be a // float, so like above, we divide the numbers, but we keep // the decimal point to get the final result. 6. Given the two lines of code below, write the next line of code that gets the user's age and stores it in the variable age' int age; printf("Please enter your age "); Answer: scanf("%d", &age); // You use scanf to get input from the user // the "%d" indicates that we are expecting an integer // the &age indicates that we are storing this in the variable "age" // as "age" is an integer, we need the ampersand in front of the // variable // also, don't forget that the semicolon must go on the end as well, // as well as the comma between %d and &age 7. Write one line of code that sets up "weight" as a floating point variable with the initial value of 163.2 Answer: float weight = 163.2; // float is (one of) the data types you'd use to set up a // floating-point variable, a datatype that can handle decimals // then the variable name is weight, so that goes after the datatype // then we assign the numerical value simply by setting it equal to // the specified value // as always, the semicolon at the end is required 8. The code below keeps track of the number of yawns, Write one more line of code that prints out the number of yawns int YawnCount = 0; //code that counts yawns Answer: printf("%d", YawnCount); //or something similar // The code that goes between the two statements doesn't really // matter, all we care about here is how to print the value. // we use printf to print values. // "%d" indicates that we ae printing an integer // then YawnCount is the variable we're printing // don't forget the semi-colon or the comma between %d and YawnCount 9. The code below starts the conversion of a temperature from Fahrenheit to Celsius. You need to fill in the right hand side of thelast line by multiplying "above32" by 5 then dividing by 9. //conversion to Celsius printf("Please enter the temperature in Fahrenheit "); float farh; scanf("%f", &farh); float above32 = farh-32; float celsius = //What goes here? Answer: above32 * 5 / 9; // We simply set up the expression as defined above // take above32 multiply by 5 and divide by 9, just write this // from left to right. 10. What does the code below print? int x = 11; printf("%d\n", 7 * (x/7) + x % 7); Answer : 11 // Here, we're simply evaulating an expression: 7 * (x / 7) + x % 7 //start here // we first substitute 11 for x to get the following 7 * (11 / 7) + 11 % 7 // Values inside Parentheses have the highest precedence, so // evaluate that first, Note that neither value inside the // parentheses has a decimal, so we treat them as ints, so the // answer will be a whole number 7 * 1 + 11 % 7 // Then going from left to right, we multiply first 7 + 11 % 7 // Since mods have higher precedence over addition, we do that next 7 + 4 // and now we add to get the final answer 11