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;
}

Recommended Answers

All 14 Replies

Here's a demo code:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    
    //variables
    //length of array
    const int len=5;
    //array of numbers. Obvs. 24 is highest
    int arr[len]={3, -1, 24, 10, 16};
    //index of current highest
    int HighestSoFar=0;
    //loop until we have tested each array value
    int i=0;
    while (i<len)
    {
        //if the current array value is higher than
        //the current highest, overwrite the current
        //highest variable ("HighestSoFar")
        if (arr[i]>arr[HighestSoFar]){HighestSoFar=i;}
        i++;
    }
    cout<<"HIGHEST INDEX         "<<HighestSoFar<<endl;
    cout<<"HIGHEST VALUE         "<<arr[HighestSoFar]<<endl;
    system("Pause");
}

Like I said, I'm new and pretty lost in the world of programming. Could you please explain what I'm suppose to do with the demo codes you gave using the variables from my code. So again, I want to make a function call in main() to find the highest gold count and the country. What would the length of the array be? 3, 7, or 21? Please be as obvious and detailed as you can (dumb it down)...I will not be offended.

OK I'll break it down line by line. The line numbers reference the code in my above post.

Lines 1-4:

#include <cstdlib>
#include <iostream>

using namespace std;

The top two lines mean that we include the cstdlib and iostream libraries in our program. You will need iostream, but you don't really need cstdlib. It's just there when I start a new project in C++.

Lines 6 - 7:

int main()
{

This is obviously the start of your main function. You would want to make this into a separate function, so you can call it whenever you need it. I just made it in main because it's much quicker when I'm doing a demo.

Lines 9 - 17:

//variables
    //length of array
    const int len=5;
    //array of numbers. Obvs. 24 is highest
    int arr[len]={3, -1, 24, 10, 16};
    //index of current highest
    int HighestSoFar=0;
    //loop until we have tested each array value
    int i=0;

The variable "len" is the length of our array. I made the variable because we need to use it again in the loop that goes through our array. It has to be in the loop as the limiting value (ie, while (i<len) etc.), otherwise we end up trying to get array values that haven't been initialized and our program crashes. You cannot make an array of variable length in C++, so we have to use a "const int". This, as you might guess, is a contant integer that cannot be changed in runtime.

Then I have my int array, of length "len". This just contains 5 numbers I randomly chose that we are going to test out. If you were making a separate function, you would need to put the array, or a pointer to it, in the args. for the function.

The variable "HighestSoFar" is an integer value that will store the index of the current highest number. For example, in the above array, 24 is the highest value. It is at position/index "2" in the array (remember that array indexes start on "0"!). Therefore, HighestSoFar will equal "2". This is so I know where the variable is, meaning that I can reference it in the array at any time.

Next, I have a variable called "i". I use "i" in my loop, which we will come onto just now.

Lines 18 - 25:

while (i<len)
    {
        //if the current array value is higher than
        //the current highest, overwrite the current
        //highest variable ("HighestSoFar")
        if (arr[i]>arr[HighestSoFar]){HighestSoFar=i;}
        i++;
    }

The logic behind the loop is:
When testing through an array for the highest value, index 0 will always hold the current highest value at the start, before we have begun checking. Then, you test index 1 to see if the value of that is higher than the value of index 0. If so, then the index number will be written to "HighestSoFar". This repeats through the entire array. At the end, you will have the index of the highest value in the array in the variable "HighestSoFar".

The "if" statement test whether the currently tested value is bigger than the current highest. If so, it will overwrite "HighestSoFar" to the new index. "i++;" increases the loop number by 1, so we don't test the same index again.

Lines 26 - 29:

cout<<"HIGHEST INDEX         "<<HighestSoFar<<endl;
    cout<<"HIGHEST VALUE         "<<arr[HighestSoFar]<<endl;
    system("Pause");
}

The top two of those lines just print out the highest index in the array (ie. the position of the highest value), and the ACTUAL highest value (ie. what it is). The last line (not including the ending line with just a brace on it) uses a DOS command to pause the program until the user presses a key. This allows us to see the output and exit when we are ready, instead of having the console screen flash up and us not being able to read it.

=====================================================================================

The bit that tests the array are really lines 18 - 25. The other stuff is just preparation and printing (you DO need the preparation, but not the printing).

I hope this helps you out somewhat. I can explain more if you need me to.
Please upvote if you find it useful. Thanks :)

Now onto your code:

1. The length of the array will be the amount of countries whose medal count you want to compare.

2. If you only want to find the highest gold, I suggest making a new array that only contains how many golds each country has. This should be simple to do (have a quick google if your not sure). Then you can pretty much use the logic in my code.

