c++ Write a program to allow user enter an array of structures, with each struct
ID: 3713016 • Letter: C
Question
c++
Write a program to allow user enter an array of structures, with each structure containing the firstname, lastname and the written test score of a driver.
· Your program should use a DYNAMIC array to make sure user has enough storage to enter the data.
· Once all the data being entered, you need to design a function to sort the data in descending order based on the test score.
· Another function should be designed to display the hightest, lowest and average test scores in all the test takers along with their name.
Explanation / Answer
#include <iostream>
#include<conio.h>
#include<string.h>
using namespace std;
struct student // structure named student
{
string name;
string lname;
int score;
};
void sort1(struct student std[],int no); // function declaration
void display(struct student std[],int no); //function declaration
int main(){
struct student stud[200];
int i,n;
clrscr();
cout<<"How many records of students have to enter::";
cin>>n;
for(i=0; i<n; i++){ //taking values from user
cout<<endl << "Student " << i + 1 << endl;
cout << "First name::";
cin >> stud[i].name;
cout << "Enter Last name::";
cin >> stud[i].lname;
cout << "Enter student's score ::";
cin >> stud[i].score;
}
for(i=0; i<n; i++)
{ //printing values
cout << "Student " << i + 1 << endl;
cout << "First name : " << stud[i].name << endl;
cout << "Last Name : " << stud[i].lname<< endl;
cout << "score : " << stud[i].score << endl;
}
cout<<endl<<"after sorting"<<endl;
sort1(stud,n); //calling the sort function to ort the data in descending order based on the test score.
getch();
return 0;
}
void sort1(struct student std[],int no) //function implementation of sort
{
int i,j,temp;
string temp1;
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
if(std[i].score<std[j].score)
{
temp=std[i].score;
std[i].score=std[j].score;
std[j].score=temp;
std[no+1].name=std[i].name;
std[i].name=std[j].name;
std[j].name=std[no+1].name;
temp1=std[i].lname;
std[i].lname=std[j].lname;
std[j].lname=temp1;
}
}
}
display(std,no); //calling the display function
}
void display(struct student std[],int no) // funtion for the displaying data
{
int i,total=0;
float avrg;
cout<<"Higest score is"<<std[0].score<<"by"<<std[0].name<<" "<<std[0].lname<<endl;
cout<<"Lowest score is"<<std[no-1].score<<"by"<<std[no-1].name<<" "<<std[no-1].lname<<endl;
for(i=0;i<no;i++)
{
total=total+std[i].score;
}
avrg=total/no;
cout<<"The avarage score is "<<avrg;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.