I have a parallel array of size 10 with several different size numbers in them.

Within my program I put a series of cout statments for the different array indexes so that was known, and here they are:

cout << classes[0] << " " << boxes[0] << endl;
cout << classes[9] << " " << boxes[9] << endl;

The code actually showed indexes 0 through 9, and here is the output:

3 80
4 234
2 54
1 78
10 142
5 87
6 34
7 98
8 43
9 62

Okay, the first column is the numbers assigned to people, the second column is the amount of money the people have, each person is assigned a number.

What I am looking to do is print the number of the person with the most money.

I have written this code:

largest = money[0];
   if(money[m] > largest)
    largest =money[m];

Although this code will print the highest dollar, I am lacking the understanding of how to print the corresponding index value of the person containing that highest amount of money...

Would anybody be able to please lend me a tip?
Thank you

10 Years
Discussion Span
Last Post by R6er

once you know the index of the largest amount of money, hang on to that variable (m in your code) and then you can print that variable to the screen...try something like this:

largest = money[0];
   if(money[m] > largest)
   largest =money[m];
cout << "Index of person with most money is " << m << endl;

Additionally, you can use that variable, m, later to interact with your array of people...

cout << "ID of person with most money is " << person[m] << endl;

Thank you for you taking the time to respond, although, I have tried both in what you had said and neither work.

If I use the variable 'm' with cout I get the number '10', I beleive that to be bcause 'm' is refering to the counter.

If I use the "money[m]" in a cout I will get a very odd number such as -1075262056 .

These are the things I have tried along with a couple of others before posting, sorry I should have mentioned.

Any other ideas?

Thank you


oops! My bad...screwed up there...when you find the index of the largest you need to assign that index value to another value...try something like this...

int indexOfLargest;
largest = money[0];
   if(money[m] > largest){
   largest =money[m];
cout << "Index of person with most money is " << indexOfLargest << endl;

and then you would use it as follows:

cout << "ID of person with most money is " << person[indexOfLargest] << endl;

Interesting, when I tried that it threw out the number 9 verses when I cout 'm' it throws out the numer 10. The number it should actually be throwing out is 4.
There must be a way of refering to that, it seems Ive tried everything but that.
Thanks again

The index of the highest is [1]. This is a parallel array with money on one array holding the highest total and persons on the other array holding the "ID" number of the person. Hence the highest amount of money holds the same index value but different array as the money, which is the person array.


Okay okay, I got it!
Forgeting those curly braces must have made my answers sku'd all day.

What I did was add this, persons being the other array.
Its pretty obvious but without those curly's my answers were comming up odd.

Thank you again ft3ssgeek

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.