CSC 306 Introduction to Programming with C++

More on Loops and Algorithm Development


Objectives

Nesting Looping Structures

A two-dimensional table consists of information stored in multiple rows and columns, such as in a multiplication table.

Suppose you want to define a function that outputs a multiplication table for the values from 1 to 6. A good way to start this is to write a simple loop that prints the multiples of a specific number (say 2) all on one line.
Source Code
int j = 1;
while (j <= 6) {
    cout << 2*j << "\t";
    j = j + 1;
}
cout << endl;
Output
2	4	6	8	10	12 

The first line initializes a variable named j, which is going to act as our loop variable. As long as j is less than or equal to 6, the loop repeats, and the value of j increases at each iteration, so it stops when j = 7. Each iteration through the loop, the value j is output to the console, followed by a tab mark. By omitting the endl from the first output statement, all the output is on a single line.

So far, so good. The next step is to put ALL THIS CODE into another loop in order to get multiple rows to print. To do this, instead of using "2" in the output statement, we will use another variable called i:

int i = 1;
while (i <= 6) {
    int j = 1;
    while (j <= 6) {
	cout << i*j << "\t";
	j = j + 1;
    }
    i = i + 1;
    cout << endl;
}
We go through the outer i loop 6 times, and for each iteration, we repeat the inner loop 6 times too.

More on Loops and Designing Algorithms

Suppose we wish to draw a square on the screen as follows:

******
*    *
*    *
*    *
*    *
******
A reasonable approach would be to use a conditional inside the inner loop in which we go through each row of the square and output a solid line when at the top and bottom, but blanks in the middle otherwise:

int i = 1;
while (i <= 6) {
  if (i==1 || i==6) {
    cout << "******" << endl;
  }
  else {
    cout << "*    *" << endl;
  }
  i = i + 1;
}
However, because the solid lines are only drawn twice no matter what the value of i is, a better algorithm would be to use the loop to draw the inner portions of the square:

cout << "******" << endl; // output top line
int i = 1;
while (i <= 4) {
    cout << "*    *" << endl;
    i = i + 1;
}
cout << "******" << endl; // output bottom line


Lab Specifics

This lab is to be done individually.

You are to design a program that will output diamonds onto the console.

  1. Prompt the user to input a positive integer that will refer to the size of a diamond you will draw on the screen. If this number is not positive or zero, thank the user and end the program.
  2. Output a hollow diamond of that size. For example, if the input size is 4, your output should appear like:
       *
      * *
     *   *
    *     *
     *   *
      * *
       *
    
    If the input size is 2, your output should appear like:
       *
      * *
       *
    
    If the input size is 1, your output should appear like:
       *
    
    HINT: Try to use variables to keep track of how many spaces you will need.
  3. Add in a new innovation that is not spelled out above.

Be sure to:

When you have completed your program and have it working to your satisfaction, drop the source code and your Microsoft Word Lab write-up YourLastName_306L5.doc into the CSC306_L05 dropbox on the Academic server.
Back to Introduction to Computer Programming with C++ Homepage