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...

  • 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 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...

  • 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() {          ...

  • In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand....

    In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand. Do not write "#include" and do not write whole "main()" function. Write only the minimal necessary code to accomplish the required task.                                                                                                           Save your answer file with the name: "FinalA.txt" 1) (2 pts) Given this loop                                                                                              int cnt = 1;     do {     cnt += 3;     } while (cnt < 25);     cout << cnt; It runs ________ times    ...

  • using the source code at the bottom of this page, use the following instructions to make...

    using the source code at the bottom of this page, use the following instructions to make the appropriate modifications to the source code. Serendipity Booksellers Software Development Project— Part 7: A Problem-Solving Exercise For this chapter’s assignment, you are to add a series of arrays to the program. For the time being, these arrays will be used to hold the data in the inventory database. The functions that allow the user to add, change, and delete books in the store’s...

  • C++ Questions Please answer quickly! 1. Please use this info to answer next questions -> 2. Please give reasoning 4. Explain why, I think it is 4 but i also think it might be 0. You need to wri...

    C++ Questions Please answer quickly! 1. Please use this info to answer next questions -> 2. Please give reasoning 4. Explain why, I think it is 4 but i also think it might be 0. You need to write a program that uses the speed of light in a vacuum (299792458 meters per second) Realizing that this is a very large value, you run this code to see how large a value you can store as an int, a long...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

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
ADVERTISEMENT