Lab 4 - Looping and Nested Loops

Write your programs in your Odin 2010/4 folder.
Part 1 - goto zero program

Name this program: gotozero.cpp

Start like this...

  $ cd 2010
  $ ./lab-start.sh
  $ cd 4
  $ vi gotozero.cpp

Use a while-loop or do-while-loop in this program.

Ask the user for a number.
Your program will count from the number to zero.
Display all the counting numbers as you go.

sample program run...
$ ./a.out Lab-4 gotozero program... Please enter a number: 14 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
After you get your program working, you may add some code so that it will handle positive and negative input, and still count to zero. sample program run...
$ ./a.out Lab-4 gotozero program... Please enter a number: -12 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
To get this working, you should establish a stepping variable that is 1 or -1. int step; Use the ternary operator to set the step value. If the user enters a positive number, the step is -1. if the user enters a negative number, the step is 1. You can then use just one loop, with no conditional statements inside your loop. Your program will be very clean.

Part 2 - remember highest number program

Name this program: highest.cpp

Write a program that asks the user to enter numbers.

Keep track of the total, and also the highest number entered.

Whenever the user enters a number, check to see if it is the
largest number so far entered. If it is the largest, then output
a message showing the largest number and the accumulated sum of all
numbers entered.

Use the number zero as a sentinel value to end the program.

Program output will look similar to below...
$ ./a.out

Lab-4 highest program...

Enter a number: 5

   5 is your highest number so far!
   5 is the total

Enter a number: 2
Enter a number: 1
Enter a number: 9

   9 is your highest number so far!
   17 is the total

Enter a number: 6
Enter a number: 4
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 0

Program is ending...

    9 was your highest number.
   33 was the total.

Because a user might enter many numbers before entering zero, this is a good opportunity for you to use a file for user input. 1. put some numbers in a file 2. run your program like this: ./a.out < myinput

Part 3 - diver-down flag program

Name this program: ddflag.cpp

Use nested for-loops to print a diver-down flag.

The flag shape will be a square.

A diver-down flag is a flag with a diagonal line from top-left to bottom-right.

Ask the user for width of the flag.
Input of 4 would yield a 4x4 flag, and will look like this...

  ##......
  ..##....
  ....##..
  ......##


Program output should look similar to below...

 $ ./a.out 

 Lab-4 diver-down flag...

 Width of flag: 2

  ##..
  ..##
 $ ./a.out 

 Lab-4 diver-down flag...

 Width of flag: 4

  ##......
  ..##....
  ....##..
  ......##
 $ ./a.out 
 
 Lab-4 diver-down flag...

 Width of flag: 22

  ##..........................................
  ..##........................................
  ....##......................................
  ......##....................................
  ........##..................................
  ..........##................................
  ............##..............................
  ..............##............................
  ................##..........................
  ..................##........................
  ....................##......................
  ......................##....................
  ........................##..................
  ..........................##................
  ............................##..............
  ..............................##............
  ................................##..........
  ..................................##........
  ....................................##......
  ......................................##....
  ........................................##..
  ..........................................##

Programs to be collected at 12:30pm.
  2010/4/gotozero.cpp
  2010/4/highest.cpp
  2010/4/ddflag.cpp