CMSC 160
Fall 2018
09/06/2018
Exploring Loops
Due:  end of lab

Handin

You may work in groups of up to 3.  If so, hand in only ONE submission sheet.    It will be assumed that all members of the group participated.

Assignment:    (3 points)

For each of the following snippets:
  1. Try to predict what it does
  2. Type the code in to a program, run it and see if you were right.
  3. If you were wrong, understand what happened.  Ask questions as necessary.

1 int x = 0;
while (x < 5)
{
    cout << "
Thursday" << endl;
    ++x ;
}
int x = 0;
while (x < 5)
{
    ++x;
    cout << "
Thursday" << endl;
}
int x = 0;
while (x < 5)
{
    cout << "Thursday" << endl;
}
2 int x = 0;
while (x < 5)
{
    
x = x + 1;
    cout << "Great Lab" << endl;
}
int x = 0;
while (x < 5)
    cout << "Great Lab" << endl;
    
x = x + 1;
int x = 0;
while (x < 5)
    ++x;
    cout << "Great Lab" << endl;
3 For each of the snippets in rows 1 and 2, change the 5 to be 10.
What will happen differently?
4 int y = 0;
while (y < 5)
{
    cout << y << endl;
    ++y;
}
int y = 0;
while (y < 5)
{
    ++y;
    cout << y << endl;
}
int y = 0;
while (y < 5)
{
    y = y + 1;
    cout << y << endl;
}
5 int x = 6;
while (x < 19)
{
    cout << "Great Lab" << endl;
    ++x;
}
int x = 153;
while (x < 162)
{
    cout << "Great Lab" << endl;
    ++x;
}
int x = 13;
while (x < 21)
{
    cout << "Great Lab" << endl;
    cout << x << endl;
    x = x + 1;
}
cout << x << endl;
6 int x = 0;
while (x < 5)
{
    cout << x << endl;
    --x;
}
int x = 5;
while (x >0)
{
    cout << "Great Lab";
    cout << x << endl;
    --x;
}
cout << x << endl;
int x = 5;
while (x >=0)
{
    cout << "Great Lab" << endl;
    --x;
}
cout << x << endl;
7 int a = 10;
int b = 20;
while (a < b)
{
    a = a + 1;
    cout << "Great Lab" << endl;
    b = b - 1;
    cout << a << "  " << b << endl;
}
cout << a << "  " << b << endl;
int a = 11;
int b = 20;
while (a < b)
{
    a = a + 1;
    cout << "Great Lab" << endl;
    b = b - 1;
    cout << a << "  " << b << endl;
}
cout << a << "  " << b << endl;
int a = 11;
int b = 20;
while (a < b)
{
    a = a + 1;
    cout << "Great Lab" << endl;
    if (b > 18)
        b = b - 1;
    cout << a << "  " << b << endl;
}
cout << a << "  " << b << endl;
8 int x = 10;
int y = 50;
while ((x < 15) and (y <58 ))
{
    x = x + 1;
    y = y + 1;
    cout << "Weekend coming" << endl;
    cout << x << "  " << y << endl;
}
cout << x << "  " << y << endl;
int x = 10;
int y = 50;
while ((x < 15) and (y <58 ))
{
    x ++;
    y ++;
    cout << "Weekend coming" << endl;
    cout << x << "  " << y << endl;
}
cout << x << "  " << y << endl;
int x = 10;
int y = 50;
while ((x < 15) or (y <58 ))
{
    x = x + 1;
    y = y + 1;
    cout << "Weekend coming" << endl;
    cout << x << "  " << y << endl;
}
cout << x << "  " << y << endl;
9 int x = 10;
int y = 50;
while ((x < 15) and (y <58 ))
{
    x ++;
    cout << "Weekend coming";
    cout << x << endl;
}
cout << x << endl;
int x = 10;
while (x < 20)
{
    cout << "Weekend coming" ;
    cout << x << endl;
    x = x + 2;
}
cout << x << endl;
int x = 10;
while (x < 100)
{
    cout << "Weekend coming";
    cout << x << endl;
    x = x * 2;

}
 cout << x << endl;

10)  
Write a program that asks the user to enter a number.  After the user has entered a number, the word "Hello" should appear on screen that many times.
Sample:
Enter a number:   7
Hello
Hello
Hello
Hello
Hello
Hello
Hello
       Enter a number:   3
Hello
Hello
Hello


GET YOUR PROGRAM TO WORK ON THE COMPUTUER.   Then, write your code on the back of the lab sheet.

PRACTICE:

These are for practice and not to be turned in.

a)  Modify program from number 10 that if the user types in a negative number they are re-prompted until they type in a positive number.  THEN, "hello" gets printed on screen that many times.

b)  Write a program that asks the user to enter the purchase price of an item, and the amount tendered.    IF the amount tendered is less than the purchase price, let them enter a new number.    The program should repeat asking them for new numbers until the amount paid is high enough.  However, the program should not let them try more than 5 times.

c) Write a program that writes the numbers 1-50 on the screen.   However, the numbers 1-10 s hould be in the first column, 11-20 in the next column, ... 41-50 in the fifth column.

d)  Write a program that asks the user for 20 numbers and reports their average to the screen.

e)  
  • Write a prgoram that calculates the sum of the numbers 1 through 100.
  • Write a program that calculates the sum of all even numbers between 1 and 1000 (inclusive).