-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray code.cpp
More file actions
136 lines (115 loc) · 3.92 KB
/
Copy pathArray code.cpp
File metadata and controls
136 lines (115 loc) · 3.92 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//Sapnil Basnet
// This program tracks and displays the medal count for various countries
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Structure to represent a country
struct Country {
string name;
int gold;
int silver;
int bronze;
};
//Named constant for the array size
const int NUM_COUNTRIES = 8;
//Function to display the table header
void showResults(const Country countries[]) {
//Header
cout << left
<< setw(3) << "N"
<< setw(15) << "Country"
<< right
<< setw(10) << "Gold"
<< setw(10) << "Silver"
<< setw(10) << "Bronze"
<< setw(10) << "Total"
<< endl;
//Display data for each country
for (int i = 0; i < NUM_COUNTRIES; i++) {
// Calculate total medals for each country
int total = countries[i].gold + countries[i].silver + countries[i].bronze;
//Display country details in a formatted row
cout << left
<< setw(3) << i + 1
<< setw(15) << countries[i].name
<< right
<< setw(10) << countries[i].gold
<< setw(10) << countries[i].silver
<< setw(10) << countries[i].bronze
<< setw(10) << total
<< endl;
}
}
//Function to update the medal counts
void addMedal(Country countries[], int countryNumber, char medalType) {
if (medalType == 'G' || medalType == 'g') {
countries[countryNumber-1].gold++;
} else if (medalType == 'S' || medalType == 's') {
countries[countryNumber-1].silver++;
} else if (medalType == 'B' || medalType == 'b') {
countries[countryNumber-1].bronze++;
}
}
//Function to calculate the total number of medals
int totalMedals(const Country countries[]) {
int total = 0;
// Sum up gold, silver, and bronze medals for all countries
for (int i = 0; i<NUM_COUNTRIES; ++i) {
total += countries[i].gold + countries[i].silver + countries[i].bronze;
}
return total;
}
//Function to find the index of the country with the most total medals
int mostTotalMedals(const Country countries[]) {
int maxTotal = -1;
int maxIndex = 0;
//countries to find the one with the most total medals
for (int i = 0; i < NUM_COUNTRIES; ++i) {
int total = countries[i].gold + countries[i].silver + countries[i].bronze;
if (total > maxTotal) {
maxTotal = total;
maxIndex = i;
}
}
return maxIndex;
}
int main() {
// Array of countries
Country countries[NUM_COUNTRIES] = {
{"Australia", 11, 14, 12},
{"Canada", 5, 0, 1},
{"China", 9, 14, 11},
{"Great Britain", 8, 4, 8},
{"Japan", 8, 10, 10},
{"Netherlands", 7, 6, 7},
{"Russia", 6, 10, 8},
{"USA", 10, 6, 7}
};
int choice;
// Main program loop
while (choice != 0) {
// Display the table of countries
showResults(countries);
// Prompt the user for input
cout << "Enter the country number (0 to quit):"<<endl;
cin >> choice;
if (choice == 0) {
// User chose to quit, exit the loop
break;
}
char medalType;
// Prompt the user for the medal type
cout << "Enter the medal type (G,S, or B):"<<endl;
cin >> medalType;
// Update medal count based on user input
addMedal(countries, choice, medalType);
}
// Calculate total medals and find the country with the most total medals
int total = totalMedals(countries);
int mostMedalsIndex = mostTotalMedals(countries);
// Display the final results
cout << "Total medals awarded: " << total<<endl;
cout << "Country with the most total medals: " << countries[mostMedalsIndex].name << endl;
return 0;
}