944,127 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3036
  • C++ RSS
Apr 15th, 2007
0

Print the parallel array index with the largest value?

Expand Post »
Hi,
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:
C++ Syntax (Toggle Plain Text)
  1. cout << classes[0] << " " << boxes[0] << endl;
  2. .
  3. .
  4. 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:

C++ Syntax (Toggle Plain Text)
  1. largest = money[0];
  2. for(m=0;m<10;m++)
  3. {
  4. if(money[m] > largest)
  5. largest =money[m];
  6. }
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
Last edited by R6er; Apr 15th, 2007 at 12:11 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R6er is offline Offline
15 posts
since Mar 2007
Apr 15th, 2007
0

Re: Print the parallel array index with the largest value?

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:
C++ Syntax (Toggle Plain Text)
  1. largest = money[0];
  2. for(m=0;m<10;m++){
  3. if(money[m] > largest)
  4. largest =money[m];
  5. }
  6. 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...
ie.
C++ Syntax (Toggle Plain Text)
  1. cout << "ID of person with most money is " << person[m] << endl;
Last edited by ft3ssgeek; Apr 15th, 2007 at 12:46 am.
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Apr 15th, 2007
0

Re: Print the parallel array index with the largest value?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R6er is offline Offline
15 posts
since Mar 2007
Apr 15th, 2007
0

Re: Print the parallel array index with the largest value?

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...
C++ Syntax (Toggle Plain Text)
  1. int indexOfLargest;
  2. largest = money[0];
  3. for(m=0;m<10;m++){
  4. if(money[m] > largest){
  5. largest =money[m];
  6. indexOfLargest=m;
  7. }
  8. }
  9. cout << "Index of person with most money is " << indexOfLargest << endl;
and then you would use it as follows:
C++ Syntax (Toggle Plain Text)
  1. cout << "ID of person with most money is " << person[indexOfLargest] << endl;
Last edited by ft3ssgeek; Apr 15th, 2007 at 1:56 am.
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Apr 15th, 2007
0

Re: Print the parallel array index with the largest value?

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.
Hrmm...
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.
Last edited by R6er; Apr 15th, 2007 at 2:01 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R6er is offline Offline
15 posts
since Mar 2007
Apr 15th, 2007
0

Re: Print the parallel array index with the largest value?

uh, I did edit my most recent post...notice the curly braces around the if statement?
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Apr 15th, 2007
0

Re: Print the parallel array index with the largest value?

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
indexOfLargest=persons[m];
Last edited by R6er; Apr 15th, 2007 at 2:13 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R6er is offline Offline
15 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Creating file from C++
Next Thread in C++ Forum Timeline: Classes and Drivers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC