Function

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
 #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 findavg(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 
Maybe something like:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<string>
using namespace std;

const size_t NO_STUD {5};

struct student {
	string fnum;
	string name;
	int age;
	double avscores;
};

void create_data(student stud[]);
double avg(student stud[]);
void findstudent(student stud[]);
void findavg(student stud[]);

void main() {
	student stud[NO_STUD];

	create_data(stud);

	cout << "\nTotal avrscores for 5 students is:" << avg(stud) << '\n';

	findstudent(stud);
	findavg(stud);
}

void create_data(student stud[])
{
	for (int i = 0; i < NO_STUD; ++i) {
		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;
		cin.ignore(1000, '\n');
	}
}

double avg(student stud[])
{
	double total {};

	for (int i = 0; i < NO_STUD; ++i)
		total += stud[i].avscores;

	return total / 5;
}

void findstudent(student stud[])
{
	for (int i = 0; i < NO_STUD; ++i)
		if ((stud[i].age <= 20) && (stud[i].avscores < 5)) {
			cout << '\n' << stud[i].fnum;
			cout << '\n' << stud[i].name;
			cout << '\n' << stud[i].age;
			cout << '\n' << stud[i].avscores;
		}
}

void findavg(student stud[])
{
	int no {};
	double sum {};

	for (int i = 0; i < NO_STUD; ++i)
		if (stud[i].avscores > 5) {
			++no;
			sum += stud[i].age;
		}

	cout << no << " students have avscores > 5. The average age is " << sum / no << '\n';
}

Topic archived. No new replies allowed.