I'm trying to write a program which asks the user to insert a series of number to make an array and then, using a function, calculate the sum and the mean of the elements of the array. The problem is that when I use the function in the main the output of sum and mean are both 0. And if I just use arr[n] without & I got other errors, why is that?
Here's the code:
#include <stdio.h>
int sum_and_mean (int arr[], int n);
int i;
int n;
int arr[100];
int main(){
printf("How many numbers do you want to insert?");
scanf("%d", &n);
if((n > 0) && (n <= 20)){
for(i = 0; i < n; i++){
printf("Insert a number:");
scanf("%d", &arr[i]);
}
printf("The array is:\n");
for (i = 0; i < n; i++){
printf("%d\n", arr[i]);
}
}
else {
printf("Error: number must be between 0 and 20");
}
sum_and_mean(&(arr[n]), n);
return 0;
}
int sum_and_mean (int arr[], int n){
double sum = 0;
double mean;
for (i = 0; i < n; i++){
sum += arr[i];
}
printf("The sum of the elements is %lf\n", sum);
mean = sum / n;
printf("The mean of the elements is %lf", mean);
return 0;
}