# Thread: arrays, sums and averages

1. ## arrays, sums and averages

Hey, Can i get help too? I have a problem same with this guy, now i got far in coding and learned alot while doing so but i have a slight problem, in those 3 displays (ODD,EVEN,PRIME)
if i input (1,2,3,4,5)
it should display
EVEN:2,4
ODD;1,3,5
PRIME;1,2,3,5

but instead it displays something like this
even;0,2,0,4,0
odd;1,0,3,0,5
prime:1,2,3,0,5

Code:
```#include<stdio.h>
#include<conio.h>
int sum (int);
float average (float,float);
void even (int,int);
void odd (int,int);
void prime (int,int);
void factorial (int,int);
int ehold[20],ohold[20],phold[20],fhold[20],facthold[20],ucnt=0;

main()
{
int N,Ncnt,tsum=0,num;
clrscr();
printf("Enter N numbers: ");
scanf("%d",&N);
for (Ncnt=1;Ncnt<=N;Ncnt++)
{
printf("Value of Number %d - ",Ncnt);
scanf("%d",&num);
tsum=sum(num)+tsum;
even(num,Ncnt);
odd(num,Ncnt);
if ((num%Ncnt)==0)
{
prime(num,Ncnt);
}
factorial(num,Ncnt);
}
printf("\nSum: %d",tsum);
printf("\nAverage: %.1f",average(tsum,N));
printf("\n\nEVEN numbers: ");
for (ucnt=1;ucnt<Ncnt-1;ucnt++)
{
printf("%d,",ehold[ucnt]);
}
printf("\b ");
printf("\n\nODD numbers: ");
for (ucnt=1;ucnt<Ncnt-2;ucnt++)
{
printf("%d,",ohold[ucnt]);
}
printf("\b ");
printf("\n\nPRIME numbers: ");
for (ucnt=1;ucnt<=Ncnt-1;ucnt++)
{
printf("%d,",phold[ucnt]);
}
printf("\b ");
printf("\n\nFactorial:");
for (ucnt=1;ucnt<=Ncnt-1;ucnt++)
{
printf("\n\tValue %d - %d = %d",ucnt,fhold[ucnt],facthold[ucnt]);
}
getch();
return 0;
}

int sum (int a)
{
int sum=0;
return sum=a+sum;
}

float average (float s,float c)
{
float ave;
ave=s/c;
return (ave);
}

void even (int e,int o)
{
if ((e%2)==0)
ehold[o]=e;
}

void odd (int o,int d)
{
if ((o%2)>0)
ohold[d]=o;
}

void prime (int p,int r)
{
int test,cnt,prc=0;
for (cnt=1;cnt<=p;cnt++)
{
if ((p%cnt)==0)
prc++;
}
if (prc<=2)
{
phold[r]=p;
}
}

void factorial (int f,int a)
{
int fact=1,cnt;
for (cnt=1;cnt<=f;cnt++)
{
fact=fact*cnt;
}
fhold[a]=f;
facthold[a]=fact;
}```

even;0,2,0,4,0
odd;1,0,3,0,5
Notice that wherever there is a zero, there is a valid number in the corresponding position in the other array.

A number is either even or odd - it cannot be both.

3. Thanks for moving it sir.

so what do i do with it,
I'll merge the 2 functions in 1?

4. Either
- each array has a separate count of the number of valid entries (ie, not the same global Ncnt
- inside each printing loop, you skip any entry which is zero.

5. how do i skip the zero entry?
i tried
if (hold[ucnt]==0)
ucnt++;

what do i put under the if?? i thinks i have it wrong,,

and how about the prime?? i thinks its easier if i just used a skip?

6. As the first thing in each loop, do something like

if ( ehold[ucnt] == 0 ) continue;

7. It worked like a charm! thanks sir! you're really good. I'll be back soon thanks again sir, you just saved me hours and a whole semester.