| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
2D array easy q
okay i feel like an idiot asking this question but i just can't seem to get it to work.
How do you output the row number with the largest element in a 2D array? Ive got the array set up with students and quizzes, and i want to output "the student with the highest score is"<<rownumber; this is my function for the array-->mark[quiznumber][stud_num] int r,largest=0; largest = mark[0][0]; for (int c=0;c<quiznumber;c++) for (int r=0;r<stud_num;r++) if (mark[r][c]>largest) largest=mark[r][c]; cout<<"The student with best quiz mark is student: "<<r<<endl; it always outputs r as being the value of quiznumber ................. can anyone explain how to do it ??? |
|
#2
|
|||
|
|||
|
ok well i thought i figured it out when i did this:
int row=0,largest=0; largest = mark[0][0]; for (int c=0;c<quiznumber;c++) for (int r=0;r<stud_num;r++) if (mark[r][c]>largest) r=row; cout<<"The student with best quiz mark is student: "<<row<<endl; but it still didnt work ![]() |
|
#3
|
||||
|
||||
|
in your if() statement, you also need to set "largest" equal to the new largest one.... because this way you initialize "largest" to 0 and only compare everything to it after that....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > 2D array easy q |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|