Function
Dec 17, 2020 at 12:04am UTC
Hello guys.I'm new at c++ programming.I have a question for you.The question is : Add a new function that find how many students have avscores>5 and calculate their average age.Which function should i need here?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
//Create a structure student with faculty number, name, age, avrscores. Create array with 5 students and calculate all avrscores,and
//find the student with age<20 and scores<5.
#include<iostream>
#include<string>
using namespace std;
struct student {
string fnum;
string name;
int age;
double avscores;
};
void create_data(student stud[]);
double avg(student stud[]);
void findstudent(student stud[]);
void main() {
student stud[5];
create_data(stud);
cout << endl << "Total avrscores for 5 students is:" << avg(stud);
findstudent(stud);
system("pause" );
}
void create_data(student stud[])
{
for (int i = 0; i < 5; i++)
{
cin.ignore(1000, '\n' );
cout << endl << "Please enter faculty number" ;
getline(cin, stud[i].fnum);
cout << endl << "Please enter student name" ;
getline(cin, stud[i].name);
cout << endl << "Please enter student age" ;
cin >> stud[i].age;
cout << endl << "Please enter student scores" ;
cin >> stud[i].avscores;
}
}
double avg(student stud[])
{
double total(0.0);
for (int i = 0; i < 5; i++)
total = total + stud[i].avscores;
return total / 5;
}
void findstudent(student stud[])
{
for (int i = 0; i < 5; i++)
if ((stud[i].age <= 20) && (stud[i].avscores < 5))
{
cout << endl << stud[i].fnum;
cout << endl << stud[i].name;
cout << endl << stud[i].age;
cout << endl << stud[i].avscores;
}
}
//Homework: Add a new function that find how many students have avscores>5 and calculate their average age
Dec 17, 2020 at 12:12am UTC
Hello kerem59,
If
http://www.cplusplus.com/forum/beginner/274858/ did not answer your question I am not sure what you are looking for except it is the function(s) you hae not written yet.
In the "create_data" function line 33 should come after line 41 to work properly.
After that I will have to find what I worked on earlier to see what I did.
Andy
Dec 17, 2020 at 1:17am UTC
Guess this means that you have your answer?
That was fun to work on.
Last edited on Dec 17, 2020 at 1:18am UTC
Topic archived. No new replies allowed.