CISC-1600-E01 Remedial Exercises

for loops

  1. What does the following loop output? (We saw it in for2.C.) How many times does it iterate (i.e., repeat itself)? What is the value of the variable i during the first iteration? What is the value of the variable i during the second iteration? What is the value of the variable i during the last iteration?
    	for (int i {0}; i < 10; ++i) {
    		cout << i << "\n";
    	}
    
  2. How is the output different when the loop is changed to
    	for (int i {0}; i < 10; ++i) {
    		cout << i << "\n\n";
    	}
    
  3. How is the output different when the loop is changed to
    	for (int i {0}; i < 10; ++i) {
    		cout << i << " ";
    	}
    
    	cout << "\n";
    
  4. Let’s go back to our original loop
    	for (int i {0}; i < 10; ++i) {
    		cout << i << "\n";
    	}
    
    Make it start at 1 instead of at 0.
    Then make it start at 5 instead of at 1.
  5. What does the following loop output?
    Why does it now count up to 10 instead of stopping at 9?
    	for (int i {0}; i <= 10; ++i) {
    		cout << i << "\n";
    	}
    
  6. Make a loop that outputs the integers from 10 to 20 inclusive, in increasing order, one after another on the same line, separated by spaces. See exercise 3 above.
  7. What does the following loop output?
    What does the += do?
    	for (int i {10}; i <= 100; i += 10) {   //i += 10 means i = i + 10
    		cout << i << "\n";
    	}
    
  8. Write a loop that outputs
    2
    4
    6
    8
    
  9. What does the following loop output? Why does it count down? Why does it stop at 0?
    	for (int i {10}; i >= 0; --i) {
    		cout << i << "\n";
    	}
    
  10. What does the following loop output? Why does it count down by tens?
    	for (int i {100}; i >= 0; i -= 10) {   //i -= 10 means i = i - 10
    		cout << i << "\n";
    	}
    
  11. What does the following loop output? (The "\t" is the tab character.) Why is each number in the second column of output 1 greater than the corresponding number in the first column of output?
    	for (int i {0}; i < 100; i += 10) {   //i += 10 means i = i + 10
    		cout << i << "\t" << i+1 << "\n";
    	}
    
  12. Write a loop whose output is the following. Each number in the second column of output must be double the corresponding number in the first column of output.
    0	0
    10	20
    20	40
    30	60
    40	80
    50	100
    60	120
    70	140
    80	160
    90	180
    
    Extra credit: use setw to write a loop whose output is the following, becuase human being like to see their columns of numbers right justified. For setw, remember to #include <iomanip>.
     0	  0
    10	 20
    20	 40
    30	 60
    40	 80
    50	100
    60	120
    70	140
    80	160
    90	180
    

if statements

  1. Write a loop whose output is the following. Note that if you divide an even number by 2, the remainder is 0. If you get any other remainder, it’s an odd number.
    0 is even.
    1 is odd.
    2 is even.
    3 is odd.
    4 is even.
    5 is odd.
    6 is even.
    7 is odd.
    8 is even.
    9 is odd.
    
  2. Write a loop whose output is the following. Note that if you divide a presidential election year by 4, the remainder is 0. And if you divide a midterm election year by 4, the remainder is 2. If you get any other remainder, it’s a local election year.
    2000 is a presidential election year.
    2001 is a local election year.
    2002 is a midterm election year.
    2003 is a local election year.
    2004 is a presidential election year.
    2005 is a local election year.
    2006 is a midterm election year.
    2007 is a local election year.
    2008 is a presidential election year.
    2009 is a local election year.
    2010 is a midterm election year.
    2011 is a local election year.
    2012 is a presidential election year.
    2013 is a local election year.
    2014 is a midterm election year.
    2015 is a local election year.
    2016 is a presidential election year.
    2017 is a local election year.
    2018 is a midterm election year.
    2019 is a local election year.
    2020 is a presidential election year.
    2021 is a local election year.
    2022 is a midterm election year.
    2023 is a local election year.
    2024 is a presidential election year.
    2025 is a local election year.
    2026 is a midterm election year.
    2027 is a local election year.
    2028 is a presidential election year.
    
  3. Combine these three separate if statements into one three-way if/else if/else. Assume you already have an int variable named year.
    	time_t t {time(nullptr)};            //remember to #include <ctime>
            tm *p {localtime(&t)};
            int currentYear {p->tm_year + 1900}; //the current year
    
    	if (year < currentYear) {
    		cout << year " was a good year.\n";
    	}
    
    	if (year == currentYear) {
    		cout << year " is a good year.\n";
    	}
    
    	if (year > currentYear) {
    		cout << year " will be a good year.\n";
    	}
    
  4. When executed simultaneously, will these two pieces of code always produce output that is identical to each other? If not, at what hour of the day would they produce different output? Note that the first piece of code will perform 4 comparisons, while the second piece of code will perform only 1 or 2 comparisons.
    	time_t t {time(nullptr)};   //remember to #include <ctime>
            tm *p {localtime(&t)};
            int hour {p->tm_hour};      //current hour of the day (0 to 23 inclusive)
    
    	if (hour < 12) {
    		cout << "Good morning.\n";
    	}
    
    	if (hour >= 12 && hour < 12+6) {
    		cout << "Good afternoon.\n";
    	}
    
    	if (hour >= 12+6) {
    		cout << "Good evening.\n";
    	}
    
    	time_t t {time(nullptr)};   //remember to #include <ctime>
            tm *p {localtime(&t)};
            int hour {p->tm_hour};      //current hour of the day (0 to 23 inclusive)
    
    	if (hour < 12) {
    		cout << "Good morning.\n";
    	} else if (hour < 12+6) {
    		cout << "Good afternoon.\n";
    	} else {
    		cout << "Good evening.\n";
    	}