Question No.1 Implement this program by using User-defined functions and Structures.
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 100), and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Your program must contain at least the following functions:
1. A function to read the students’ data into the array.
2. A function to assign the relevant grade to each student.
3. A function to find the highest test score.
4. A function to print the names of the students having the highest test score.
#include<iostream>
#include<string>
using namespace std;
struct StudentType
{
string Fname;
string Lname;
int testScore;
char Grade;
};
void ReadStud(StudentType* arr, int n)
{
for (int i = 0; i < n; i++)
{
StudentType temp;
cout << "Please, enter Fname of student " << i + 1 << ": ";
cin >> temp.Fname;
cout << "Please, enter Lname of student " << i + 1 << ": ";
cin >> temp.Lname;
arr[i] = temp;
}
}
void GradeAssign(StudentType* arr, int n)
{
for (int i = 0; i < n; i++)
{
cout << "Please, enter testScore and grade for "
<< arr[i].Fname << " " << arr[i].Lname << ":";
cin >> arr[i].testScore >> arr[i].Grade;
}
}
int HighestScore(StudentType* arr, int n)
{
int high = arr[0].testScore;
for (int i = 0; i < n; i++)
{
if (arr[i].testScore > high)
high = arr[i].testScore;
}
return high;
}
void PrintHigh(StudentType* arr, int n)
{
int high = HighestScore(arr, n);
cout << "The highest testScore has(have) this students:\n";
for (int i = 0; i < n; i++)
{
if (arr[i].testScore == high)
cout << arr[i].Fname << " " << arr[i].Lname << endl;
}
}
int main()
{
const int N = 5;
StudentType arrStd[N];
ReadStud(arrStd, N);
GradeAssign(arrStd, N);
for (int i = 0; i < N; i++)
{
cout << arrStd[i].Fname << " " << arrStd[i].Lname<<" "
<< arrStd[i].testScore << " " << arrStd[i].Grade <<endl;
}
PrintHigh(arrStd, N);
}
Comments
Leave a comment