Question

In: Computer Science

Directions: Write a program to calculate students’ average test scores and their grades. You may assume...

Directions:

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

Use three arrays: a one-dimensional array to store the students’ names, a (parallel) two-dimensional array to store the test scores, and a parallel one-dimensional array to store grades. Your program must contain at least the following functions: 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.

So below is my code so far.

I'm getting the error "readData (inputFile, name, scores)"

Is there another way to do this? Please help thanks.

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

const int MAX = 50;

void getdata(ifstream& infile, string name[],int grades[][5], int& n)

{

n = 0;

int i = 0, j = 0;

while (!infile.eof())

{

infile >> name[i];

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

infile >> grades[i][j];

i++;

}

n = i;

}

char calculateGrade(double average)

{

if(average >=90)

   cout<<"A" << endl;

else if(average >=80)

   cout<<"B" << endl;

else if(average >=70)

   cout<<"C\n";

else if(average >=60)

   cout<<"D\n";

else

   cout<<"F\n";

return 0;

}

void calculateaverage(int a[][5],char grade[], double avg[])

{

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

{

double sum = 0.0;

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

sum += a[i][j];

avg[i] = sum / 5.0;

grade[i] = calculateGrade(avg[i]);

}

}

void print(string names[], double avg[], int scores[][5], char grade[], int n)

{

cout << setw(10) << "Student name" << setw(20) << "Test Scores" << endl;

for (int i = 0; i<n; i++) {

cout << setw(10) << names[i];

for (int k = 0; k < 5; k++)

cout << setw(8) << scores[i][k];

cout << endl;

}

}

int main()

{

string name[MAX];

int scores[MAX][5];

double avg[MAX];

char grade[MAX];

  

ifstream inputFile("/Users/veronicadean/Desktop/1501/RandomTemp/2110/Lab8");

if(!inputFile)

{

cout << "Unable to open the input file.";

return 1;

}

readData (inputFile, name, scores)

inputFile.close();

calculateaverage (scores, grade, avg);

cout << name << avg << scores << grade;

  

return 0;

}

Solutions

Expert Solution

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

const int MAX = 50;

void getdata(ifstream& infile, string name[],int grades[][5], int& n)

{

n = 0;

int i = 0, j = 0;

while (!infile.eof())

{

infile >> name[i];

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

infile >> grades[i][j];

i++;

}

n = i;

}

char calculateGrade(double average)

{

if(average >=90)

   return 'A';

else if(average >=80)

   return 'B';

else if(average >=70)

   return 'C';

else if(average >=60)

   return 'D';

else

   return 'F';

return 0;

}

void calculateaverage(int a[][5],char grade[], double avg[])

{

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

{

double sum = 0.0;

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

sum += a[i][j];

avg[i] = sum / 5.0;

grade[i] = calculateGrade(avg[i]);

}

}

void print(string names[], double avg[], int scores[][5], char grade[], int n)

{

cout << setw(10) << "Student name" << setw(20) << "Test Scores"  << setw(30) << "Average" << setw(10) << "Grade" << endl;

for (int i = 0; i<n; i++) {

cout << setw(10) << names[i];

for (int k = 0; k < 5; k++){

cout << setw(8) << scores[i][k];

}

cout << setw(10) << avg[i]; 

cout << setw(10) << grade[i]; 

cout << endl;

}

}

int main()

{

string name[MAX];

int scores[MAX][5];
int n;

double avg[MAX];

char grade[MAX];

  

ifstream inputFile("data.txt");

if(!inputFile)

{

cout << "Unable to open the input file.";

return 1;

}

getdata (inputFile, name, scores, n);

inputFile.close();

calculateaverage (scores, grade, avg);

print(name, avg, scores, grade, n);

  

return 0;

}


Also i am attaching screenshot of code and output for the same.

Note: My input file name was data.txt

Output:


Related Solutions

Instructions: Write a program to calculate students’ average test scores and their grades. You may assume...
Instructions: 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...
You are to write a program that grades students (Maybe I will use it on you)....
You are to write a program that grades students (Maybe I will use it on you). Your program must open and read in "input.txt" (an example of which is below). It will contain the students name and their test scores. You are to calculate the students final grade in the course. Take the average of their test scores and output their average to a file with the below format. For the output.txt ensure that the formatting is in the block...
Write a program that reads students’ names followed by their test scores. The program should output...
Write a program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score. Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testScore of type int (testScore is between 0 and...
Write a program that calculates the average of a group of test scores, where the lowest...
Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions: void getScore() should ask the user for a test score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the five scores to be entered. void calcAverage() should calculate and display the average of the four highest scores. This function...
Write a C++ program that reads a file consisting of students’ test scores in the range...
Write a C++ program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176,...
Write a program that will find the class average of fifteen (15) test scores for five...
Write a program that will find the class average of fifteen (15) test scores for five (5) different tests. The class average must be stored into an integer array. You are to have three separate arrays: Note: _jd represents the initials of the programmer. Your array and function names should be named by replace the initials jd with your first and last initials Arrays Student Names Scores Averages Name of array students_jd scores_jd averages_jd Size of array [15][10] [15][5] .[5]...
Write a program that reads a file consisting of students’ test scores in the range 0–200....
Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200,...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...
Write a program that will calculate numerical and letter grades for a student and output a...
Write a program that will calculate numerical and letter grades for a student and output a report: The information will be in the provided data file studentGrades.txt. The data file consists of several rows of data containing a student name and information for 3 types of assignments: First Name, Last Name, number of homework grades, values for homework grades, percentage of total grade, number of program grades, values for program grades, percental of total grade, number of ex-am grades, values...
Write a program that will calculate numerical and letter grades for a student and output a...
Write a program that will calculate numerical and letter grades for a student and output a report: The information will be in the provided data file studentGrades.txt. The data file consists of several rows of data containing a student name and information for 3 types of assignments: First Name, Last Name, number of homework grades, values for homework grades, percentage of total grade, number of program grades, values for program grades, percental of total grade, number of ex-am grades, values...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT