Arrays and parameters

To find and return the largest index:


1
2
3
4
5
6
7
8
9
int largestIndex(const int list[], int size)
{
    int index;
    int max = 0;
    for(index = 1; index < size; index++)
        if(list[index] > list[max])
            max = index;
    return max;
}



In the if loop, if I say list[max] = list[index] would I still get the same value I am looking for?

i.e. The index and max variables are used to iterate and compare through the array; and index and max can also assign array values? This is confusing me because to print the index and to print the value, the code, respectively:


1
2
3
4
cout<<largestIndex(listA, SIZE);
  // location
cout<<listA[largestIndex(listA, SIZE)];
 // value 


Can soneone please clarify this?
I don't understand your question at all.

and index and max can also assign array values?


This in particular makes no sense to me. Can you clarify your question?
Sorry, Disch. I am trying to figure out why/how/if a variable in a function can assign valuse to array components.
I still don't understand.... sorry.

You can assign any two variables as long as they're the same type.
In the if loop, if I say list[max] = list[index] would I still get the same value I am looking for?


If you do that inside of the loop you would not be setting the max INDEX, you would be changing what the max number would be which would be the current number. so if you changed the loop to this
1
2
3
4
5
6
7
8
9
10
11
int largestIndex(const int list[], int size)
{
    int index;
    int max = 0;
    for(index = 1; index < size; index++)
        if(list[index] > list[max])
            list[max] = list[index];// this part is different
    return max;
}



if you did this you would be returning 0 from the function. Also the max number in the array would now be at position 0, which im sure is not what you want.

your function is perfectly correct based on its title and what it actually does.
also this would give the desired results
1
2
3
4
5
6
7
cout<<largestIndex(listA, SIZE); // returns the index from the function and outputs to screen
  // location
cout<<listA[largestIndex(listA, SIZE)];
 // returns the index from the function and then resolves down to the followning
// listA[returned value from function] which would return the value at that index
// then it would be output to the screen

IM not sure what you need clarifying on because everything you had was correct
Note that there is an STL algorithm to do this: http://www.cplusplus.com/reference/algorithm/max_element/
1
2
3
4
5
6
//let's say this is the array
int listA[] = {1, 2, 3,  4, 5};
cout<<largestIndex(listA, SIZE); //the function should would return 4 since 5 is the largest
  // location
cout<<listA[largestIndex(listA, SIZE)]; //is actually equal to listA[4]
 // value  


is that clear enough?
Thank you everyone. For some reason, trying to learn how to manipulate an array was confusing; I force myself to refer to the number in the subscript operator ( [] ) as a location rather than a variable, think that is where I was getting confused.

So I have this 2 dimensional array that stores the lowest temperature per month in the first column and the highest temperature in the second column. The array is [12][2] and holds 24 integers. To index the highest temp, I have the following function which only iterates once for the second column (that is where all the high temps are stored):


1
2
3
4
5
6
7
8
9
10
11
12
int indexHigh(const int list[][2])
{
	int max = list[0][0];
	for(int col = 1; col < 2; col++)
	{
		for(int row = 0; row < 12; row++)
			if(list[row][col] > max)
				max = list[row][col];
	}
	return max;
}


My problem is that I am trying to print the value, not the index. In a regular array, I would put the function call within the subscript operator:

list[indexHigh(list)]

Can anyone show me proper syntax or where I need to place the function call for what I am trying to accomplish please? I have tried everything I can think of. here is what I am trying to print:


1
2
3
4
5
6
7
void print(const int list[][2], int hTemp, int lTemp)
{
	cout<<"Highest temperature for the year:     "<<endl; // need help here
	cout<<"Lowest temperature for the year:      "<<endl; // need help here
	cout<<"Average high:                         "<<averageHigh(list)<<endl;
	cout<<"Average low:                          "<<averageLow(list)<<endl;
}
Topic archived. No new replies allowed.