# Thread: help with averagine array function

1. ## help with averagine array function

hi people, i needed to write a program that would ask the user for numbers and store them in an array, entering -1 to stop entering numbers. then the program will use a function to find the average of the numbers in the array. the only thing bugging me is i cant get it to stop when you enter -1. thanks for any help or directions to the right way.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{

int a[1000];
int num;
int total = 0;
int i = 0;

void calc_avg( int[] , int, int, int );

while(i != -1) {
printf( "Enter a rating between 0 and 100 " );
scanf( "%d", &num );
a[i] = num;
}

system("PAUSE");
return 0;
}

void calc_avg( int a[], int num, int total, int answer ) {
int i = 0;

for (i = 0; i < 1000; i++) {
total += a[i];
}

printf ("The average of the numbers was: %d", answer);
}```

2. Code:
```   while(i != -1) {
printf( "Enter a rating between 0 and 100 " );
scanf( "%d", &num );
a[i] = num;
}```
At which point will i be -1?
In fact, when does i change in this loop?

Code:
```      void calc_avg( int a[], int num, int total, int answer ) {
int i = 0;

for (i = 0; i < 1000; i++) {
total += a[i];
}

printf ("The average of the numbers was: %d", answer);
}```
Do you always have 1000 elements?

You certainly don't need to pass a variable "total" or "answer" - declare them inside this function. "num" is not even used, so that could be completely omitted. Naturally, the relevant changes also need to be made in the code above for calling the function.

--
Mats

3. Originally Posted by matsp
Code:
```   while(i != -1) {
printf( "Enter a rating between 0 and 100 " );
scanf( "%d", &num );
a[i] = num;
}```
At which point will i be -1?
In fact, when does i change in this loop?

Code:
```      void calc_avg( int a[], int num, int total, int answer ) {
int i = 0;

for (i = 0; i < 1000; i++) {
total += a[i];
}

printf ("The average of the numbers was: %d", answer);
}```
Do you always have 1000 elements?

You certainly don't need to pass a variable "total" or "answer" - declare them inside this function. "num" is not even used, so that could be completely omitted. Naturally, the relevant changes also need to be made in the code above for calling the function.

--
Mats
Sure there is a big mistake because no matter what you want to do with the arguments in the funtion that calculates the avarage you must declare them as int *answer or int *total and in the function block to say:
*total += a[i];