Write a Program That Reads Students Names Followed by Their Test Scores C++
I have implemented the program as per the requirements given above. Delight find the following Lawmaking Screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT.
Delight Retrieve TO CREATE A INPUT FILE WITH 25 ENTRIES AS MENTIONED IN THE Chore OR COPY THE FILE AT THE END OF THE POST
i.CODE SCREENSHOT:
2.OUTPUT:
3.CODE:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct Student
{
cord studentFName;
cord studentLName;
int testScore;
char grade;
};
/*A function to read the students information (i.e. Names & test scores) from text file into an assortment*/
void readData(ifstream& in,Student s[],int size){
int northward=0;
while(n<size){
in>>south[n].studentLName>>s[n].studentFName>>due south[n].testScore;
northward++;
}
}
/*
A part to write educatee data (i.e. Names, examination scores, and grades) from the array to a text file.*/
void writeData(ofstream& out,Student s[],int size){
int n=0;
while(due north<size){
out<<s[n].studentLName<<", "<<s[n].studentFName<<" "<<southward[northward].testScore<<endl;
n++;
}
}
//A function to assign the relevant course to each student.
void assignGrades(Educatee due south[], int size){
int i;
for(i=0;i<size;i++)
if (south[i].testScore<60)
s[i].course='F';
else if (s[i].testScore<seventy)
s[i].grade='D';
else if (southward[i].testScore<80)
s[i].form='C';
else if(s[i].testScore<ninety)
s[i].grade='B';
else
s[i].grade='A';
}
/*A role to find the highest exam score.*/
int findHighestScore(const Educatee s[], int size){
int high=0,i;
for(i=0;i<size;i++)
if (loftier < southward[i].testScore )
loftier=s[i].testScore;
render high;
}
/*A function to detect the lowest examination score.*/
int findLowestScore(const Student due south[], int size){
int depression=100,i;
for(i=0;i<size;i++)
if (low > s[i].testScore )
low=s[i].testScore;
return low;
}
/*A function to notice the boilerplate for all test scores.*/
double calculateAverage(const Educatee 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 Educatee 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);
}
render v/size;
}
/*A function to print the names of student having the highest examination score.*/
void printHighestScore(const Student s[], int size){
int high=0,i,hi=-1;
for(i=0;i<size;i++)
if (loftier < due south[i].testScore ){
high=south[i].testScore;
hi=i;
}
cout<<"Highest Test Score: "<<high<<endl;
cout<<"Students having the highest test score: "<<s[hullo].studentLName<<", "<<s[hi].studentFName<<endl;
}
/*A function to impress the names of student having the lowest examination score*/
void printLowestScore(const Student south[], int size){
int low=100,i,li=-1;
for(i=0;i<size;i++)
if (low > due south[i].testScore ){
low=south[i].testScore;
li=i;
}
cout<<"Highest Examination Score: "<<low<<endl;
cout<<"Students having the Lowest examination score: "<<s[li].studentLName<<", "<<southward[li].studentFName<<endl;
}
void print(const Student southward[], int size){
cout<<left<<setw(30)<<"Student Name"<<correct<<setw(20)<<"TestScore"<<right<<setw(7)<<"Class"<<endl;
string proper 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<<correct<<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 non open please check it\n";
exit(0);
}
Educatee s[25];
readData(in,south,25);
assignGrades(s,25);
impress(southward,25);
cout<<"\nThe Average is:"<<calculateAverage(s,25)<<endl;
cout<<"\nThe Variance is:"<<calculateVariance(south,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();
arrangement("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
i StudentGrades.cpp X #include <iostream> 2 #include <cord> 3 #include <fstream> four #include <iomanip> 5 using namespace std; half dozen struct Student seven { 8 cord student FName; 9 string studentLName; x int testscore; 11 char course; 12 - }; 13 /*A part to read the students data (i.e. Names & test scores) from text file into an array*/ xiv &void readData(ifstream& in, Student S[], int size) { 15 int n=0; 16 while (due north<size) { 17 in>>s [northward].studentLName>>s[north].educatee FName>>s [n].testscore; 18 north++; 19 20 - } } 21 /* 22 23 24 25 26 27 LA function to write educatee data (i.e. Names, exam scores, and grades) from the array to a text file. */ şvoid writeData (ofstream& out, Pupil s[l,int size) { int north=0; while (north<size) { out<<s[due north].studentLName<<", "<<s[due north].student FName<<" "<<s[north].testscore<<endl; north++;
ܝܟ StudentGrades.cpp 28 29 -} 30 //A function to assign the relevant grade to each student. 31 şvoid assignGrades (Student s[], int size) { 32 int i; 33 for(i=0;i<size;i++) 34 if (s[i].testscore<60) 35 s[i].grade='F'; 36 else if (south[i].testscore<70) 37 South[i].grade='D'; 38 else if (south[i].testscore<80) 39 due south[i].grade='c'; 40 else if (s[i].testscore<90) 41 s[i].grade='B'; 42 else 43 southward[i].grade='A'; 44. - } 45 /*A function to find the highest test score. */ 46 çint findHighest score (const educatee s[], int size) { 47 int high=0,i; 48 for(i=0;i<size; i++) 49 if (loftier < s[i].testScore ) 50 loftier=s[i].testscore; 51 render high; 52 v
J7 J7 J7 J7 ܝܟ StudentGrades.cpp 10 53 /*A office to find the lowest test score. */ 54 gint findLowestscore (const Pupil Due south[], int size) { 55 int low=100,i; 56 for(i=0;i<size;i++) 57 if (low > southward[i].testscore ) 58 low=s[i].testscore; 59 return low; sixty 61 /*A function to find the boilerplate for all test scores. */ 62 double calculateAverage (const Pupil S[], int size) { 63 double sum=0; 64 int i; 65 for(i=0;i<size;i++) 66 sumurs[i].exam score; 67 return sum/size; 68 69 70 /*A function to find the variance of exam scores*/ 71 gint calculateVariance (const pupil s[], int size) { 72 double sum=0, avg=0,v=0; 73 int i; 74 avg=calculateAverage (s,size); 75 for(i=0;i<size;i++) { 76 v+=(s[i].testscore-avg) * (south[i].testscore-avg); 77 78 render five/size; 79 - } } }
} StudentGrades.cpp 10 80 /*A function to print the names of student having the highest examination score. */ 81 void printHighest score (const Student s[], int size) { 82 int high=0, i,hi=-1; 83 for(i=0;i<size;i++) 84 A if (loftier < s[i].test Score ) { 85 high=s[i].exam Score; 86 hiri; 87 88 cout<<"Highest Test Score: "<<high<<endl; 89 cout<<"Students having the highest test score: "<<due south[how-do-you-do].studentLName<<", "<<s [hullo].educatee FName<<endl; ninety 91 } 92 /*A part to impress the names of pupil having the lowest test score* / 93 şvoid printLowestscore (const Educatee s[], int size) { 94 int depression=100,i,li=-1; 95 for(i=0;i<size;i++) 96 if (low > s[i].testscore ) { 97 low=due south[i].test score; 98 liri; 99 100 cout<<"Highest Test Score: "<<depression<<endl; 101 cout<<"Students having the Lowest exam score: "<<south[li].studentLName<<", "<<south[li].pupil FName<<endl; 102 103 ܝܟ }
} } StudentGrades.cpp 10 104 şvoid impress (const Pupil due south[], int size) { 105 cout<<left<<setw(xxx) <<"Student Proper name"<<correct<<setw(20)<<"Testscore"<<correct<<setw(vii) <<"Grade"<<endl; 106 string name; 107 int high,i; 108 for(i=0;i<size; i++) { 109 name=due south[i].studentLName+", "+s[i].student FName; 110 cout<<left<<setw(xxx) <<proper name<<right<<setw(20) <<s[i].testScore<<right<<setw(7) <<s[i].grade<<endl; 111 112 cout<<endl; 113 114 Fint main() { 115 ifstream in; 116 ofstream out; 117 in.open("input.txt"); 118 out.open("output.txt"); 119 if(in.fail()) 120 cout<<"file did not open delight check information technology\n"; 121 leave(0); 122 123 Educatee due south[25]; 124 readData(in,s, 25); 125 assignGrades (5,25); 126 print (s, 25); 127 cout<<"\nThe Boilerplate is:"<<calculateAverage (5,25)<<endl; 128 cout<<"\nThe Variance is:"<<calculateVariance (s, 25) <<endl; 129 printHighest score (due south, 25); 130 printLowest Score (due south, 25); { ܝܟ
130 131 132 133 134 135 136 137 138 139 printLowest Score (s, 25); writeData (out,v,25); cout<<"Data is Saved into file named 'output.txt' Successfully..."<<endl; out.close(); in.close(); system("interruption"); return 0; - }
0:1 C:\WINDOWS\system32\cmd.exe - a D:\cppsrc\FILES\StudentGrades>yard++ StudentGrades.cpp D:\cppsrc\FILES\StudentGrades>a Student Name John, Smith Hank, Tom David, Hoper James, Michel John, Franklin Derrick, Myers Mary, Anna Margaret, Helen Elsie, Luky Dorothy, Mary Margaret, Ruth Annie, Elizabeth Helen, Mary Elsie, Agnes Lillian, Julia Mildred, Alice Elizabeth, Ruth Frances, Clara Emma, Anna Louise, Marie Peter, John Gorge, Peter Sallt, Bob Dorothy, Olga TestScore Grade 56 F 97 A 78 с 95 A 56 F 89 B 98 A 85 B 78 C 58 F 77 C 45 F 75 C 35 F 48 F 65 D 68 D 39 F 86 B 95 A 84 B 69 D 86 B 78 C
C.Due south. C:\WINDOWS\system32\cmd.exe-a Dorothy, Mary Margaret, Ruth Annie, Elizabeth Helen, Mary Elsie, Agnes Lillian, Julia Mildred, Alice Elizabeth, Ruth Frances, clara Emma, Anna Louise, Marie Peter, John Gorge, Peter Sallt, Bob Dorothy, Olga Rose, Sophie 58 77 45 75 35 48 65 68 39 86 95 84 69 86 78 79 F с F с F F D D F B A B D B с с The Boilerplate is:72.76 The Variance is:322 Highest Test Score: 98 Students having the highest test score: Mary, Anna Highest Test Score: 35 Students having the Everyman test score: Elsie, Agnes Data is Saved into file named 'output.txt' Successfully... Press whatsoever key to go along.
new 1 x StudentGrades.cpp X output.txt x one John, Smith 56 2 Hank, Tom 97 three David, Hoper 78 4. James, Michel 95 5 John, Franklin 56 6 Derrick, Myers 89 vii Mary, Anna 98 8 Margaret, Helen 85 9 Elsie, Luky 78 10 Dorothy, Mary 58 eleven Margaret, Ruth 77 12 Annie, Elizabeth 45 xiii Helen, Mary 75 14 Elsie, Agnes 35 15 Lillian, Julia 48 xvi Mildred, Alice 65 17 Elizabeth, Ruth 68 18 Frances, Clara 39 19 Emma, Anna 86 twenty Louise, Marie 95 21 Peter, John 84 22 Gorge, Peter 69 23 Sallt, Bob 86 24 Dorothy, olga 78 25 Rose, Sophie 79 26
Source: https://itprospt.com/qa/21786/write-a-complete-c-program-that-reads-students
0 Response to "Write a Program That Reads Students Names Followed by Their Test Scores C++"
إرسال تعليق