The following program is to produce the sum of Even and Odd array element. The program works properly when I use int instead of double. But getting errors when using double. Could anyone tell me what is the problem? Cus I wan it by using double array.
and I don't understand this step too >>
for (i = 0;i <= 10;i++)
{
a = i;
}
what does it mean? THANKS.

#include <stdio.h>

void sum(double a[], int n);
int main()
{
	int i, n = 10;
	double a[11];
	for (i = 0;i <= 10;i++)
	{
		a[i] = i;
	}
	sum(a, n);
}
void sum(double a[], int n)
{
	int i;
	int esum = 0, osum = 0;
	for (i = 0;i <= n;i++)
	{
		if (a[i] % 2 == 0)
			esum = esum + a[i];
		else
			osum = osum + a[i];
	}
	printf("Sum of Even %d\n", esum);
	printf("Sum of Odd %d\n", osum);
}

and I don't understand this step too:

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

what does it mean? THANKS.

It initializes the array with the values 1-10.
It's a loop. Every time the loop loops, one element of the array will receive a value. So the first pass i == 0, so a[0] will become 0. The second pass i == 1 so a[1] will become 1. etc etc.

But getting errors when using double

Change this line : if (a[i] % 2 == 0) to if ((int)a[i] % 2 == 0). This will trim all the numbers behind the decimal point, and it should work.

Thank u very much for quick response and good answer.
But one more thing I just found out
when I changed
if (a % 2 == 0) to if (i% 2 == 0) It compiled too. So what is the difference
if (i% 2 == 0) and if ((int)a % 2 == 0)
Thank u.

because 'i' is already an declared as an int. 'A' was declared as an array of doubles.

Now in your particular case i and a have the same value because of the initializing for() loop you asked about in your previous post.

If you gave the array some random values, your method wouldn't work anymore.

Here's a tutorial on loops, and here's one on arrays

Thanks very much. Now i got it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.