# a program on arrays. need some help.

• 10-14-2010
ashlee
a program on arrays. need some help.
Quote:

#include <stdio.h>
main()
{
int i;
int x[16] = { 80, 75, 72, 65, 95, 79, 88, 82, 70, 75, 88, 95, 98, 56, 81, 87};
float sum = 0, count = 0;
for (i = 0; i < 16; ++i){
if (x[i]%2 == 1)
x[i] = 0;
sum += (float)x[i];
}
for (i = 0; i < 16; ++i) {
if (x[i] > 0){
printf("x[%i] = %i\n", i, x[i]);
++count;
}
}
printf("what = %.1f\n", sum/count);
}
the output being:

Quote:

i = 0, x[0] = 80
sum = 0 + 80 = 80
i = 1, x[1] = 75
75%2 = 1
x[1] = 0
sum = 80 + 0 = 80
i = 2, x[2] = 72
sum = 80 + 72 = 152
i = 3, x[3] = 65
x[3] = 0
sum = 152 + 0 = 152
i = 4, x[4] = 95
x[4] = 0
sum = 152 + 0 = 152
i'm just trying hard to make sense of this program here.
so this
Quote:

for (i = 0; i < 16; ++i){
if (x[i]%2 == 1)
x[i] = 0;
is going to loop each array and check if the remainder is 1. if so then the array = 0?
and then that would be added to the number in that array? (0, or that number would be added to the next number if not 0?)
Quote:

sum += (float)x[i];
and is this just checking if the number is positive?
Quote:

for (i = 0; i < 16; ++i) {
if (x[i] > 0){
printf("x[%i] = %i\n", i, x[i]);
++count;
}
sorry, quite new to arrays so i'm trying hard to make sense of this. some explanation or guidance would be most appreciated.
• 10-14-2010
PLEASE put code in CODE tags, not QUOTE tags. Quote tags force the code, all the way to the left side of the page, making it a pita to study.

What are you trying to do here? If you just want to "understand" the program, then just run it, with a bunch of different input values, and also watch the values as you step through the code with a debugger.

That last bit of code is counting each positive number, and showing their index number and value, to you.

And Welcome to the forum, Ashlee! :cool:
• 10-15-2010
ashlee
x[i] = 0; thats what is confusing me.. why exactly is that there? x[i] = 0; i is 0 there for setting x[0] to 0?
• 10-15-2010
claudiu
x[i] = 0 happens only when x[i]%2 == 1 for i between 0 and 15. Think about what that means...
• 10-15-2010
laserlight
If you look at the later part of the program:
Code:

`if (x[i] > 0){`
So, the idea here is to set the current element to 0 so that it will not be printed later.
• 10-15-2010
Your program is essentially adding up even integers. It's taking the odd integers and setting them to zero. Then it prints out all the array indices and the values of the numbers greater than zero in that array, meaning you will only find even integers in there. Then finally you print out the average of all the numbers that were greater than zero.

I deduced that from your if(x[i]%2 == 1) { x[i] = 0; } stuff. Clearly x mod 2 is 1 only if x is odd.
• 10-15-2010
ashlee
okay so it's starting to make sense here. the index is being added by one like a normal array is.. so if the numbers were 4,8,10,11,13 it would add the totals of each even number and then each odd number would just be +0? so 4+0 = 4 then the second array would be 12 ..22.. 22...22?

and you said that this last print out gives the average? printf("what = %.1f\n", sum/count);

how exactly does that work?

and this

sum += (float)x[i]; just converts whats in the array to float? what exactly is the point of sum? is that just zero the whole time? and count?

this is what my compiler gives me when i run this. sorta different
http://img713.imageshack.us/img713/2384/42086782.gif
• 10-15-2010
printf will substitute an expression for the format specifier in the " " part of the printf line of code.

In this case, it will substitute sum/count for the %f. Obviously, sum is not staying zero the whole time, since 0/any number is nonsense.

To find an average, you need two things: 1) The sum of the quantities you're averaging, and 2) The number of items that you are putting into the sum.

If you play with that code a bit, you'll sort it out. :D
• 10-16-2010
ashlee
sooo we're getting the sum here?
sum += (float)x[i]

and what exactly is %.1f?
• 10-16-2010
%=a substitution token
. = a decimal point
1 = format specifier indicating that 1 digit after the decimal only, should be printed
f = data type identifier for float.

Note: a digit before the decimal point indicates the desired width of the field that the number will be printed into.
• 10-16-2010
ashlee
i knew that!

thanks. we're getting there
• 10-16-2010
Quote:

Originally Posted by ashlee
okay so it's starting to make sense here. the index is being added by one like a normal array is.. so if the numbers were 4,8,10,11,13 it would add the totals of each even number and then each odd number would just be +0? so 4+0 = 4 then the second array would be 12 ..22.. 22...22?

and you said that this last print out gives the average? printf("what = %.1f\n", sum/count);

how exactly does that work?

and this

sum += (float)x[i]; just converts whats in the array to float? what exactly is the point of sum? is that just zero the whole time? and count?

this is what my compiler gives me when i run this. sorta different
http://img713.imageshack.us/img713/2384/42086782.gif

I will get the same thing when I run it. We're all a bit confused here because this program does a little bit more than just computing the average of some even numbers. It also destroys the contents of all the odd integers. Is that what you wanted? By slightly altering your program, I can print out all the integers. Is this what you wanted?

Code:

```x[0] = 80 x[1] = 0 x[2] = 72 x[3] = 0 x[4] = 0 x[5] = 0 x[6] = 88 x[7] = 82 x[8] = 70 x[9] = 0 x[10] = 88 x[11] = 0 x[12] = 98 x[13] = 56 x[14] = 0 x[15] = 0 what = 79.2```