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 to 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:
Initial comments. Read the algorithm for the relation between Distance, Number of posts, Interval d1. |
/* 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. */ |
Variables are declared and defined. |
#include |
The user is prompted for the coordinates of the starting and
ending points. An input check is performed to make sure that xstart < xend |
cout<<" \n Enter the coordinates xstart and xend ( in feet)"<<endl; |
The formulas explained in the algorithm part of the example are used to calculate the interval d1. |
{ |
The for loop structure which generates a given number of coordinates. Note the cast (float)k |
for (k = 1; k <= postnumber ; k++ ) |
After the end of the loop, the remaining statements are executed. |
cout<<" \n The end is located at " << xend << " feet "<< endl; |
The output to the default window. |
/* 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 .... */ |