In: Computer Science
In the same file, complete the following exercises in the
author’s pseudocode as presented in the
text book and material on Blackboard in this chapter, and following
all requirements for good
program design that were shown in Chapter 2 and all examples since
then.
At the Summer Olympic Games every four years, for historical
reasons, athletes represent
National Olympic Committees (NOCs) rather than strictly countries.
For the sake of convenience
in our program, let us refer to them simply as “nations”. As of the
2016 Rio Olympics, then,
there were 206 nations eligible to participate in the Olympics.
Write a program that could be
used to represent the medal table for the next Olympics that will
be held in Tokyo in 2020. Since
we do not know how many nations will actually participate, our
program will have to plan on
there being a maximum of 225 nations (in case some more NOCs are
recognized before the 2020
Olympics), but must adapt to fewer nations actually being present.
So, write a program that
1. Allows a user to enter
a. The names of the nations that are participating
b. The number of gold medals that nation has won
c. The number of silver medals it has won
d. The number of bronze medals it has won
e. “ZZZ” as the nation’s name to indicate that they have finished
entering input
2. Outputs a list of sentences that describe each nation’s
performance, e.g., for the 2016
Olympics, to describe the performance of the United States, this
program would have
output “United States won 46 gold, 37 silver, and 38 bronze
medals, for a total of 121 medals” . Below this list, the program
must
output the total number of gold medals awarded, silver medals
awarded and bronze
medals awarded, and total of all medals awarded.
3. Scans for and outputs
a. The names of the nations with the most and least gold medals,
along with their
gold medal counts, e.g., “United States won the most gold
medals: 46”
b. Similarly, the names of the nations with the most and least
silver and bronze
medals respectively
4. Allows the user to enter the name of a nation that they want
to search for, and if it finds
that nation, outputs a sentence in the format shown in #2
above.
All of the above must be done by applying the concepts and
algorithms shown in Chapter 6, both
in the text book and in the material provided on Blackboard, and
building on what we have
learned in all the chapters before this one.
The only data that this program stores is the names of the
nations, and the matching counts of
gold, silver and bronze medals. All calculations and scanning for
highest and lowest, etc., must
be done by the program after the user has entered the sentinel to
indicate the end of their input.
All output sentences described above must be generated when they
are required.
Book this is from is "Programming Logic and Design 8th Edition" by: Joyce Farrell. Must be written in pseudocode that resembles that in the book.
this is what I have so far. It probably isn't right, but it will at least give you some structure to follow. Any help is appreciated.
Start
Declarations
string EXIT = ZZZ
NATION_CAPACITY = 225
string
NATION_NAME[NATION_CAPACITY]
num goldMedals
num silverMedals
num bronzeMedals
num totalMedals
num count
num nextNation
num mostGold
num leastGold
num mostSilver
num leastSilver
num mostBronze
num leastBronze
output "Welcome to our Medal Tracker
Program."
output "This program will allow the user to enter the
name of nations participating in the olympics, allow
the user to enter the number of
gold, silver, and bronze medals that nation has won."
output "Please enter the name of the first nation
(type ", ZZZ, " to stop): "
input nextNation
count = 0
while nextNation <> ZZZ AND count <
NATION_CAPACITY
NATION_NAME[NATION_CAPACITY] =
nextNation
output "Please enter the amount
of gold medals this nation has won: "
input goldMedals[count]
output "Please enter the amount
of silver medals this nation has won: "
input silverMedals[count]
output "Please enter the amount
of bronze medals this nation has won: "
input bronzeMedals[count]
count = count + 1
output "Please enter the next
nation or ", ZZZ, " to stop: "
input nextCustomerID
endwhile
output "You entered data for ", count, " nations."
totalMedals = goldMedals + silverMedals + bronzeMedals
output "The nation ", nextNation, " won ",
goldMedals, " gold medals, ", silverMedals, " silver medals, and ",
bronzeMedals, "
bronze medals, for a total of ",
totalMedals, " medals.
mostGold =
The logic of the program is as follows:
Start
Declarations
string EXIT = ZZZ
NATION_CAPACITY = 225
string NATION_NAME[NATION_CAPACITY]
num g #gold
num s #silver
num b #bronze
num totalMedals
num count
num nextNation
num mostGold
num leastGold
num mostSilver
num leastSilver
num mostBronze
num leastBronze
output "Welcome to our Medal Tracker Program."
output "This program will allow the user to enter the name of nations and enter the number of gold, silver, and bronze medals that nation has won."
output "Please enter the name of the nation (type ", ZZZ, " to stop): "
input nextNation
count = 0
/* Function add_name adds the given country to the struct 'country' and sets the amount of every medal to 0. */
struct country *add_name(struct country *array, int len, const char *c_name)
if (len == 0)
array = malloc(sizeof(struct country)); /* When the array is created, the memory allocated for the array is the size of one 'country' struct. */
else
array = realloc(array, (len + 1) * sizeof(struct country)); /* More memory is allocated for the array. */
}
strcpy(array[len].name, c_name);
array[len].name[21] = '\0';
array[len].g = 0;
array[len].s = 0;
array[len].b = 0;
return array;
}
/* Function add_medals adds medals based on the given parameters. */
/* This function also checks if the given country has already been added to the struct 'country' and tells the users if it hasn't. */
int add_medals(struct country *array, int len, const char *c_name, signed int g, signed int s, signed int b)
{
int i = 0;
while (i < len) {
if (strcmp(array[i].name, c_name) == 0) {
array[i].g += g;
if (array[i].g < 0) { /* Here we check if the amount of the medals is less than zero and if this is the case, we set the amount of the medals to zero */
array[i].g = 0;
}
array[i].s += s;
if (array[i].s < 0) {
array[i].s = 0;
}
array[i].b += b;
if (array[i].b < 0) {
array[i].b = 0;
}
i = -1;
break;
}
i++;
}
/* Function print_medals orders the countries based on their medals and prints the countries and their medals in this order. */
int print_medals(struct country *array, int len)
{
int i;
int a;
struct country temp;
struct country c;
int d;
int flag = 0;
for (i = 0; i < len - 1; i++) {
c = array[i];
for (a = len - 1; a > i; a--) {
if (array[a].g > c.g) {
c = array[a];
d = a;
flag = 1;
}
else if (array[a].g == c.g && array[a].s > c.s) {
c = array[a];
d = a;
flag = 1;
}
else if (array[a].g == c.g && array[a].s == c.s && array[a].b > c.b) {
c = array[a];
d = a;
flag = 1;
}
else if (array[a].g == c.g && array[a].s == c.s && array[a].b == c.b && strcmp(array[a].name, c.name) < 0) {
c = array[a];
d = a;
flag = 1;
}
}
if (flag == 1) {
temp = array[i];
array[i] = c;
array[d] = temp;
}
flag = 0;
}
for (i = 0; i < len; i++) {
printf("%s, %d, %d, %d\n", array[i].name, array[i].g, array[i].s, array[i].b);
}
return 0;
}