1. ## array help, fast!

i need to write a program that reads a sequence of natural numbers ending with '-1', calculates the average, and prints the firrence between the average and each number, one per line. there are at least 10 numbers, but no more than 40.

e.g

10
20
30
40
40
60
70
80
90
101
-1 (average is 50) gives

40
30
20
10
0
-10
-20
-30
-40
-51
51

i need serious help, i'm majourly stuck

2. Well lets see:
• Create an array
• Sum them up
• Divide by number of elements
• Go through the array and calculate and display the difference
• exit the program

3. the -1 is the problem, how do i get it to stop at -1

• Compare the number against -1
• If its -1 break
• else keep going with the loop

4. this is what i have so far
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 40

int main(void)
{
int a[40];
int i=0;
int c;
int sum=0;

while(1)
{
scanf("%d",&c);
if(c>=0){
a[i] =c;
i++;
}
else if(c==-1){
a[i]=c;
break;}
}

while(a[i]!=-1)
{
sum=a[i]+a[i+1];
i++;
}
sum=sum+-1;
return 0;
}```
correct?

5. Some points:
You defined MAX so use it
first while condition should make sure you don't over run your array (check to make sure i is less then MAX)
after your read you know how many items are in the array
between the first and second loop the value of i doesn't change so it will never go into the second loop
you still aren't calculating the average
you still aren't calculating the difference
you need consistant indentation

6. sorry i'm having rel problems summing the values, here is my code so far:

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

#define MAX 40

int main(void)
{
int a[MAX];
int i=0;
int b=0;
int c;
int d;
int sum=0;

while(i<MAX)
{
scanf("%d",&c);
if(c>=0){
a[i] =c;
i++;
}
else if(c==-1){
a[i]=c;
break;}
}

return 0;
}```
where and how do i sum the values?

7. Since you do seem to be trying (but your indention needs work)
Code:
```int c =0; /* Using c now for indexing */
while (c < i ) /* We really don't want to add the -1 as it just signals the end of data */
{
sum += a[c];
c++;
}```
to calculate average just divide sum by i. The trouble is that they both are integers and an int divided by an int produces an int. So if you want to get a decimal answer you'll need to cast one of them to a float or a double

8. thanks for the help thantos, i was having a stupid moment, ive fixed my problem now