Hello, I am trying to complete this but I\'m not sure where to start. Instructio
ID: 3756242 • Letter: H
Question
Hello, I am trying to complete this but I'm not sure where to start.
Instructions
In Programming Exercise 13 (Chapter 8), you are asked to write a program to calculate students’ average test scores and their grades. Improve this programming exercise by adding a function to sort students’ names so that students’ data is output into ascending order according to their name.
Pseudo code for Programming
Declare the following prototypes :
void ReadData(ifstream& inf, string n[], double ScoreData[][6], int count);
void calcAverage(double ScoreData[][6], int count);
void calcGrade(double ScoreData[][6], char gr[], int count);
void swapArrays(double list1[][6], double list2[][6], int rowPos1, int rowPos2);
void sortData(string n[], double ScoreData[][6], char gr[], int count);
void Output(string n[], double ScoreData[][6], char gr[], int count);
In the mainline :
Declare 3 arrays :
One for the student names. There are 10 students.
One for all the scores. This is two dimensional.
One for the letter grade.
Declare an input file
Open the input file and verify it is opened successfully. If not, terminate the
program.
Set manipulators so the scores are outputted with 2 decimal positions.
Call the ReadData function.
Call the calcAverage function.
Call the calcGrade function.
Call the sortData function.
Call the Output function.
Close the input file.
End program.
After the mainline :
Define the functions
ReadData function : See the programming example, Code Detection at the end
of chapter 8. Note that the input file is being passed into the function as a
parameter. Use a loop to read through the input records. Inside the loop :
Load the name into the name array
Loop through the scores to load a student’s scores into a row of ScoreData
Set the sixth element of a row in ScoreData to 0.
calcAverage function : See the Sum By Row logic in chapter 8.
Next Page à
calcGrade function : Loop through the rows of the ScoresData array testing
the value of the sixth element in the row. Based on this element, assign a
letter grade to the appropriate element in the Grades array.
sortData function : This function is modeled from parts of In Class
Assignment 5. In this case. Note that the student’s name, all the scores,
the grades will need to be swapped. Here is the logic :
int index;
int smallestIndex;
int minIndex;
char tempChar;
string tempName;
for (index = 0; index < count - 1; index++)
{
//Step a : Find lowest name in the name array
smallestIndex = index;
for (minIndex = index + 1; minIndex < count; minIndex++)
if (n[minIndex] < n[smallestIndex])
smallestIndex = minIndex;
//Step b : Swap names
tempName = n[smallestIndex];
n[smallestIndex] = n[index];
n[index] = tempName;
//Step c : Swap all the scores for a student
swapArrays(tstData, tstData, smallestIndex, index);
//Step d : Swap the letter grade
tempChar = gr[smallestIndex];
gr[smallestIndex] = gr[index];
gr[index] = tempChar;
}
SwapArray function : This function will swap a row of scores
void swapArrays(double list1[][6], double list2[][6], int rowPos1, int rowPos2)
{
for (int i = 0; i < 6; i++)
{
double temp = list1[rowPos1][i];
list1[rowPos1][i] = list2[rowPos2][i];
list2[rowPos2][i] = temp;
}
}
Output function : See the Print function logic in chapter 8.
Output the heading
Loop:
Output name
Output scores (in a loop)
Output letter grade
Add sixth element of the row of scores to SUM
Output average (SUM/count)
The expected program output is :
Name Test 1 Test 2 Test 3 Test 4 Test 5 Average Grade
Aniston 80.00 90.00 95.00 93.00 48.00 81.20 B
Blair 23.00 45.00 96.00 38.00 59.00 52.20 F
Bronson 93.00 94.00 89.00 77.00 97.00 90.00 A
Clark 60.00 85.00 45.00 39.00 67.00 59.20 F
Cooper 78.00 81.00 11.00 90.00 73.00 66.60 D
Gupta 92.00 83.00 30.00 69.00 87.00 72.20 C
Johnson 85.00 83.00 77.00 91.00 76.00 82.40 B
Kennedy 77.00 31.00 52.00 74.00 83.00 63.40 D
Smith 85.00 72.00 49.00 75.00 63.00 68.80 D
Sunny 79.00 85.00 28.00 93.00 82.00 73.40 C
Class average: 70.94
Explanation / Answer
If you have any doubts, please give me comment...
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int ROW = 10;
const int COL = 5;
void readData(string names[], double testScores[][COL]); //completed
void calcGrade(double testScores[][COL], char grades[]); //completed
double classAverage(double testScores[][COL]);
void display(double testScores[][COL], string names[], char grades[]); //completed
//use the sort function and modify to sort on names
void sortData(double testScores[][COL], string names[], char grades[]);
int main()
{
string names[ROW];
double testScores[ROW][COL];
char grades[ROW] = {'Z'};
readData(names, testScores); // call to the readData function -- "using the function"
calcGrade(testScores, grades);
//sort the data
sortData(testScores, names, grades);
display(testScores, names, grades);
//display the class average
cout<<"Class Average: "<<classAverage(testScores)<<endl;
system("pause");
return 0;
}
void readData(string names[], double testScores[][COL])
{
int r = 0, c = 0;
ifstream indata;
indata.open("studentData.txt");
while (!indata.eof())
{ //read each line and store in arrays
if (r < ROW)
{
indata >> names[r]; //read the name and store in names array
for (c = 0; c < COL; c++) //reads each test score on the
indata >> testScores[r][c]; //line and store in array
}
else //using the if and else prevents accidentally exceeding
break; //our array bounds
r++; //increments to the next row
} //end of while
}
void display(double testScores[][COL], string names[], char grades[])
{
cout << fixed << showpoint << setprecision(2);
cout << " ";
cout << setw(10) << left << "Name" << setw(7)<<left << "Test1"<<setw(7)<<left << "Test2"<<setw(7)<<left << "Test3"<<setw(7)<<left << "Test4" <<setw(7)<<left << "Test5"<< setw(10)<<"Average"<<setw(5) << left << "Grade ";
for (int r = 0; r < ROW; r++)
{
cout << setw(10) << left << names[r];
//cout << setw(30) << left;
double avg = 0.0;
for (int c = 0; c < COL; c++){
cout << setw(6)<<left<< testScores[r][c] << " ";
avg += testScores[r][c];
}
avg /= COL;
cout << setw(10)<<avg<<grades[r] << " ";
} //end of outer for loop
}
void calcGrade(double testScores[][COL], char grades[])
{
double sum = 0, average = 0;
for (int r = 0; r < ROW; r++)
{
sum = 0; //resets sum for each row
for (int c = 0; c < COL; c++)
sum += testScores[r][c]; // sums each row
average = sum / COL; //cals average
if (average >= 90) //assigns the appropriate grade
grades[r] = 'A';
else if (average >= 80)
grades[r] = 'B';
else if (average >= 70)
grades[r] = 'C';
else if (average >= 60)
grades[r] = 'D';
else
grades[r] = 'F';
}
}
void sortData(double testScores[][COL], string names[], char grades[]){
for(int i=0; i<ROW; i++){
for(int j=i+1; j<ROW; j++){
if(names[i].compare(names[j])>0){
string temp = names[i];
names[i] = names[j];
names[j] = temp;
char tempG = grades[i];
grades[i] = grades[j];
grades[j] = tempG;
double tempScores[COL];
for(int k=0; k<COL; k++)
tempScores[k] = testScores[i][k];
for(int k=0; k<COL; k++)
testScores[i][k] = testScores[j][k];
for(int k=0; k<COL; k++)
testScores[j][k] = tempScores[k];
}
}
}
}
double classAverage(double testScores[][COL]){
double avg = 0.0;
for(int i=0; i<ROW; i++){
for(int j=0; j<COL; j++){
avg += testScores[i][j];
}
}
avg /= (ROW*COL);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.