help returning index

This function is supposed to return the index of the highest temperature. What I have now returns the actual highest temperature so how would I change this to output the index instead?
1
2
3
4
5
6
7
8
9
10
11
12
13
int indexHighTemp(int temperatures[][12])
{
    int highIndex;
    for (int r=0; r<2; r++)
    {
       int largest=temperatures[r][0];
        for (int c=0; c<12; c++)
        if(largest<temperatures[r][c])
        largest=temperatures[r][c];
        
       return largest;
        }
}
Whenever you find the highest temperature, just keep track of which index it was in a separate variable.
What would that look like? I don't even know how to print the index without something like r+1.
What would that look like?


It would look like a normal variable assignment. I can't really give an example without giving away the entire solution.

It's really very simple. You know how to create variables right? And assign them? That's all this is.

1) You already have a variable to keep track of the highest temperature (the variable is "largest"). Create another variable that keeps track of which index had the highest temperature.

2) Set that new variable the same way you set "largest" whenever you find a higher temperature. Only instead of setting it to the temperature, you'd set it to the index.

3) Either print that new variable or return it -- whatever your homework wants (I would think returning it makes more sense, especially given the name of the function.)



EDIT:

Actually, you already have step 1 complete. "highIndex" looks to be what you want.
Last edited on
I understand you don't want to just give me the answer, that's fine. Could you give me an example cout statement that prints any index in an array?
Last edited on
Just remember, if you have just an index it is very easy to get the value (array[index]), but when you have just a value you can't just get the index.

Hint: what do you think temperatures[highIndex][...] should give you if used in the for-loop?
Last edited on
I figured it out. Thanks for the help guys.
Topic archived. No new replies allowed.