{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture9 Control Structure Examples

# lecture9 Control Structure Examples - CMPT 128 Introduction...

This preview shows pages 1–6. Sign up to view the full content.

1 © Janice Regan, CMPT 128, Jan 2007 CMPT 128: Introduction to Computing Science for Engineering Students Examples using control structures

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 2 Problem Statement You will be tabulating the data resulting from a field count of a particular type of bird. Each observer has provided the number of birds counted, and the area in acres of the observation region in which those birds were counted. You must determine the total number of birds counted, and the number of birds per acre.
© Janice Regan, CMPT 128, Jan 2007 3 Problem Analysis Make the following assumptions: The coordinator of the experiment will be provided you with a file, surveydata.txt, containing the survey data after the survey is complete. The format of the data in that file is as follows The number of observers, numberObs, is an integer on the first line of the file. Following the first line in the file there is one line for each observer. On each of these lines is an integer, NumBirds, indicating the number of birds counted a double, AreaAcres, indicating the area of the observation region You may assume these two numbers are separated by a blank space. There is data for more than one observer. (numberObs>1) More information about the survey is included in the file following the lines of interest to you.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 4 Algorithm Your C program should complete each of the following tasks: Read the number of Observers (numberObs) from the first line of file surveydata.txt Use a for loop to Read the next line of data from file input.txt. This line includes the number of birds (numBirds), and the area in acres (areaAcres). Add the number of birds for this observer to the total number of birds (totalNumBirds) Add the area for this observer to the total area, (totalArea). Determine the number of birds per acre (birdsPerAcre). Print the total number of birds and the number of birds per acre on separate lines. Each line should include an explanation of what the printed number represents.
© Janice Regan, CMPT 128, Jan 2007 5 Choosing control structures (1) A loop is needed, but what kind? We know the number of observers We will execute the loop once for each observer Therefore we need a counting loop In C++ the counting loop can be implemented using a for loop. Note: We could use a conditional while loop to build our own counting loop if we wanted to do more work ourselves.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}