Question

can someone please comment through this code to explain me specifically how the variables and arrays...

can someone please comment through this code to explain me specifically how the variables and arrays are working? I am just learning arrays

code is below assignment

C++ Programming from Problem Analysis to Program Design by D. S. Malik, 8th ed.

Programming Exercise 12 on page 607

Lab9_data.txtPreview the document

Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week, they run a certain number of miles and write them into a notebook. At the end of the week, they would like to know the number of miles run each day, the total miles for the week for each runner, and average miles run each day BY THE WHOLE GROUP. Write a program to help them analyze their data. Your program must contain parallel arrays: an array to store the names of the runners and a two-dimensional array of five rows and seven columns to store the number of miles run by each runner each day. Furthermore, your program must contain at least the following functions: a function to read and store the runners’ names and the numbers of miles run each day; a function to find the total miles run by each runner and the average number of miles run each day; and a function to output the results. (Your may assume that the input data is stored in a file and each line of data is in the following form: runnerName milesDay1 milesDay2 milesDay3 milesDay4 milesDay5 milesDay6 milesDay7.)

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

// constant variable declaration
const int COUNT = 5;
const int DAYS = 7;

// This functions tores the names of runners and number of mils run each day
void inputData(string names[], int sizeOfName, double numOfMiles[][DAYS], int rowSize)

{
   //Create input file name
   ifstream infile;
   infile.open("Lab9_data.txt ");
   int checkNames = 0, checkMiles = 0;

   while (checkNames < sizeOfName)
   {
       infile >> names[checkNames];
       while (checkMiles < DAYS)
       {
           infile >> numOfMiles[checkNames][checkMiles];
           checkMiles++;
       }
       checkMiles = 0;
       checkNames++;

   }
}

/* This function is to find the total miles run by each runner and the
avg number of miles run each day*/

void computeMiles(double status[][2], double numOfMiles[][DAYS], int rowSize)
{
   // declare variables
   double total = 0, avg = 0;
   int runCount = 0, num = 0;

   cout << fixed << showpoint << setprecision(2) << endl;
   while (runCount < rowSize)
   {
       while (num < DAYS)
       {
           total = total + numOfMiles[runCount][num];
           num++;
       }
       avg = (total / DAYS);
       status[runCount][0] = total;
       status[runCount][1] = avg;
       total = 0;
       avg = 0;
       num = 0;
       runCount++;
   }
}

// function displays runner's names, total miles and avg of runners
void displayResult(double status[][2], string names[], int sizeOfName)
{
   cout << setw(10) << "Runner Name" << setw(12)
       << "Total miles" << setw(10) << "Average" << endl;
   int runCount = 0;
   while (runCount < sizeOfName)
   {
       cout << setw(10) << names[runCount] << setw(12) << status[runCount][0]
           << setw(10) << status[runCount][1] << endl;
       runCount++;

   }
}

