Counting Loops:

In many instances of repetition, we know ahead of time how many of those we must perform. So the program can either fix the number of cycles or it can prompt the user for the number of cycles and then proceed accordingly.  These types of loops are called counting loops. For example if we want to list the grades of students in a class, we may first ask the user for the size of the class and then loop through the grades and list them.  Or, if we need to create a table with 16 lines, again we can loop 16 times and create the table.
 
 
The following is an annotated example for a typical problem in engineering .  We want to place a given number of points in between two fixed points, and do so while we keep the distances between all points equal.

Read the purpose and algorithm of the example for more information on this important topic:

Place your mouse in the areas below to see comments about the corresponding sections of code  
Code
 
 

/* Example on counting loop and for statement.  file: 3ex2.cpp
   
FALL 1998
___________________________________
Jacob Y. Kazakia   jyk0
September 16, 1998
___________________________________
 
Purpose: 
This program determines the positions where a given number of 
wooden posts are to be placed. The location of the beginning 
point and the location of the end point are read through the
keyboard. The beginning and end points are marked by concrete
minitowers. The number of the posts to be placed on a straight
line in between these two points is also read through the
keyboard.  The posts are placed at equal intervals. The
location of each post is calculated and outputted to the screen.
 
Algorithm: 
The location of the beginning point and the end point are given 
using coordinates xstart and xend in reference to a line
connecting the two points with origin at a fixed point on this
line.  In order to simplify things we will require that
xend > xstart.  All coordinates will be given in feet.
The distance between the start and the end is:
 
          distance = xend - xstart
 
If the number of posts to be placed in between is postnumber
then the distance  d1 between two consecutive posts will be:

          d1 = distance / ( postnumber + 1  )
 
The location of the first post will be  xstart + d1
the location of the second post will be xstart + 2 * d1
             of the third               xstart + 3 * d1
      etc.
                           */
 
#include 
 
main()
{
float xstart;   // x coordinate of the starting minitower
float xend;     // corresponding coordinate of the end
                // minitower
float distance; // the distance between start and end
float d1;       // the distance between two consecutive 
                // posts
float location; // the location of the current post
 
int postnumber; // the number of wooden posts to be placed
                // at equal intervals between the two
                // minitowers on a straight line.
 
int k;          // a running index indicating the current
                // post
 
cout<<" \n  Enter the coordinates xstart and xend ( in feet)"<<endl;
cout<<" Leave a space in between and make sure xend > xstart"<< endl;
 
cin >> xstart>>xend;
 
if( xstart > xend)
 cout<<" You entered the coordinates incorrectly" ;
else
 
{
cout<<" enter the number of wooden posts you need .....";
cin >> postnumber;
 
distance = xend - xstart;
d1 = distance / (postnumber + 1 );
 
cout<<"  \n The start is located at " << xstart <<" feet"<< endl;

 

 
	  for (k = 1; k <= postnumber ; k++ )
 {
 location = xstart + (float)k * d1 ;
 cout<<" The post # "<< k << "  is at " << location <<" feet"<< end;
 
    }
 
cout<<"  \n The end is located at " << xend << " feet "<< endl;
 
  }
 
cout<<"  \n\n  Enter e (exit) to terminate  ....";
char hold;
cin>>hold;
 
/*    THE OUTPUT:
 
 
Enter the coordinates xstart and xend ( in feet).
Leave a space in between and make sure xend > xstart
4.5  8.98
enter the number of wooden posts you need .....9
 
 The start is located at 4.5 feet
 The post # 1    is at  4.948 feet
 The post # 2    is at  5.396 feet
 The post # 3    is at  5.844 feet
 The post # 4    is at  6.292 feet
 The post # 5    is at  6.74 feet
 The post # 6    is at  7.188 feet
 The post # 7    is at  7.636 feet
 The post # 8    is at  8.084 feet
 The post # 9    is at  8.532 feet
 
 The end is located at 8.98 feet
 
 
  Enter e (exit) to terminate  ....
 
 
 
*/

(text file of the code)

(printable file)

© 2001 J.Y. Kazakia. All rights reserved