ints) Complete the program on the given sheets of paper for the following our pr
ID: 3840374 • Letter: I
Question
ints) Complete the program on the given sheets of paper for the following our program will have a struct named student which contains the following 3 fields: fName, IName, and age. an array of students. Allocate 50 slots so the array can hold up to 50 students a function called fillArray that will fill the array (first name, last name, age) from the keyboard. Ask the user for the number of students she is going to enter. This function should return number of students filled in the array to the caller a function called findUnder21 that will output the first and last names of all the students who are under 21 to an output ile (name it under21.da). a function called avgAge that will calculate the average age and return it. a function called youngestStu that will return the index of the youngest student. a function called age To this function, pass only one student as a parameter and any increment his/her by 1 Do not the entire al (This function should work with age pass student being passed.) a function called printoneStu. To this function, pass one student as a parameter and display his/her first name, last name and age. This function should work with any student being passed.) the main function that will have the declarations and call all the above functions in the same order as listed. When you call the avgAge function, display to the screen the average age to two decimal places. When you call the youngestStu function, display to the screen the first name of the youngest student. When you call ageOneStu and printOneStu, pass the first student to them. Check the sample runs on the next page. of paper. Complete the program on the given sheetsExplanation / Answer
The program is as follows:
#include<iostream.h>
#include<string>
struct student {
string fName;
string lName;
int age;
};
int count; // number of students entered by the user
student list[50];
int fillArray(){
while (1) {
cout << "How many students?";
cin >> count;
if (count > 0 && count < 50)
break;
}
if (count < 50 && count > 0){
for (int i = 0; i<count; i++){
cout << "Enter the info for student # " << i + 1 << endl;
cout << "first name:";
cin >> list[i].fName;
cout << "last name:";
cin >> list[i].lName;
cout << "age:";
cin >> list[i].age;
}
}
else {
cout << "Count exceeds maximum capacity (50)" << endl;
}
return count;
}
void fillUnder21(){
ofstream outfile;
outfile.open("under21.dat");
for (int i = 0; i<count; i++){
if (list[i].age < 21){
outfile << list[i].fName << " " << list[i].lName << endl;
}
}
}
outfile.close();
}
float avgAge(){
float sum, avg;
for (int i = 0; i<count; i++){
sum = sum + list[i].age;
}
avg = sum/count;
return avg;
}
int youngestStu(){
int min;
int index;
min = list[0].age;
for (int i = 0; i<count; i++){
if (min > list[i].age){
min = list[i].age;
index = i;
}
}
}
return index;
}
void ageOneStu(struct student *p){
p->age++;
}
void printOneStu(struct student *p){
cout << p->fName << " " << p->lName << " " << p->age << endl;
}
void main(){
fillArray();
findUnder21();
cout << "The average age is "<< setprecision(2) << avgAge() << endl;
cout << "The youngest student is "<< list[youngestStu()].fName << endl;
ageOneStu(&list[0]);
printOneStu(&list[0]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.