| | |
Print the parallel array index with the largest value?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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:
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:
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
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)
cout << classes[0] << " " << boxes[0] << endl; . . cout << classes[9] << " " << boxes[9] << endl;
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)
largest = money[0]; for(m=0;m<10;m++) { if(money[m] > largest) largest =money[m]; }
Would anybody be able to please lend me a tip?
Thank you
Last edited by R6er; Apr 15th, 2007 at 12:11 am.
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:
Additionally, you can use that variable, m, later to interact with your array of people...
ie.
C++ Syntax (Toggle Plain Text)
largest = money[0]; for(m=0;m<10;m++){ if(money[m] > largest) largest =money[m]; } cout << "Index of person with most money is " << m << endl;
ie.
C++ Syntax (Toggle Plain Text)
cout << "ID of person with most money is " << person[m] << endl;
Last edited by ft3ssgeek; Apr 15th, 2007 at 12:46 am.
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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
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...
and then you would use it as follows:
C++ Syntax (Toggle Plain Text)
int indexOfLargest; largest = money[0]; for(m=0;m<10;m++){ if(money[m] > largest){ largest =money[m]; indexOfLargest=m; } } cout << "Index of person with most money is " << indexOfLargest << endl;
C++ Syntax (Toggle Plain Text)
cout << "ID of person with most money is " << person[indexOfLargest] << endl;
Last edited by ft3ssgeek; Apr 15th, 2007 at 1:56 am.
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Mar 2007
Posts: 15
Reputation:
Solved Threads: 0
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];
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.
![]() |
Similar Threads
- Reading numbers into an array (C++)
- Array Index Problem in Quick Sort (C#)
- Need direction on how start this programm (C++)
Other Threads in the C++ Forum
- Previous Thread: Creating file from C++
- Next Thread: Classes and Drivers
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





