In c++ Write a program that uses an array of structs to store the first name, mi
ID: 3920082 • Letter: I
Question
In c++
Write a program that uses an array of structs to store the first name, middle initial, last name, and score of 5 students.
1. Declare the size of the array of structs.
2. Define your struct globally(i.e before main)
3. Main:
a) you'll declare the array of structs.
b) you'll call a function that willl initialize the values of the array of struct(first names, middle initial, last name, and score) entered by user.
c) then you'll ask the user to enter a name to find then call a function that will search for a student by the name entered by the user. The funtion returns a Boolean value and display an apppropiate message to the user.
d) Next, you'll call a function that will return the average of all the students' scores and dissplay the result to the user.
Write all necessary functions and prototypes. All parameters must be passed by reference.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
struct Student
{
string fname;
string mname;
string lname;
int score;
};
void readData(Student *s,int size);
bool search(Student *s,int size,string searchName);
void calAvgScore(Student *s,int size,double &avg);
int main()
{
const int SIZE=5;
double avg;
string searchName;
struct Student s[SIZE];
readData(s,SIZE);
cin.ignore();
cout<<" Enter the name to search :";
std::getline(std::cin, searchName);
if(search(s,SIZE,searchName))
cout<<searchName<<" found in the array."<<endl;
else
cout<<searchName<<" not found."<<endl;
calAvgScore(s,SIZE,avg);
cout<<" Average Score :"<<avg<<endl;
return 0;
}
void readData(Student *s,int size)
{
for(int i=0;i<size;i++)
{
cout<<" Student#"<<i+1<<":"<<endl;
cout<<"Enter first name:";
cin>>s[i].fname;
cout<<"Enter middle name:";
cin>>s[i].mname;
cout<<"Enter last name:";
cin>>s[i].lname;
cout<<"Enter Score:";
cin>>s[i].score;
}
}
bool search(Student *s,int size,string searchName)
{
string name="";
for(int i=0;i<size;i++)
{
name=s[i].fname+" "+s[i].mname+" "+s[i].lname;
if(name.compare(searchName) == 0)
return true;
}
return false;
}
void calAvgScore(Student *s,int size,double &avg)
{
double sum=0;
for(int i=0;i<size;i++)
{
sum+=s[i].score;
}
avg=sum/size;
}
________________
I will Attach output screen shot also.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.