# Thread: How do I skip over zero elements in an array?

1. ## How do I skip over zero elements in an array?

I'm initializing an array with a value of [9]. The user can either enter all 10 values or enter a negative number to stop the array. Then I want to average all of the elements in the array, but if the user enters a negative number to stop the array, then it makes the rest of the values in the array zero. How would I code it to make sure that the zero values do not get averaged?

2. Originally Posted by Roon Hapoon
I'm initializing an array with a value of [9]. The user can either enter all 10 values or enter a negative number to stop the array. Then I want to average all of the elements in the array, but if the user enters a negative number to stop the array, then it makes the rest of the values in the array zero. How would I code it to make sure that the zero values do not get averaged?
First of all, if you are creating an array with [9] indexes, then a user can only enter 9 values -- not 10.

Why not store -1 in the index, and then when you average the numbers, don't include any index with a -1.

3. Originally Posted by bithub
Why not store -1 in the index, and then when you average the numbers, don't include any index with a -1.
But how would I do that? With an if statement?

for (x = 0; x < 10; x++)
{
avg = (array[x] + avg) / x;
if (array[x] == -1)

but then what? That's the part I just don't get :\

4. So, loop until the index reaches 10, or the current element is negative.

5. Originally Posted by Roon Hapoon
But how would I do that? With an if statement?

for (x = 0; x < 10; x++)
{
avg = (array[x] + avg) / x;
if (array[x] == -1)

but then what? That's the part I just don't get :\
Use the break statement to exit your loop when you reach a -1.

6. Math Note: Average is the sum of the elements divided by the number of elements.

I have no idea what your code is doing.

Tim S.

7. Originally Posted by Roon Hapoon
But how would I do that? With an if statement?

for (x = 0; x < 10; x++)
{
avg = (array[x] + avg) / x;
if (array[x] == -1)

but then what? That's the part I just don't get :\
I would try something like...
Code:
```int array[10];
int avg ;

// user enters values

avg = 0;
for (int x = 0; x < 10; x++)
{ if (array[x] == -1)
{ avg /= x;
break; }
avg += array[x]; }```

8. I wouldn't calculate a "running" average. Unless you specifically need to keep a running average, don't do it - it's just extra calculation for nothing.

Code:
```for (x = 0; x < 10; x++)
{
avg = (array[x] + avg) / x;
if (array[x] == -1)

and removing the running average and adding a sum:
for(x = 0, sum = 0; x < 10; x++)
{
if(array[x] > -1)
sum = sum + array[x];
else
break;
}
//now calculate the sum, just once:
average = sum/x;```
The standard idiom or practice in C, with arithmetic like: sum = sum + someNumber, is to use the shorthand: sum += someNumber. Don't be surprised if you see C code like that - it's very common. That applies to subtraction, multiplication, and division, as well as addition.