Question

Down below I posted the previous lab I had and the current lab that I need...

Down below I posted the previous lab I had and the current lab that I need help with. I also posted what I have so far as my code. Can you please edit my lab code so that it would do whatever is said in the current lab or if you could write a new one ,would work as well. Thank you.

write a program to calculate students average test scores and their grades. you may assume the following input data.

Johnson 85 83 77 91 76

Aniston 80 90 95 93 48

Cooper 78 81 11 90 73

Gupta 92 83 30 69 87

Blair 23 45 96 38 59

Clark 60 85 45 39 67

Kennedy 77 31 52 74 83

Bronson 93 94 89 77 97

Sunny 79 85 28 93 82

Smith 85 72 49 75 63

Previous Lab:

use three arrays. a one dimetional array to store the students names, a two dimensional array to store the test scores, and a parrallel one dimetional array to store grades. your program must contain at least the following function; a function to read and store data into two arrays, a function to calculate the average test score and grade, and a function to output the results. have your program also output the class average.

Current (what I need help with):

rewrite it to use dynamic arrays so that it will work for any number of students and any number of tests. Call this new program Lab11TestScores. Modify the program so that it asks the user to enter the number of students and the number of tests before it reads the data from the file named Lab11ScoresData.txt.

What I have:

//*******************************************************************

#include <iomanip>

#include<string>

#include <fstream>

using namespace std;

int getnumtests(string *names, int **grades);

int getdata(string[], int[][5], int nums, int tests);

void getaverage(int[][5], double[]);

void print(string[], int[][5], double[]);

int main()

{

cout << fixed << showpoint << setprecision(2);

int i = 0; double avg[];

int grades[][5];

string names[];

double average[];

int tot;

tot = getdata(names, grades,nums,tests);

getaverage(grades, average);

print(names, grades, average);

system("pause");

return 0;

}

void print(string names[], int grades[][5], double avg[], int nums,int tests)

{

int i, j;

double total = 0;

double sum = 0;

cout << setw(8) << left << "Names" << setw(8) << left << "Score 1"<< "\tScore 2"

<<"\tScore 3"<<"\tScore 4"<<"\tScore 5"<<"\taverage"<< endl;

for (i = 0; i < nums; i++)

{

sum += avg[i];

cout << setw(8) << left;

cout << names[i] << "\t";

for (j = 0; j < tests; j++)

{

cout << grades[i][j] << " ";

}

cout << avg[i] << "\t";

if (avg[i] >= 90)

cout << "A" << endl;

else if (avg[i] >= 80)

cout << "B" << endl;

else if (avg[i] >= 70)

cout << "C" << endl;

else if (avg[i] >= 60)

cout << "D" << endl;

else

cout << "F" << endl;

}

cout << "\nClass Average = " << sum / 10 << endl << endl;

}

void getaverage(int **grades, double *avg)

{

int i, j, tot; double sum;

avg = new double[sizeof(*grades)/sizeof(1000000.0)];

for (i = 0; i < sizeof(*grades); i++)

{

sum = 0;

for (j = 0; j < sizeof(grades[0]); j++)

sum += grades[i][j];

avg[i] = sum / (double)sizeof(grades[0]);

cout << avg[i] << endl;

}

for (i = 0; i < 10; i++)

{

sum += avg[i];

}

cout << "The average of the test scores = " << sum / 10 << endl;

}

int getdata(string *names, int **grades,int nums, int tests)

{

int i = 0;

ifstream inputFile;

inputFile.open("Lab10ScoresData.txt");

if (!inputFile)

{

cout << "file not found" << endl;

return 0;

}

cout << "Enter the number of students: ";

int nums;

cin >> nums;

names = new string[nums];

cout << "Enter the number of test scores: ";

int tests;

cin >> tests;

grades = new int*[nums];

for (i = 0; i < nums; i++)

grades[i] = new int[tests];

//*grades = new int[tests];

while (inputFile)

{

inputFile >> names[i];

for (int j = 0; j < tests; j++)

inputFile >> grades[i][j];

i++;

}

inputFile.close();

return 0;

}

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

#include <iostream>

#include <iomanip>

#include<string>

#include <fstream>

using namespace std;

bool getdata(string *names, int **grades, int nums, int tests);

void getaverage(int **grades, double *avg, int nums, int tests);

void print(string *names, int **grades, double *avg, int nums,int tests);

int main() {

               cout << fixed << showpoint << setprecision(2);

               int nums = 0, tests = 0;

               int **grades = NULL;

               string *names = NULL;

               double *average = NULL;

               // input of number of students and tests

               cout << "Enter the number of students: ";

               cin >> nums;

               cout << "Enter the number of test scores: ";

               cin >> tests;

               // allocate memory

               names = new string[nums];

               grades = new int*[nums];

               for(int i=0;i<nums;i++)

                              grades[i] = new int[tests];

               average = new double[nums];

               bool success = getdata(names, grades,nums,tests);

               // if file was opened and read successfully

               if(success)

               {

                              getaverage(grades, average, nums, tests);

                              print(names, grades, average, nums, tests);

               }

               system("pause");

               return 0;

}

void print(string *names, int **grades, double *avg, int nums,int tests)

{

               int i, j;

               double sum = 0;

               cout << setw(8) << left << "Names";

               for(int i=0;i<tests;i++)

                              cout<< left << "\tScore "<<(i+1);

               cout<<left<<"\taverage"<< endl;

               for (i = 0; i < nums; i++)

               {

                              sum += avg[i];

                              cout << setw(8) << left;

                              cout << names[i] ;

                              for (j = 0; j < tests; j++)

                              {

                                             cout <<left<<"\t"<<grades[i][j];

                              }

                              cout <<left<<"\t"<<avg[i]<<"\t";

                              if (avg[i] >= 90)

                                             cout << "A" << endl;

                              else if (avg[i] >= 80)

                                             cout << "B" << endl;

                              else if (avg[i] >= 70)

                                             cout << "C" << endl;

                              else if (avg[i] >= 60)

                                             cout << "D" << endl;

                              else

                                             cout << "F" << endl;

               }

               cout << "\nClass Average = " << sum / nums << endl << endl;

}

void getaverage(int **grades, double *avg, int nums, int tests)

{

               int i, j;

               double sum;

               for (i = 0; i < nums; i++)

               {

                              sum = 0;

                              for (j = 0; j < tests; j++)

                              {

                                             sum += grades[i][j];

                              }

                              avg[i] = sum /tests;

                              cout << avg[i] << endl;

               }

               sum = 0;

               for (i = 0; i < nums; i++)

               {

                              sum += avg[i];

               }

               cout << "The average of the test scores = " << sum / nums << endl;

}

bool getdata(string *names, int **grades,int nums, int tests)

{

               int i = 0;

               ifstream inputFile;

               inputFile.open("Lab11ScoresData.txt");

               if (!inputFile)

               {

                              cout << "file not found" << endl;

                              return false;

               }

               i = 0;

               while (!inputFile.eof() && i < nums)

               {

                              inputFile >> names[i];

                              for (int j = 0; j < tests; j++)

                                             inputFile >> grades[i][j];

                              i++;

               }

               inputFile.close();

               return true;

}

//end of program

Output:

Input file:

Output:

Add a comment
Know the answer?
Add Answer to:
Down below I posted the previous lab I had and the current lab that I need...
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
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