Passing dynamic array into a function

Hello,
I am a complete noob in c++ and I am trying to work out this problem:
I need to get the user to input the number of elements of a dynamically allocated array and then return an average of the numbers.
I have tried to do the following:

#include <iostream>
using namespace std;

double avg(double r, int n);

int main()
{
int n;
cout << "Input the size of array:\t"; cin >> n;
double *r; r = new double[n];
for (int i=0;i<n;i++) {cout << "[" <<i<<"]:";cin >> r[i];}
cout << "Average is:\t"<< avg(r[n],n)<<endl;

delete[]r;
return 0;

}

double avg(double r, int n){

double sum = 0;

for (int i=0;i<n;i++){
sum = sum + *r[i]; // Error "subscript requires array or pointer type" given here
}
return (sum/n);
}




This, however, does not work and I get the error "subscript requires array or pointer type".

Any advice would be greatly appreciated!
Pass the pointer itself to the function ( declare the function to take a pointer parameter )
Even if you declared the function parameter 'r' as a pointer *r[i] will give you an error ( because you wouldn't need the * )
I guess you need to read a bit about pointers: http://www.cplusplus.com/doc/tutorial/pointers/

And please use [code][/code] tags
you don't pass an array to avg. It should be:
1
2
3
4
5
6
7
8
9
double avg(double r[], int n){ // note the []

double sum = 0;

for (int i=0;i<n;i++){
sum = sum + r[i]; // note: no *
}
return (sum/n);
}
Thanks for the reply,
I have tried doing something like that but still couldnt get it to work. I changed the function parameter to a pointer, but then, how do I actually pass the arguments into the function?


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

#include <iostream>
using namespace std;

double avg(double *r, int n);

int main()
{
int n;
cout << "Input the size of array:\t"; cin >> n;	
double *r; r = new double[n];
for (int i=0;i<n;i++) {cout << "[" <<i<<"]:";cin >> r[i];}
cout << "Average is:\t"<< avg(*r[n],n)<<endl; // error given here: cannot convert parameter 1 from 'double' to 'double *'

delete[]r;
return 0;

}

double avg(double *r, int n){

	double sum = 0;

	for (int i=0;i<n;i++){
	sum = sum + r[i];
	}
	return (sum/n);
}




The error I get
Tried doing this aswell:


 
cout << "Average is:\t"<< avg(*r[n],n)<<endl;


1
2
3
4
5
6
7
8
9
10
11
double avg(double r[], int n){

	double sum = 0;

	for (int i=0;i<n;i++){
	sum = sum + r[i];
	}
	return (sum/n);
}



But to no avail, the line where I pass parameters into the function still gives an error "cannot convert parameter 1 from 'double' to 'double []'"
Got it working!

the trick was to get rid of [] and * when passing parameters to the function.
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
#include <iostream>
using namespace std;

double avg(double *r, int n);

int main()
{
int n;
cout << "Input the size of array:\t"; cin >> n;	
double *r; r = new double[n];
for (int i=0;i<n;i++) {cout << "[" <<i<<"]:";cin >> r[i];}
cout << "Average is:\t"<< avg(r,n)<<endl; // error given here: cannot convert parameter 1 from 'double' to 'double *'

delete[]r;
return 0;

}

double avg(double *r, int n){

	double sum = 0;

	for (int i=0;i<n;i++){
	sum = sum + r[i];
	}
	return (sum/n);
}


avg expects a pointer to a double.

r is a pointer to a double
r[someValue] is a double
Topic archived. No new replies allowed.