Indexes in an array to reference! HELP!

im using a for loop to find the index values of the tied high scores and store them into string list then reference list in the second for loop to output it to screen however it isnt letting me use an array with index i as an index its self. Is there a way around this or maybe an alternative to still doing it this way so I dont have to change all of my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void printHighest(Student s[], int length){
	int index;
	string list[10];//you're never going to have more than 10 people with a tieing highscore.	
        index = findMax(s, length);
	for(int i = 0; i < length; i++){
		if(s[i].testScore == s[index].testScore )
			list[i] = i;
		
	}
	for(int i = 0; i < length; i++){
		cout << "Highest grade: "  << s[list[i]].testScore
		 	 << " held by " << s[list[i]].fName << " " << s[list[i]].lName << endl;
	}
}


for the time being I simply removed the idea of string list and just put the contents of the second for loop into the if statement above it. However, I am still curious as to if I can reference an index of an array in an index of another array.
Last edited on
You mean something like the following? (Untested code.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void printHighest( Student s[], unsigned length )
{
    unsigned index[10]  = {0} ;
    unsigned highest_score = s[0].testScore ;
    unsigned high_scores = 1 ;

    for ( unsigned i=1; i<length; ++i )
    {
        if ( s[i].testScore > highest_score )
        {
             highest_score = s[i].testScore ;
             index[0] = i ;
             high_scores = 1 ;
        }
        else if ( s[i].testScore == highestScore )
            index[high_scores++] = i ;
    }

    cout << "Highest Grade: " << s[index[0]].testScore << "\nheld by:\n" ;
    for ( unsigned i=0; i<high_scores; ++i )
        cout << '\t' << s[index[i]].fName << " " << s[index[i]].lName << '\n' ;
}


Topic archived. No new replies allowed.