Need help with this program! Arrays and functions

Hi, so basically this program just takes an array of user input and sorts the numbers in ascending order, then it passes this array to a function that will display the average of this array. It is a very small array, but I am running into an error in the average function if someone could assist here that would be great. I feel like I am so close to finishing this.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::fixed;
using std::setprecision;

void sortArray();
void displayAverage(int []);

int main()
{
	cout << "Hi there, I will be taking whatever numbers you give me and I will sort them in ascending order and also give you the average!" << endl << endl;
	sortArray();

	return 0;
}	//end of main function



//start of sort function
void sortArray()
{
	int num[5]   = {0, 0, 0, 0, 0};
	int x        = 0;	    //keeps track of subscripts
	int temp     = 0;       //variable used for swapping
	int maxSub   = 4;       //maximum subscript
	int lastSwap = 0;       //indicates position of last swap
	char swap    = 'Y';     //indicates whether a swap was made

	//take input for num[x]
	cout << "Please enter your first number: ";
	cin >> num[0];
	cout << endl;

	for (int x = 1; x < 5; x = x + 1)
	{
		cout << "Next number: ";
		cin >> num[x];
	}


	//keep repeating as long as swaps are made
	while (swap == 'Y')
	{
		swap = 'N';		//assume no swap is neccessary
		x = 0;			//begin comparing the first array element


		//compare adjacent array elements to determine whether a swap is needed
		while (x < maxSub)
		{
			if (num[x] > num[x + 1])
			{
				//a swap is needed
				temp = num[x];
				num[x] = num[x + 1];
				num[x + 1] = temp;
				swap = 'Y';
				lastSwap = x;
			}	//endif
			x += 1;	//increment subscript
		}	//endwhile
		maxSub = lastSwap;	//reset max subscript
	}	//endwhile

	//display sorted array
	for (int x = 0; x < 4; x = x + 1)
		cout << num[x] << endl;
	//end for

	//pass array on to get average
	displayAverage(num);

}	//end of sort function


void displayAverage(int numbers)
{
	int total = 0;
	double average = 0;
	
	//Gets total
	for (int x = 0; x < 5; x += 1)
		total = total + numbers[x];
	//end for

	//calculates and displays average
	average = static_cast<double>(total) / 5.0;
	cout << "Average: " << average << endl;
}	//end of average function
You're passing displayAverage a pointer to an int, but it expects an int. Change what it expects.

Your compiler already said this when it presented you with the error.
Last edited on
What do you mean? "Pointer to an int"? How do I change this?
What do you mean? "Pointer to an int"?


A pointer is a fundamental data type in C++. Definitely read up on them.

See this function; void displayAverage(int numbers) ? It accepts an object of type int, named numbers.

See this function call; displayAverage(num); ? You're passing in num, which is a pointer to an int.

The function accepts an int. You are passing a pointer to an int. num is an array; when you pass an array like that, you're passing a pointer. If you passed num[1] you would be passing an int.

When a function accepts one type as an input parameter, you cannot just try to pass it something else. Sometimes the compiler will make a guess as to what you meant and try to convert it for you; sometimes it will simply give up. In this case, it's giving up.





Ooh I get you now. Ok so I cant pass the whole array here? I can only pass elements of it?
In this context, the array variable num is just syntactic sugar around a pointer to the start of the array, and ways of dereferencing parts of it.

num[x] is identical to *(num+x) which just means "take the pointer num, move along x, and then dereference it".
Last edited on
Ok so how would I achieve what I am trying to accomplish here? If passing a whole array to a function is not possible?

Well, I could declare the array globally?
Omg, ok I am stupid. I figured it out. Thank you for all your help though! Took me a good few hours to just figure out my argument was wrong :/
Topic archived. No new replies allowed.