CISC-1600-E01 Second Midterm Postmortem, October 24, 2024

  1. What is the value of each of the following C++ expressions?
    Answer to the corresponding question in the first midterm.
    1. 	10 - 20 - 30
      
      The value is −40, because subtraction has left-to-right associativity.
    2. 	(10 - 20) - 30
      
      The value is −40, because the operation in the parentheses is executed first.
    3. 	10 - (20 - 30)
      
      The value is 20, because the operation in the parentheses is executed first.

  2. Assume that you already have the following four C++ variables.
    	int i {10};
    	int j {20};
    
    	double d {10.0};
    	double e {20.0};
    
    What would be the value of each of the following C++ expressions?
    1. 	i/j
      
      The value is 0, because the value is of data type int and an int cannot hold a fraction.
    2. 	d/e
      
      The value is 0.5, because the value is of data type double and a double can hold a fraction.

  3. Will the following two pieces of C++ code always produce the same output as each other, no matter what values are in the variables harris and trump?
    1. 	if (harris <= trump) {
      		cout << "Harris did not win.\n";
      	}
      
      	if (harris >= trump) {
      		cout << "Trump did not win.\n";
      	}
      
    2. 	if (harris <= trump) {
      		cout << "Harris did not win.\n";
      	} else {
      		cout << "Trump did not win.\n";
      	}
      
      No. If the two variables hold the same value, then the two pieces of code produce different outputs.
      Answer to the corresponding question in the first midterm.

  4. Assume you already have a variable named dollars, of data type int, containing the number of dollars you have. (You must not create this variable: it already exists.) Write C++ code that will output an English sentence such as the following, to show the value of the variable. The English sentence should end with a period. Please output one newline character after the sentence. Output the newline without using endl.
    You have 5 dollars.
    
    The code should show the value of the variable no matter what that value is. In particular, the code should show the value of the variable even if that value is not 5. In the sentence that you output, the last word must be singular (“dollar”) if the value of the variable dollars is 1, plural (“dollars”) otherwise.

    Be sure to output a space on both sides of the number: you don’t want to output something like

    You have 5dollars.
    

    	if (dollars == 1) {
    		cout << "You have " << dollars << " dollar.\n";   //singular
    	} else {
    		cout << "You have " << dollars << " dollars.\n";  //plural
         	}
    
    Answer to the corresponding question in the first midterm.
    The following students got this question right: ac239, jcrews1, lam2, kheslin2, jrn4.
    Why are the following answers wrong?
    cout << dolars;
    	if (dollars == 5) {
    		cout << " dollars.\n";
    	} else {
    		cout << " dollar.\n";
    	}
    
    	If (dollars > 1) {
    		cout << "You have ___ " << dollars << "___ dollars." << "\n";
    	} else if (dollars == 1) {
    		cout << "You have ___ " << dollars << "___ dollar." << "\n";
    	} else {
    		cout << "You have no money." << "\n";
    	}
    
    	cout << "You have " << dollars << " dollars.\n";
    
    	int dollars
    	cout << "You have 5 dollars." >>
    
    	if (dollars > 1) {
    		cout << "You have " << dollars << " dollars.\n";}
    	if (dollars == 1) {
    		cout << "You have 1 dollar.\n";
    
    	if (dollar == 5)
    	cout << "You have " << 5 dollars << " dollars.\n";
    	} else {
    	 cout << "You have " << 5 dollars << " dollars.\n";
    
    	if dollars (== 1) {
    		cout << dollar.
    	else
    		cout << dollars.
    

  5. What (if anything) goes wrong in the following C++ code? Assume you already have four variables of type double, named a, b, c, d. (You do not need to create these variables: they already exist.)
    	if (b != 0.0 || d != 0.0) {
    		cout << "It's okay to go ahead and perform both divisions.\n";
    		cout << "The quotients are " << a/b << " and " << c/d << ".\n";
    	} else {
    		cerr << "Sorry, you can't divide by 0.\n";
    		return EXIT_FAILURE;
    	}
    
    The || (“or”) should have been && (“and”).
    Answer to the corresponding question in the first midterm.

  6. What is a computer?
    A computer is a machine that follows instructions.
    (The technical name for this is a Von Neuman machine.)
    The syllabus and in-class examples for the course. Answer to the corresponding question in the first midterm.

  7. What is a program?
    The list of instructions you put into the computer is called a program.
    Answer to the corresponding question in the first midterm.

  8. Will the following two pieces of C++ code always produce the same output as each other, no matter what value is in the variable hour?
    1. 	cout << "Good ";
      
      	if (hour >= 12) {
      		if (hour >= 12 + 5) {
      			cout << "evening";
      		} else {
      			cout << "afternoon";
      		}
      	} else {
      		cout << "morning";
      	}
      
      	cout << ".\n";
      
    2. 	cout << "Good ";
      
      	if (hour < 12) {
      		cout << "morning";
      	} else if (hour < 12 + 5) {
      		cout << "afternoon";
      	} else {
      		cout << "evening";
      	}
      
      	cout << ".\n";
      
    Yes.
    Answer to the corresponding question in the first midterm.

  9. How many operators are there in each of the following C++ expressions? Number the operators in the order in which they are executed. For example, in the first expression, the * is number 1, and the + is number 2.
    Answer to the corresponding question in the first midterm.
    1. 	a + 10 * b
      	  2    1
      
    2. 	-a * 10 - b
              1  2    3                 (Negation has higher precedence than multiplication.)
      
    3. 	cout << a + 10 * b << "\n"
      	     3    2    1   4
      
    4. 	cout << 1 - 2 - 3 << "\n"
      	     3    1   2   4
      
    5. 	cout << a << b << c << "\n"
                   1    2    3    4     (Output has left-to-right associativity.)
      
    6. 	z = y = x = 10
                3   2   1               (Assignment has right-to-left associativity.)   
      

  10. How many characters does this C++ statement output?
    	cout << "\tX\n";
    
    3 characters: tab, uppercase X, newline.
    Answer to the corresponding question in the first midterm.

  11. What does each of the following C++ examples output?
    Answer to the corresponding question in the first midterm.
    1. 	int i {16};
      	i += 2;
      	cout << i << "\n";
      
      18
      
    2. 	int i {16};
      	i *= 2;
      	cout << i << "\n";
      
      32
      
    3. 	int i {16};
      	i /= 2;
      	cout << i << "\n";
      
      8
      
    4. 	int i {16};
      	++i;
      	cout << i << "\n";
      
      17
      

  12. Assume you already have a variable named i, of data type int, holding a positive number. (You must not create this variable: it already exists.) In the following parentheses, write a C++ expression whose value will be true if i is a number that ends with a 0 (such as 10, 20, 120, etc.), false if i is a number that does not end with a 0 (such as 11, 12, 21, 22, etc).
    See ordinal.C for i % 10.
    Answer to the corresponding question in the first midterm.
    	if (i % 10 == 0) {
    		cout << i << " ends with a 0.\n";
    	} else {
    		cout << i << " does not end with a 0.\n";
    	}
    
    The following students got this question right: did4, dg51, lam2, jsr1.
    Why are the following answers wrong?
    	if (int i {0}; i % == 0) {
    		cout << i << ends with a 0.\n";
    	} else {
    		cout << i << ends with a 0.\n"
    
    	if (i % 2 == 0)
    
    	if (i % 10)
    
    	If (i % == 0)
    
    	if (i >= (
    

  13. Assume you already have a variable named m, of data type int, containing the number of minutes that a job took. (You must not create this variable: it already exists.) Define two more variables, named hours and minutes, of type int. Initialize hours to the number of hours that this job took. Initialize minutes to the number of remaining minutes that this job took. (Hint: the initial value of minutes must be a number in the range 0 to 59 inclusive.)
    	int hours   {m / 60};
    	int minutes {m % 60};
    
    Answer to the corresponding question in the first midterm.
    The following students got this question right: ac239, dg51, lam2, sm56.
    Why are the following answers wrong?
    	Hours   (m / 24)
    	minutes (m / 60)
    
    	int hours   {minutes / 60};
    	int minutes {minutes % 60};
    
    	int m {130};
    	int Hour {60}
    	int min {30}
    
    	int hours   = m / 60;     //This is how they wrote C++ many years ago.
    	int minutes = m % 60;
    
    	int hours   {minutes / 60};
    	int minutes {minutes / 59};
    
    	int hours   {m / 60}
    	int minutes {m % 60}
    
    	int min
    	hours {24}
    	minutes {59}
    
    	int hours {minutes / 60};
    	int minutes {hours % 60};
    

  14. Draw a picture of the output produced by each of the following C++ for loops.
    Answer to the corresponding question in the first midterm.
    1. 	int n {4};
      
      	for (int i {0}; i < n; ++i) {
      		cout << i << "\n";
      	}
      
      0
      1
      2
      3
      
    2. 	int n {4};
      
      	for (int i {1}; i < n; ++i) {
      		cout << i << "\n";
      	}
      
      1
      2
      3
      
    3. 	int n {4};
      
      	for (int i {0}; i <= n; ++i) {
      		cout << i << "\n";
      	}
      
      0
      1
      2
      3
      4
      
    4. 	int n {4};
      
      	for (int i {1}; i <= n; ++i) {
      		cout << i << "\n";
      	}
      
      1
      2
      3
      4
      
    5. 	int n {4};
      
      	for (int i {1}; i <= n; ++i) {
      		cout << i;
      	}
      
      	cout << "\n";
      
      1234
      

  15. Draw a picture of the output producted by the following C++ code.
    	for (int i {0}; i < 3; ++i) {
    		for (int j {0}; j < 2; ++j) {
    			cout << "X";
    		}
    		cout << "\n";
    	}
    
    XX
    XX
    XX
    
    Answer to the corresponding question in the first midterm.

  16. What (if anything) goes wrong in the following C++ code?
    	for (int i {0}; i < 10; ++i) {
    		cout << i << "\n";
    	}
    
    	cout << "After the loop is over, the value of i is " << i << "\n";
    
    The variable i no longer exists (and therefore certainly no longer holds any value) after the loop is over.
    Answer to the corresponding question in the first midterm.

  17. How many multiplications does the following C++ code execute?
    	double money {100.00};
    
    	//Increase the money by 6 percent each year.
    
    	for (int year {0}; year < 10; ++year) {
    		money *= 1.06;
    	}
    
    	cout << "The money has grown to " << money << "\n";
    
    10 multiplications, because the for loop iterates 10 times.
    Answer to the corresponding question in the first midterm.

Table of precedence and associativity
for the C++ operators we have covered so far

Precedence Operator Description Associativity
3   -a  
  ++a  
negation (as in “negative 5”)
increment (i.e., “add 1”)
right-to-left
5   a*b     a/b     a%b   multiplication, division, and remainder left-to-right
6   a+b     a-b   addition, subtraction left-to-right
7   cout<<x     cin>>x   output, input left-to-right
9   a<b     a<=b  
  a>b     a>=b  
is less than, is less than or equal to,
is greater than, is greater than or equal to
left-to-right
10   a==b     a!=b   is equal to, is not equal to left-to-right
14   a&&b   and left-to-right
15   a||b   or left-to-right
16   a=b  
  a+=b     a-=b     a*=b     a/=b     a%=b  
assignment
(change the value of a variable)
right-to-left