Last week we started to look at loops. With that we moved to writing programs that made the computer work harder than we did. This week we have another part of that puzzle. Before we had loops the number of statements executed was equal to the number of statements that we typed into the program. With loops we can repeat through a block of statements any number of times we choose. Similarly until now we must declare variables one at a time. Arrays give us a way to allocated lots at one go.

Arrays

The following line decares an integer variable called i:
int i;
The next line declares 100 integer variables:
int j[100];
They are called j[0], j[1], j[2] .... j[98], j[99].
Note: we start numbering at zero and stop one before the size of the array.
We can assign numbers to a position in an array as if it was an ordinary variable:
i[45] = 666;
And we can use numbers in an array in expressions like any other variable:
i=j[44]+1;
Nearly all the time, however, instead of the 'subscript' of an array being an integer, and therefore indicating a particular position in the array, the subscript is an 'integer expression' -something that evaluates at run time to an integer (this may be as simple as the name of an integer variable). Look at the following:

int loopcount;
int tt[10];
loopcount=0;
while(loopcount<10)
{
tt[loopcount]=loopcount+1;
loopcount=loopcount+1;
}//endwhile

loopcount=0;
while(loopcount<10)
{
cout <<tt[loopcount]<<endl;
loopcount=loopcount+1;
}//endwhile




Nested loops

Loops within loops are said to be nested. Suppose that I want to write out the times tables to 10 (actually printing out tables of one sort or another was an important role of computers in the early days). Sometimes it is easier to do the inner loop first but here I think I would start with the outer one. The outer loop is going to decide which table we are working one and print the title int i;
i=1;
while(i<=10)
{
cout <<i<<" Times Table"#60;<endl;#60;<endl
//
//Inner loop goes here



i=i+1;//increment for outer loop
cout <<endl<<endl; //put a space between the tables
}//end outer while


So we have the program writing out "1 times table" "2 Times table" etc. just not the actual table. The variable i keeps track of this outer loop. We need a new variable that will go from 1 to 10 for each table. Lets declare one called j. Within the outer loop we will create a new inner loop. This will involve:
  1. Setting j to one each time before the while loop
  2. Creating a new while statement that tests for j being less than or equal to 10.
  3. An output line that does something with i times j.
  4. Increment j
Something like:
int i;
int j;
i=1;
while(i<=10)
{
cout <<i<<" Times Table" <<endl<<endl;
//
//Inner loop goes here
j=1;
while(j<=10) { cout <<i<<" times "<<j<<" = "<<i*j<<endl;
j=j+1;
}


i=i+1;//increment for outer loop
cout <<endl<<endl; //put a space between the tables
}//end outer while


Something Else

There is another optional part to an if statement. Very often we want to have an if test where we execute one set of statements if the test is true and another if and only if it isn't. The magic word else with a second (compound?) statement accomplishes this:
if(age>17)
{
cout<<"Old Enough to Vote "<<endl;
}
else
{
cout<<"Not Old Enough to Vote "<<endl;
}

What Is Required for a Project

  1. Project description: Less than a Page! Describe what the project is about. For example, suppose that you want to work on pascals triangle. You might say a little about what it is and why it is useful and how the program is supposed to be helpful. Describe breifly how it is supposed to work in a non computing way. If you have done any research mention it here.
  2. Technical description: Briefly state how the program works, the use of loops, arrays, functions. How they are used to acheif the programs goals.
  3. Indicate that you have tested the program works. Is there any input it can't cope with?
  4. E-mail a working version of the program. It should have appropriate comments including explanation of variables ("variable dictionary"), and laid out for easy reading.
A satisfactory project might include input and output, an if statement in a loop and some sort of calculation. Superior projects would uses techniques like nested loops arrays, function calls etc..

Project ideas

Sausage

This is a game that is used to teach number skills to kids. A number in the range 2-9 is chosen and a group of players in a circle count from 1 substituting the word "sausage" when the number is either a multiple of the chosen number or contains the digit e.g. 13 contains a 3. The project would have two parts. First have the computer print out numbers 1 to 999 making the appropriate sausage substitutes.

Farey sequence

This is a technique for finding fractions in order in the range 0-1. A related problem draws 'Ford circles', touching circles generated by these fractions. See wikipedia etc. for more details.

Repeated designs.

Create a function to draw e.g. a flower/tree in different positions sizes etc.. Call this function in a loop to generate a complicated design.

Totient Numbers

Take the number e.g. 6. How many of the fractions 0/6, 1/6, 2/6...5/6 are in the lowest denominator? There is a well known function for finding lowest common denominators of integers and I think you could use this to find totient numbers to 100 or so.

Sports Competitions

Write a program to organise sport competitions so that every team play avery other team once.

Area of a polygon

Input the coordinates of a polygon into two arrays (X and Y) then find the area using the trapezium rule (the rule is simple, handling the data needs a bit of thinking about).

Bubble Sort

Use an array and nested loops.

Text file handout
Home Page of RDScience