// main function
int main()
{
   // Declare array variables
   string names[COUNT];
   double milesStatus[COUNT][DAYS];
   double output[COUNT][2];
   // call the function
   inputData(names, 5, milesStatus, 5);
   computeMiles(output, milesStatus, 5);
   displayResult(output, names, 5);

   // pause the system
   system("PAUSE");
   return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #0

/* IDEA BEHIND 1-D ARRAY IS TO ACCESS ONE ROW OF TABLE AND 2D ARRAY IS A MATRIX OF GIVEN ROW AND COLUMNS */

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

// constant variable declaration
const int COUNT = 5;
const int DAYS = 7;

// This functions tores the names of runners and number of mils run each day
void inputData(string names[], int sizeOfName, double numOfMiles[][DAYS], int rowSize)

{
//Create input file name
ifstream infile;
infile.open("Lab9_data.txt "); // opening file
//declaring variable
int checkNames = 0, checkMiles = 0;
//looping 0 to 5
while (checkNames < sizeOfName)
{
    //coping 1 runner name from file to names array
infile >> names[checkNames];
while (checkMiles < DAYS)
{
    // coping 1 total miles to numofMiles array
infile >> numOfMiles[checkNames][checkMiles];
checkMiles++; //increaing column
}
checkMiles = 0;
checkNames++;// increasing names array index

}
}

/* This function is to find the total miles run by each runner and the
avg number of miles run each day*/

void computeMiles(double status[][2], double numOfMiles[][DAYS], int rowSize)
{
// declare variables
double total = 0, avg = 0;
int runCount = 0, num = 0;

cout << fixed << showpoint << setprecision(2) << endl;
//running loop from 0 to 5
while (runCount < rowSize)
{
    //calculating total of nummofmiles
while (num < DAYS)
{
        //Accessing numfofMiles[0][0] index of numofMiles array and increasing it
total = total + numOfMiles[runCount][num];
num++;//increasing index
}
avg = (total / DAYS);
//status is 5 row two column matrix
status[runCount][0] = total; // coping total in status matrix[0][0]
status[runCount][1] = avg; // copying avg in status matrix[0][1]
total = 0;
avg = 0;
num = 0;
runCount++;//increasing row count
}
}

// function displays runner's names, total miles and avg of runners
void displayResult(double status[][2], string names[], int sizeOfName)
{
cout << setw(10) << "Runner Name" << setw(12)
<< "Total miles" << setw(10) << "Average" << endl;
int runCount = 0;
// looping from o to 5
while (runCount < sizeOfName)
{
    // printing runner's name at 0 index first and increasing print total of index 0
cout << setw(10) << names[runCount] << setw(12) << status[runCount][0]
<< setw(10) << status[runCount][1] << endl;
runCount++;

}
}

// main function
int main()
{
// Declare array variables
// declaring 1D string array of 5 names
string names[COUNT];
// declaring 2D matrix of of 5 rows 7 columns
double milesStatus[COUNT][DAYS];
// declaring 2D matrix of of 5 rows 2 columns
double output[COUNT][2];
// call the function
inputData(names, 5, milesStatus, 5);
computeMiles(output, milesStatus, 5);
displayResult(output, names, 5);

// pause the system
system("PAUSE");
return 0;

}

/* PLEASE UPVOTE (THANK YOU IN ADVANCE) IF YOU SATISFY WITH THE ANSWER IF ANY QUERY ASK ME IN COMMENT SECTION I WILL RE-EXPLAIN THE QUESTION FOR YOU */

Add a comment
Know the answer?
Add Answer to:
can someone please comment through this code to explain me specifically how the variables and arrays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • I am getting an error that the variable num was not declared in the scope on...

    I am getting an error that the variable num was not declared in the scope on the line of code that has; while(num != 5). Do you know how to fixe this error? #include <iostream> #include <iomanip> #include <string> #include<fstream> #include <cstring> using namespace std; //Declare the structure struct Games { string visit_team; int home_score; int visit_score; }; // Function prototypes int readData(Games *&stats); int menu(); void pickGame(Games *&stats, int size); void inputValid (Games *&stats, int size); void homeTotal(Games *&stats,...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

  • Can someone help me fill in the first part of this c++ code: I need it...

    Can someone help me fill in the first part of this c++ code: I need it to print the menu based on the day selected. #include #include using namespace std; int main(){ double sub_total=0,tax=0,total=0; string food1[3]={"T-Bone Steak","Pork Chops","Iceland Cod"}; double prices_f1[]={20.50,15.45,10.55}; string food2[3]={"Sirloin Steak","Salmon Fillet","Jumbo Shrimp"}; double prices_f2[]={30.35,24.50,15.50}; string food3[3]={"Pork Tenderloin","Buffalo Chicken Sandwhich","Avocado Burger"}; double prices_f3[]={10.60,15.60,25.60}; string drink1[3]={"Raspberry Sgroppino","Sparkling Apple Sangria ","Spiced Cranberry Rum"}; double prices_d1[]={2.55,5.75,4.25}; string drink2[3]={"Champagne Mojitos","Roman Hoilday Cocktail","Dry Martini"}; double prices_d2[]={1.5,3,25,8.45}; string drink3[3]={"Radler Beer","Port wine","Primm's"}; double prices_d3[]={3.55,2.25,4.45};...

  • Can some help me with my code I'm not sure why its not working. Thanks In...

    Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...

  • In C please Write a function so that the main() code below can be replaced by...

    In C please Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original maino: int main(void) { double milesPerHour, double minutes Traveled; double hours Traveled; double miles Traveled; scanf("%f", &milesPerHour); scanf("%lf", &minutes Traveled); hours Traveled = minutes Traveled / 60.0; miles Traveled = hours Traveled * milesPerHour; printf("Miles: %1f\n", miles Traveled); return 0; 1 #include <stdio.h> 3/* Your solution goes here */ 1 test passed 4 All tests passed...

  • I need this done in C++ Can someone help me get my code from crashing. The...

    I need this done in C++ Can someone help me get my code from crashing. The code compiles but it can be easily crashed with user input. The rubric and code below Instructions: Write a program that scores the following data about a soccer player in a structure:            Player’s Name            Player’s Number            Points Scored by Player      The program should keep an array of 12 of these structures. Each element is for a different player on a...

  • Can someone please edit my current code to accept a user input for the text file...

    Can someone please edit my current code to accept a user input for the text file name in lieu of how it currently will read data1.txt. Also if the user enters a data file that isn't ready to be used, cout a statement "cout << "Could not open file " << userInputforData.txt << endl; Another condition that I need to add is if there is a special character anywhere in the word it cannot be an acceptable variable name. could...

  • hello there. can you please help me to complete this code. thank you. Lab #3 -...

    hello there. can you please help me to complete this code. thank you. Lab #3 - Classes/Constructors Part I - Fill in the missing parts of this code #include<iostream> #include<string> #include<fstream> using namespace std; class classGrades      {      public:            void printlist() const;            void inputGrades(ifstream &);            double returnAvg() const;            void setName(string);            void setNumStudents(int);            classGrades();            classGrades(int);      private:            int gradeList[30];            int numStudents;            string name;      }; int main() {          ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT