Quiz 3 Solutions

You have up to 20 minutes. Please write your answers on this page, and your name at the top. You may not use the computer, notes, or books.

The program on this page uses nested conditional statements. Given the values below as inputs, what does the program output?

  • fred = 6; wilma = 3; Output: EF
  • fred = 7; wilma = 7; Output: D
  • fred = 3; wilma = 6; Output: ACF
  • fred = 6; wilma = 9; Output: ACEF
  • fred = 5; wilma = 1; Output: F
#include <iostream>
using namespace std;
int main()
{
    int fred, wilma;
    cin >> fred >> wilma; // inputs given above
    if(fred < wilma)
    {
        if(wilma > 5) cout << "A";
        else cout << "B";
        cout << "C";
    }
    if(fred == wilma)
    {
        cout << "D";
    }
    else
    {
        if(fred > 5) cout << "E";
        cout << "F";
    }
}

This is a similar problem, but the program on this page uses a while loop. What does the program output?

#include <iostream>
using namespace std;
int main()
{
    int carol = 3;
    while(carol > 1)
    {
        cout << carol << "\n";
        if(carol % 2 == 0) {
            carol /= 2;
        }
        else {
            carol = carol * 3 + 1;
        }
    }
    return 0;
}

3
10
5
16
8
4
2