Question

In: Computer Science

Write a program to sort the student’s names (ascending order), calculate students’ average test scores and...

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

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

  2. (60%) Your program must contain at least the following functions :

    1. A function to read and store data into two arrays,

    2. A function to calculate the average test score and letter grade,

    3. A function to sort all the arrays by student name, and

    4. A function to output all the results (i.e. sorted list of students and their corresponding grades)  

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

Solutions

Expert Solution

// 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;
}

_______________________________

Output:

_____________________Thank You


Related Solutions

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...
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...
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...
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
Write a program that determines a student’s grade. The program will read three types of scores...
Write a program that determines a student’s grade. The program will read three types of scores (quiz, mid-term, and final scores) and determine the grade based on the following rules: -if the average score =90% =>grade=A -if the average score >= 70% and <90% => grade=B -if the average score>=50% and <70% =>grade=C -if the average score<50% =>grade=F Using Switch Statement Need to code to be simple to understand. No pointers should be used
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 prompts the user to enter the number of students and each student’s...
Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score and the student with the second-highest score. Use the next() method in the Scanner class to read a name rather using the nextLine() method. This is my code , but i get this error Enter the number of students: Exception in thread "main" java.util.InputMismatchException    at java.util.Scanner.throwFor(Scanner.java:871)    at java.util.Scanner.next(Scanner.java:1494)    at...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT