Question

Write a complete C++ program that reads students names and their test scores from an input...

Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores.

Student data obtained from the input text file should be stored in a structure of type studentType, which has four components: studentFName and studentLName of type string, testScore of type int (testScore is between 0 and 100), and grade of type char. Suppose that the class has 25 students. Use an array of 25 components of type studentType. [Hint: The input text file should consist of 25 records].

Your program must contain the following functions:

  1. A function to read the students data (i.e. Names & test scores) from text file into an array
  2. A function to write student data (i.e. Names, test scores, and grades) from the array to a text file.
  3. A function to assign the relevant grade to each student.
  4. A function to find the highest test score.
  5. A function to find the lowest test score.
  6. A function to find the average for all test scores.
  7. A function to find the variance of test scores.
  8. A function to print the names of student having the highest test score.
  9. A function to print the names of student having the lowest test score

The variance is calculated as follows:

Variance = ∑( Xi – XAvg )2 / N

Where;

∑ = Summation

Xi = Each value in array

XAvg = Average

N = The number of elements in array

[Note: The variance is obtained by subtracting the average from each value in the array, squaring the values obtained, adding them, and dividing by the number of elements in the array.]

Your program must output each student’s name in this form: last name followed by a comma, followed by a space, followed by the first name.

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

I have implemented the program as per the requirements given above. Please find the following Code Screenshot, Output, and Code.

ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT.

PLEASE REMEMBER TO CREATE A INPUT FILE WITH 25 ENTRIES AS MENTIONED IN THE TASK OR COPY THE FILE AT THE END OF THE POST

1.CODE SCREENSHOT:

1 StudentGrades.cpp X #include <iostream> 2 #include <string> 3 #include <fstream> 4 #include <iomanip> 5 using namespace std

ܝܟ StudentGrades.cpp 28 29 -} 30 //A function to assign the relevant grade to each student. 31 şvoid assignGrades (Student s[

J7 J7 J7 J7 ܝܟ StudentGrades.cpp X 53 /*A function to find the lowest test score. */ 54 gint findLowestscore (const Student S

} StudentGrades.cpp X 80 /*A function to print the names of student having the highest test score. */ 81 void printHighest sc

} } StudentGrades.cpp X 104 şvoid print (const Student s[], int size) { 105 cout<<left<<setw(30) <<Student Name<<right<<set

130 131 132 133 134 135 136 137 138 139 printLowest Score (s, 25); writeData (out,5,25); cout<<Data is Saved into file named

2.OUTPUT:

0:1 C:\WINDOWS\system32\cmd.exe - a D:\cppsrc\FILES\StudentGrades>g++ StudentGrades.cpp D:\cppsrc\FILES\StudentGrades>a Stude

C.S. C:\WINDOWS\system32\cmd.exe-a Dorothy, Mary Margaret, Ruth Annie, Elizabeth Helen, Mary Elsie, Agnes Lillian, Julia Mild

new 1 x StudentGrades.cpp X output.txt x 1 John, Smith 56 2 Hank, Tom 97 3 David, Hoper 78 4. James, Michel 95 5 John, Frankl

3.CODE:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct Student
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
/*A function to read the students data (i.e. Names & test scores) from text file into an array*/
void readData(ifstream& in,Student s[],int size){
   int n=0;
   while(n<size){
   in>>s[n].studentLName>>s[n].studentFName>>s[n].testScore;
   n++;
}
}
/*
A function to write student data (i.e. Names, test scores, and grades) from the array to a text file.*/
void writeData(ofstream& out,Student s[],int size){
   int n=0;
   while(n<size){
   out<<s[n].studentLName<<", "<<s[n].studentFName<<" "<<s[n].testScore<<endl;
   n++;
}
}
//A function to assign the relevant grade to each student.
void assignGrades(Student s[], int size){
   int i;
   for(i=0;i<size;i++)
if (s[i].testScore<60)
       s[i].grade='F';
else if (s[i].testScore<70)
       s[i].grade='D';
   else if (s[i].testScore<80)
       s[i].grade='C';
   else if(s[i].testScore<90)
       s[i].grade='B';
else
       s[i].grade='A';   
}
/*A function to find the highest test score.*/
int findHighestScore(const Student s[], int size){
   int high=0,i;
   for(i=0;i<size;i++)
   if (high < s[i].testScore )
       high=s[i].testScore;
   return high;
}
/*A function to find the lowest test score.*/
int findLowestScore(const Student s[], int size){
   int low=100,i;
   for(i=0;i<size;i++)
   if (low > s[i].testScore )
       low=s[i].testScore;
   return low;
}
/*A function to find the average for all test scores.*/
double calculateAverage(const Student s[], int size){
   double sum=0;
   int i;
   for(i=0;i<size;i++)
       sum+=s[i].testScore;
   return sum/size;
}

/*A function to find the variance of test scores*/
int calculateVariance(const Student s[], int size){
   double sum=0,avg=0,v=0;
   int i;
   avg=calculateAverage(s,size);
   for(i=0;i<size;i++){
       v+=(s[i].testScore-avg)*(s[i].testScore-avg);
   }
   return v/size;
}
/*A function to print the names of student having the highest test score.*/
void printHighestScore(const Student s[], int size){
   int high=0,i,hi=-1;
   for(i=0;i<size;i++)
   if (high < s[i].testScore ){
       high=s[i].testScore;
       hi=i;
   }
   cout<<"Highest Test Score: "<<high<<endl;
cout<<"Students having the highest test score: "<<s[hi].studentLName<<", "<<s[hi].studentFName<<endl;
  
}
/*A function to print the names of student having the lowest test score*/
void printLowestScore(const Student s[], int size){
   int low=100,i,li=-1;
   for(i=0;i<size;i++)
   if (low > s[i].testScore ){
       low=s[i].testScore;
       li=i;
   }
   cout<<"Highest Test Score: "<<low<<endl;
cout<<"Students having the Lowest test score: "<<s[li].studentLName<<", "<<s[li].studentFName<<endl;
  
}
void print(const Student s[], int size){

   cout<<left<<setw(30)<<"Student Name"<<right<<setw(20)<<"TestScore"<<right<<setw(7)<<"Grade"<<endl;
string name;
int high,i;
for(i=0;i<size;i++) {
   name=s[i].studentLName+", "+s[i].studentFName;
   cout<<left<<setw(30)<<name<<right<<setw(20)<<s[i].testScore<<right<<setw(7)<<s[i].grade<<endl;
}
cout<<endl;
}
int main()
{
ifstream in;
   ofstream out;
   in.open("input.txt");
   out.open("output.txt");
   if(in.fail()) {
       cout<<"file did not open please check it\n";
       exit(0);
}
Student s[25];
readData(in,s,25);
assignGrades(s,25);
print(s,25);
   cout<<"\nThe Average is:"<<calculateAverage(s,25)<<endl;
   cout<<"\nThe Variance is:"<<calculateVariance(s,25)<<endl;
   printHighestScore(s,25);
   printLowestScore(s,25);
   writeData(out,s,25);
   cout<<"Data is Saved into file named 'output.txt' Successfully..."<<endl;
   out.close();
in.close();
system("pause");
return 0;
}

4.INPUT FILE:

input.txt:

John Smith 56
Hank Tom 97
David Hoper 78
James Michel 95
John Franklin 56
Derrick Myers 89
Mary Anna 98
Margaret Helen 85
Elsie Luky 78
Dorothy Mary 58
Margaret Ruth 77
Annie Elizabeth 45
Helen Mary 75
Elsie Agnes 35
Lillian Julia 48
Mildred Alice 65
Elizabeth Ruth 68
Frances Clara 39
Emma Anna 86
Louise Marie 95
Peter John 84
Gorge Peter 69
Sallt Bob 86
Dorothy Olga 78
Rose Sophie 79
Add a comment
Know the answer?
Add Answer to:
Write a complete C++ program that reads students names and their test scores from an input...
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
  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. 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 and grade of type char. Suppose that the class has 20 students. Use an array of...

  • C++ program that reads students' names followed by their test scores (5 scores) from the given...

    C++ program that reads students' names followed by their test scores (5 scores) from the given input file, students.txt. The program should output to a file, output.txt, each student's first name, last name, followed by the test scores and the relevant grade. All data should be separated by a single space. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, an array of testScores of type int...

  • C++ Student Test Scores

    Write a program in C++ that reads students' names followed by their test scores. The program should output each student's name folloewd by the test scores and therelevant grade. It should also find and print the highest test score and the name of the students having the highest test score. Suppose that the class has 20students. Use an array of 20 components of type studentType.Your program must output each student's name in this form:last name followed by a comma, followed...

  • THIS IS FINAL COURSE ASSIGNMENT, PLEASE FOLLOW ALL REQUIREMENTS. Hello, please help write program in JAVA...

    THIS IS FINAL COURSE ASSIGNMENT, PLEASE FOLLOW ALL REQUIREMENTS. Hello, please help write program in JAVA that reads students’ names followed by their test scores from "data.txt" file. The program should output "out.txt" file where each student’s name is followed by the test scores and the relevant grade, 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 an instance of class variable of type...

  • codeblock c++ language Write a program that reads students test scores in the range 0-200 from...

    codeblock c++ language Write a program that reads students test scores in the range 0-200 from a file until end of file (use e of() to check if ifstream reaches the End of File Stream) and store those scores in an array. 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. Finally, the program outputs the total number of students, each score range...

  • c++ Instructions Overview In this programming challenge you will create a program to handle student test...

    c++ Instructions Overview In this programming challenge you will create a program to handle student test scores.  You will have three tasks to complete in this challenge. Instructions Task 1 Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the...

  • c++ The program will be recieved from the user as input name of an input file...

    c++ The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to search for, when it find...

  • [JAVA/chapter 7] Assignment: Write a program to process scores for a set of students. Arrays and...

    [JAVA/chapter 7] Assignment: Write a program to process scores for a set of students. Arrays and methods must be used for this program. Process: •Read the number of students in a class. Make sure that the user enters 1 or more students. •Create an array with the number of students entered by the user. •Then, read the name and the test score for each student. •Calculate the best or highest score. •Then, Calculate letter grades based on the following criteria:...

  • Write a c++ program which uses a structure having the indicated member names to store the...

    Write a c++ program which uses a structure having the indicated member names to store the following data: Name (student name) IDnum (student ID number) Tests (an array of three test scores) Average (average test score) Grade (course grade) The program will keep a list of three test scores for one student. The program may prompt the user for the name, ID number, and test scores, or these may be assigned within the program. The average test score will be...

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
ADVERTISEMENT