Hello guys! I am new here and joined recently to show my simple problem because I am having final exam this week.
My problem is whenever I introduce pointers in this array code, a error shows up and I am having trouble understanding this error.
#include <iostream>
usingnamespace std;
int arror(int *arrr[],int *size){
int m=0, sum=0;
double average =0;
while(m<*size){
cout << "ENTER data no." << m+1 << ": ";
cin >> arrr[m];
sum+= arrr[m];
m++;
}
average = sum/ *size;
return average;
}
int main()
{ int n,arr[1000];
cout << "How many data you want to check for this program accuracy? ";
cin >> n;
cout << "Okay it seems that their average is: " << arror(&arr,&n) << endl;
return 0;
}
#include <iostream>
void get_input(int n, int *arr);
double average(int n, constint *arr);
usingnamespace std;
int main()
{ int n,arr[1000];
cout << "How many data you want to check for this program accuracy? ";
cin >> n;
get_input(n, arr);
cout << "Okay it seems that their average is: " << average(n, arr) << endl;
return 0;
}
double average(int n, constint *arr) {
double sum = 0;
for (int i = 0; i < n; ++i) {
sum += arr[i];
}
double average = sum/n;
return average;
}
void get_input(int n, int *arr) {
for (int i = 0; i <n; ++i) {
cout << "ENTER data no." << i+1 << ": ";
cin >> arr[i];
}
}
arr[1000]
there is no need to declare additional memory if you're not going to use it. in this problem you can create the array on the heap so that you allocate just the memory you need: