Write a program to sort the student’s names (ascending order), calculate students’ average test scores and letter grades (Use the 10 point grading scale). In addition, count the number of students receiving a particular letter grade. You may assume the following input file data :
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 48
Gupta 92 83 30 69 87
Muhammed 23 45 96 38 59
Clark 60 85 45 39 67
Patel 77 31 52 74 83
Abara 93 94 89 77 97
Abebe 79 85 28 93 82
Abioye 85 72 49 75 63
(40%) Use four arrays: a one-dimensional array to store the students’ names, a (parallel) two dimensional array to store the test scores, a one-dimensional array to store the student’s average test scores and a one-dimensional array to store the student’s letter grades.
(60%) 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 letter grade,
A function to sort all the arrays by student name, and
A function to output all the results (i.e. sorted list of students and their corresponding grades)
Have your program also output the count of the number of students receiving a particular letter grade.
NOTE : No non-constant global variables are to be used. You can name the arrays and functions anything you like. You can use the operator >= to sort the strings.
In C++

Respuesta :

Answer:

// testscores.txt

Johnson 85 83 77 91 76

Aniston 80 90 95 93 48

Cooper 78 81 11 90 48

Gupta 92 83 30 69 87

Muhammed 23 45 96 38 59

Clark 60 85 45 39 67

Patel 77 31 52 74 83

Abara 93 94 89 77 97

Abebe 79 85 28 93 82

Abioye 85 72 49 75 63

______________________

#include <fstream>

#include <iostream>

#include <iomanip>

using namespace std;

// function declarations

void readData(ifstream& dataIn, double** scores, string names[], int size);

void calculateGrade(double* avgs, char grades[], int size);

void outputResults(string names[], double** scores,double avgs[], char grades[], int size,int NUM_TESTS);

void calculateAverage(double **scores,double avgs[],int NUM_STUDENTS,int NUM_TESTS);    

void sortByName(string names[],double avgs[],char grades[],int NUM_STUDENTS);

void countStudents(string names[],char grades[],char gradeLetter,int NUM_STUDENTS);

int main()

{

  const int NUM_STUDENTS = 10;

const int NUM_TESTS = 5;

// Declaring variables

string name;

int score;

char ch;

// defines an input stream for the data file

ifstream dataIn;

// Defines an output stream for the data file

ofstream dataOut;

// setting the precision to two decimal places

std::cout << std::setprecision(2) << std::fixed;

// Opening the input file

dataIn.open("testscores.txt");

// checking whether the file name is valid or not

if (dataIn.fail())

{

cout << "** File Not Found **";

return 1;

}

else

{

 

// Creating 2-D array Dynamically

double** scores = new double*[NUM_STUDENTS];

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

scores[i] = new double[NUM_TESTS];

// Creating array dynamically

string* names = new string[NUM_STUDENTS];

char* grades = new char[NUM_STUDENTS];

double* avgs = new double[NUM_STUDENTS];

// Closing the intput file

dataIn.close();

// calling the functions

readData(dataIn, scores, names, NUM_STUDENTS);

 

calculateAverage(scores,avgs,NUM_STUDENTS,NUM_TESTS);    

     

 

calculateGrade(avgs, grades, NUM_STUDENTS);

 

sortByName(names,avgs,grades,NUM_STUDENTS);

outputResults(names, scores,avgs, grades, NUM_STUDENTS,NUM_TESTS);

cout<<"Enter Grade Letter :";

cin>>ch;

countStudents(names,grades,ch,NUM_STUDENTS);

 

}

return 0;

}

/* This function will read the data from

* the file and populate the values into arrays

*/

void readData(ifstream& dataIn, double** scores, string names[], int size)

{

// Opening the input file

dataIn.open("testscores.txt");

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

{

dataIn >> names[i];

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

{

dataIn >> scores[i][j];

}

}

dataIn.close();

}

/* This function will find the average of

* each student and find the letter grade

*/

void calculateGrade(double* avgs, char grades[], int size)

{

 

char gradeLetter;

 

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

{

if (avgs[i]>=90 && avgs[i]<=100)

gradeLetter = 'A';

else if (avgs[i]>=80 && avgs[i]<=89.99)

gradeLetter = 'B';

else if (avgs[i]>=70 && avgs[i] <=79.99)

gradeLetter = 'C';

else if (avgs[i]>=60 && avgs[i] <=69.99)

gradeLetter = 'D';

else if (avgs[i]<=59.99)

gradeLetter = 'F';

grades[i] = gradeLetter;

}

}

// This function will display the report

void outputResults(string names[], double** scores,double avgs[], char grades[], int size,int NUM_TESTS)

{

cout << "Name\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage\tGrade" << endl;

cout << "----\t\t-----\t-----\t-----\t-----\t-----\t-------\t-----" << endl;

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

{

cout <<setw(8)<<left<< names[i] << "\t";

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

{

cout << scores[i][j] << "\t";

}

cout<<avgs[i]<<"\t"<<grades[i] << endl;

}

}

/* calculates the average test score per student

* and stores it as the last column of the test score array

*/

void calculateAverage(double **scores,double avgs[],int NUM_STUDENTS,int NUM_TESTS)

{

double average=0.0,tot=0,totAvg=0,overallAvg=0.0;

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

{

tot = 0;

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

{

tot += scores[i][j];

}

average = tot / NUM_TESTS;

avgs[i]=average;

}

}

void sortByName(string names[],double avgs[],char grades[],int NUM_STUDENTS)

{

  string tempName;

  double tempAvg;

  char tempGrade;

  //This Logic will Sort the Array of elements in Ascending order

  int temp;

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

{

for (int j = i + 1; j < NUM_STUDENTS; j++)

{

if (names[i]>names[j])

{

tempName = names[i];

names[i] = names[j];

names[j] = tempName;

 

tempAvg = avgs[i];

avgs[i] = avgs[j];

avgs[j] = tempAvg;

 

tempGrade = grades[i];

grades[i] = grades[j];

grades[j] = tempGrade;

 

}

}

 

}

}

void countStudents(string names[],char grades[],char gradeLetter,int NUM_STUDENTS)

{

  int cnt=0;

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

  {

      if(grades[i]==gradeLetter)

      {

          cnt++;

      }

  }

  cout<<"No of Students Who got the grade letter '"<<gradeLetter<<"' is :"<<cnt<<endl;

}