-
Notifications
You must be signed in to change notification settings - Fork 0
/
tugas_m12.cpp
81 lines (72 loc) · 2 KB
/
tugas_m12.cpp
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>
using namespace std;
double calculatemean1(int nilaiindex);
double calculatemean2(int mahasiswaindex);
struct mahasiswa
{
string nama;
string nrp;
int nilai1;
int nilai2;
int nilai3;
} mhs[3];
int main()
{
int i;
for (i = 0; i < 3; i++)
{
cout << "Mahasiswa ke-" << i + 1 << endl;
cout << "Input Nama\t: ";
cin.ignore();
getline(cin, mhs[i].nama);
cout << "Input NRP\t: ";
getline(cin, mhs[i].nrp);
cout << "Input Nilai MK-1\t: ";
cin >> mhs[i].nilai1;
cout << "Input Nilai MK-2\t: ";
cin >> mhs[i].nilai2;
cout << "Input Nilai MK-3\t: ";
cin >> mhs[i].nilai3;
}
cout << "\n----- Hasil Perhitungan -----" << endl;
for (i = 0; i < 3; i++) // print information
{
cout << "Mahasiswa ke-" << i + 1 << endl;
cout << "Nama\t\t: " << mhs[i].nama << endl;
cout << "NRP\t\t: " << mhs[i].nrp << endl;
cout << "Nilai MK-1\t: " << mhs[i].nilai1 << endl;
cout << "Nilai MK-2\t: " << mhs[i].nilai2 << endl;
cout << "Nilai MK-3\t: " << mhs[i].nilai3 << endl;
}
cout << endl
<< "Nilai rata rata tiap mata kuliah" << endl;
for (int i = 1; i <= 3; i++)
{
cout << "MK-" << i << "\t: " << calculatemean1(i) << endl;
}
cout << "\nNilai rata rata tiap mahasiswa" << endl;
for (int k = 0; k < 3; k++)
{
cout << "Mahasiswa ke-" << k + 1 << "\t: " << calculatemean2(k) << endl;
}
return 0;
}
double calculatemean1(int nilaiindex)
{
double sum = 0;
for (int i = 0; i < 3; i++)
{
if (nilaiindex == 1)
sum += mhs[i].nilai1;
else if (nilaiindex == 2)
sum += mhs[i].nilai2;
else if (nilaiindex == 3)
sum += mhs[i].nilai3;
}
return sum / 3.0;
}
double calculatemean2(int mahasiswaindex)
{
double sum = mhs[mahasiswaindex].nilai1 + mhs[mahasiswaindex].nilai2 + mhs[mahasiswaindex].nilai3;
return sum / 3.0;
}