Hi, I'm new to c++ and programming all together and I really need help.
So what I had to do was display a table showing the medals earned by several countries at a certain competition and their total medal count.
Then I'm asked to write a function which returns the index of the array for the highest number found. This index will be returned to function main where it will be used to display the name of the country and its gold medal count.
I've done the first part but I can't seem to figure out how to do the second part. I'm guessing the problem is with the function definition for HighestGold which I basically just guessed at.
I'm a total beginner so please dumb down your explanation as much as you can.
Thanks!
#include <iostream>
#include <string>
#include <conio.h>
#include <iomanip>
const int COLUMNS = 3;
const int COUNTRIES = 7;
const int MEDALS = 3;
int RowTotal(int table[][COLUMNS], int row); //funtion prototype for RowTotal
int HighestGold (int table[][COLUMNS], int col); //function prototype for HighestGold
using namespace std;
int main()
{
int total; //total medals for a country
string countries[] = {"Canada", "China", "Japan" , "Russia",
"Switzerland", "Ukraine", "United States"};
int medalCounts[COUNTRIES][MEDALS] = {{3, 2, 1}, {2, 5, 1}, {1, 4, 0},
{5, 0, 1}, {0, 1, 0}, {0, 0, 1},
{6, 2, 0}};
cout << setw(12) << "Country" << setw(12) << "Gold"
<< setw(12) << "Silver" << setw(10) << "Bronze"
<< setw(10) << "Total" << endl << endl;
cout << "---------------------------------------------------------\n";
//print countries, counts, and row totals
for (int i = 0; i < COUNTRIES; i++)
{
cout << setw(13) << countries[i];
//proccess the ith row
for (int j = 0; j < MEDALS; j++)
{
cout << setw(10) << medalCounts[i][j];
}
total = RowTotal(medalCounts, i); //calculate total medals for the ith row
cout << setw(12) << total << endl << endl;
HighestGold; //find country with highest gold count
cout << "The country with the highest gold count is " << countries << " with "
<< medalCounts << " gold medals.";
}
_getch();
return 0;
}
//-----------------------------------------------------------------------------------------------
int RowTotal(int table[][COLUMNS], int row)
{
int total = 0;
for (int j = 0; j < COLUMNS; j++)
{
total = total + table[row][j];
}
return total;
}
//-----------------------------------------------------------------------------------------------
int HighestGold(int table[][COLUMNS], int col)
{
int index = 0;
int highest = 0;
bool found = false;
while( (!found) && (highest < col) )
{
if (highest == table[highest])
found = true;
else
highest++;
}
if (found)
return highest;
}