Image

Imagenallix wrote in Imagecpp

Looking for some more critique. This week's lecture in my Introduction to C++ class covered loops, and here was the assignment to go with it

Homework 4: Cycling
(Loops)

With Lance Armstrong as his inspiration, Joseph Nitua begins a cycling exercise program. About every other day, Nitua plans to get some badly needed exercise in the form of cycling. He carefully records the mileage for each of his "tours". Having compiled a month's worth of exercise and data, he would like a summary of that data.

Since you are the only neighbor with programming expertise, Nitua has asked you to construct a program that will input his collected data and produce a short summary including the total mileage traveled during the month, the average traveled per day, the most traveled in any one day, and the least.

Input for the program is in two parts. The first part is a pair of integers indicating the month and year of the recorded data. These two items are on the first line, separated by one or more spaces. The rest of the data consist of a sequence of floating-point values (one per line) indicating the mileage traveled on a given day.

Output from the program consists of 7 lines (including a blank line) in the following general form

Month: # Year: ####

## = days of cycling
##.# = total distance traveled this month
#.# = average distance per day
#.# = most miles traveled in one day
#.# = least miles traveled in one day

where # and #### represent the month and year (in numeric form) of the summarized data; ## is the number of cycling days of the month; ##.# is the total distance (a floating-point value) traveled during the month; and #.# ( a floating-point value) represents either the average distance traveled, the maxim distance in a single day, or the shortest distance traveled in one day. Floating-point values should be accurate to two places to the right of the decimal point.

Input for the program is in the file cycling.dat and is obtained from the instructor.




/*Author: Dennis Groome
 *
 *Purpose: Produce a short summary of cycling trips totaling the mileage
 *         during the month, daily average, and most and least during a day.
 *
 *Input: Two integers for month and year on one line, and a series of floating
 *       point values, one to each line.
 *
 *Output: seven lines (including a blank line) in the following general form
 *
 *        Month: #    Year: ####
 *
 *        ## = days of cycling
 *        ##.# = total distance traveled this month
 *        #.# = average distance per day
 *        #.# = most miles traveled in one day
 *        #.# = least miles traveled in one day
 */

#include 
#include 

int main()
{ //program: cycling

  int intMonth, intYear, intDays;                               //Month, Year, and number of Days
  double dblDistance, dblDistanceTotal;                         //Daily distance and distance total
  double dblAverageDistance, dblMostDistance, dblLeastDistance; //Daily Average, Highest and Lowest distance in a day
  intDays = 0;                                                  //Initialize Day counter
  dblDistanceTotal = 0;                                         //Initialize Distance Total

  cin >> intMonth >> intYear;     //Get Month and Day
  cin >> dblDistance;             //Get first distance
  dblMostDistance = dblDistance;  //Assume first distance is most
  dblLeastDistance = dblDistance; //Assume first distance in least

  while (cin) { //Input loop to gather daily distance, number of days, and total
    dblDistanceTotal = dblDistanceTotal + dblDistance; //add new distance to total
    intDays = intDays + 1;                             //Increment Day counter
    cin >> dblDistance;                                //Get next distance

    if (dblDistance > dblMostDistance) { //test and correct for most distance
      dblMostDistance = dblDistance;     //Assign most distance
    } //test for most distance

    if (dblDistance < dblLeastDistance) { //test and correct for least distance
      dblLeastDistance = dblDistance;     //Assign least distance
    } //test for least distance

  } //Input loop

  dblAverageDistance = dblDistanceTotal / intDays; //Calculate daily average

  //Display Output
  cout << "Month: " << intMonth << "    Year: " << intYear << endl << endl;      //Display month and year
  cout << intDays << " - days of cycling" << endl;                               //Display Days cyclilng
  cout << setprecision (3);                                                      //Set floating point values to two decimal places.
  cout << dblDistanceTotal << " - total distance traveled this month" << endl;   //Display total distance
  cout << dblAverageDistance << " - average distance per day" << endl;           //Display daily average
  cout << dblMostDistance << " - most miles traveled in one day" << endl;        //Display most miles traveled in a single day
  cout << dblLeastDistance << " - least miles traveled in one day" << endl;      //Display least miles traveled in a single day


  return 0;
}