Passing arrays to functions

closed account (Sw07fSEw)
I have a couple questions about passing arrays to functions. I solved this problem sort of by accident, but I'd like to understand what's going on behind the scenes. In the scenario, the user inputs 10 integers into an array and the program finds the largest and smallest values. In int main(), when findLargest() takes tenNum[9] as an argument, is that actually accepting the 9th index of the array, or all of the contents up too the 9th index? Am I getting lucky because the array is global and the findLargest() function can reference all the elements? Is there a more efficient way to do this that a beginner could understand?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <array>
using namespace std;

void findLargest(int);
void findSmallest(int);
int tenNum[10];	// sets length of array to 10

int main(){
	cout << "Please input 10 integers." << endl;
	for(int i = 0; i < 10; i++){
		cout << "Integer " << i + 1 << ": ";
		cin >> tenNum[i];
	}
	
	findLargest(tenNum[9]); // is this taking the whole array up to the 9th index as an argument? or is it specifically taking the 9th index?
	findSmallest(tenNum[9]);
	
	return 0;
}
void findLargest(int){
	int largest = tenNum[0];
	for (int n = 0; n < 10; n++){
		if(tenNum[n] >= largest){
			largest = tenNum[n];
		}
	}
	cout << "The largest number is: " << largest << endl; 
the code on line 16 is only taking the value in the 9th index of the array that is why you are not having a compiler error.

To get the whole array you would need to do something like this:

line 6-7
1
2
void findLargest(int []);
void findSmallest(int []);


And then line 16-17
1
2
findLargest(tenNum);
findSmallest(tenNum);


I hope this helps you.

- Hirokachi
Line 21: This line is incorrect. You need to give the argument a name. Your function works because you're referencing the global array (poor practice). You're not passing the the array in the function call.

line 5-6: These declarations are wrong. You're saying that the two functions accept a single integer, not an array of ints.

Line 16-17: You're passing a single integer to the function, not the array.

findLargest should look like this:
1
2
3
4
5
6
7
8
9
void findLargest(int arr[], int sz)
{   int largest = arr[0];
    for (int n = 0; n < sz; n++)
    {  if (arr[n] >= largest)
        {  largest =arr[n];
        }
    }
    cout << "The largest number is: " << largest << endl; 
}


closed account (Sw07fSEw)
Thanks for your both of your feedback. I think I understand better now. I was uncertain with the scope in the previous example so I resorted to a global array as a shortcut, which I totally thought was poor practice. Thanks for making that concrete. I tried to rewrite the code without using a global array to further my understanding. I think this is more 'sound', is there anything to improve upon?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void findLargest(int []);
void findSmallest(int []);

int main(){
	int tenNum[10];
	cout << "Please input 10 integers." << endl;
	for(int i = 0; i < 10; i++){
		cout << "Integer " << i + 1 << ": ";
		cin >> tenNum[i];
	}
	findLargest(tenNum); 
	findSmallest(tenNum);
	return 0;
}
void findLargest(int localArray[]){ // elements in the local array tenNum[] in int main() are now passed to a new local array called localArray
	int largest = localArray[0];
	for (int n = 0; n < 10; n++){
		if(localArray[n] >= largest){
			largest = localArray[n];
		}
	}
	cout << "The largest number is: " << largest << endl;
}


Follow up question, when the array gets passed to the findLargest function, does the size remain the same (10)? From my understanding, Abstractionanon's new variable sz is there to define the size of the array.
Follow up question, when the array gets passed to the findLargest function, does the size remain the same (10)?

The size of the array in the caller does not change.

In your findLargest function, you're assuming the size of the array is 10. Again a poor practice. By passing the size of the array as an argument as I did, findLargest() will work with any size array. If you change the size of the array in your program, now you don't have to make changes to findLargest.
Topic archived. No new replies allowed.