K, so this is what I wrote for function HighestGold which is meant to be called in main to find the name of the country with most gold medals earned and the number.

Ideally, I want the output to look like this:

Country Gold Silver Bronze total
------------------------------------------------------
Canada 3 2 1 6

China 2 5 1 8

Japan 1 4 0 5
(and so on...)

The country with the highest gold count is 6
with 6 gold medals

However, when I run it I get something like:

Country Gold Silver Bronze total
------------------------------------------------------
Canada 3 2 1 6
The country with the highest gold count is 6
with 6 gold medals
China 2 5 1 8
The country with the highest gold count is 6
with 6 gold medals
(and so on...)

int HighestGold(int table[][COLUMNS], int col)
{
	//variables
	//lenght of array
	const int lenght = 7;
	//array of numbers
	int numbers[lenght] = {3, 2, 1, 5, 0, 0, 6};
	//index of current highest
	int HighestSoFar = 0;
	//loop until we have tested each array value
	int i = 0;
	while (i < lenght)
	{
		//if the current array value is higher than the current highest, overwrite the current
		//highest variable HighestSoFar
		if (numbers[i] > numbers[HighestSoFar])
		{
			HighestSoFar = i;
		}
		i++;
	}
	cout << "The country with the highest gold count is " << HighestSoFar << endl;
	cout << "with " << numbers[HighestSoFar] << " gold medlas." << endl;

	return HighestSoFar; 
}
Country Gold Silver Bronze total
------------------------------------------------------
Canada 3 2 1 6
The country with the highest gold count is 6
with 6 gold medals
China 2 5 1 8
The country with the highest gold count is 6
with 6 gold medals
(and so on...)

I take it you want the output to be "The country with the highest gold count is <country>" and not "The country with the highest gold count is 6". This is what you put in your "wanted output".

On the output line 4, you are getting the country name as "6". This is because "HighsetSoFar" is the index to the array value of the country/medal count, NOT the actual value. Say index 5 had the most gold medals. You need to find out which country relates to index 5. There is also something wrong with your printing, seeing as it tells you who got the most golds repeatedly.

Yes, that is what I meant.

Would adding an ampersand somewhere fix this?
For the repeated output, how exactly do I take it out of the loop? Do I just remove it from the HighestGold function and put it in the main? If I did that would I just declare numbers and HighestSoFar again? But when I do that, I get an error for HighestSoFar telling me to use a pointer which we haven't learned about yet so I can't use it.


***Never mind about the loop, I fixed it.

Take the printing for whoever got the highest gold out of the loop and have that after you print the table. Not sure what you mean by the ampersand. Where do you want to add it? I'll have a look at it again in a little bit.

It might be helpful to you to "single step" through your program so that you can see what is happening when.

AND I hate to say this at this point, but you might consider making the whole thing easier to wrap your head around if you start from scratch and restructure your arrays:

Your country array of strings is useful. Your medal array isn't so much. Why not have separate medal arrays like this:

int Gold[7]={3, 2, 1, 5, 0, 0, 6};

Notice that the numbers are in the same sequence as the country array? The first gold medal count (index 0, containing 3) is the gold medal count for Canada, which is also index 0 for countries. This way, Gold[0] plus Silver[0] plus Bronze[0] would yield the total number of medals for countries[0] (Canada)

You could use a "for loop" with an index "i" to cycle through 0 through 6. The first thing to do in the for loop would be to print the country countries, the number of Gold, etc. adding Gold plus Silver plus Bronze to give you a total for countries medals.

After that (still inside the loop) you'd use an if statement to test if the number of gold medals for that value of "i" was the biggest so far. IF so, then you would store the index "i" as "highcountry" and Gold as "highmedals". "highcountry" would be the index to use with "countries" to pick out the correct string. With your data, there is a coincidence of countries[6] (United States) having the highest number of gold medals (6).

After the loop, you just print out the contents of "countries(highcountry)" with "highmedals".

Liking the above suggestion from flukebob. May take a little bit to put right (having to change every mention of the current arrays and restructure some parts), but it would definitely be worth it. Have a shot anyway and come back to us.

I asked my instructor about it and she wants me to stick with what I have so far but, use 2-dimensional arrays in the function HighestGold. Thanks guys for your help so far.

Any more suggestions?

2D arrays for highest gold? If you could give me and explanation of what she meant by that, I could help you with that :)

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} };

I think she want me to use something like the above code instead of making a new 1-D array of just the gold medals.

So for example, if you had

Canada: 1 gold 3 silver 3 bronze
Sweden: 1 gold 4 silver 2 bronze

You would have

{ {1, 3, 3}, {1, 4, 2} }

?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.