Help with even and odd integer program
Heres the problem: Write a program that reads integers until 0 is entered. After input terminates, the program should report the total number of even integers (excluding the 0) entered, the average value of the even integers, the total number of odd integers entered, and the average value of the odd integers.
The program stalls at the input loop, and the odd and even integers aren't getting in the array. What am I doing wrong? Any help would be greatly appreciated.
heres my code so far
[CODE]
#include <stdio.h>
int main(void)
{
int index, num2, num[20], nume[20], numo[20];
int sum1, odd = 0;
int sum2, even = 0;
float ave1, ave2;
printf("Enter up to 20 integers:\n");
for (index = 0; num[index] != '0'; index++) //should read input until 0 is entered but doesn't
scanf("%d", &num[index]);
for (index = 0; num[index] != '0'; index++) // this set of nested loops is to find odd and
{ //even integers then putting them into seperate arrays
num2 = num[index];
for(;num2>1;num2-=2);
{
if(num2 == 1)
{
numo[index] = (num2 += 2); //to get the number back to its original value
odd++;
}
else
{
nume[index] = (num2 += 2); //to get the number back to its original value
even++;
}
}
}
for (index = 0; index <= odd; index++) //to find the sum of all the odd integers
sum1 += numo[index];
for (index = 0; index <= even; index++) //to find the sum of all the even integers
sum2 += nume[index];
ave1 = sum1 / odd; //to find the average of the even integers
ave2 = sum2 / even; //to find the average of the odd integers
printf("%d %s even, %d %s odd.\n", even, even == 1 ? "is" : "are", odd, odd == 1 ? "is" : "are");
printf("The average even number is %0.1f, the average odd number is %0.1f.\n", ave2, ave1);
system ("pause");
return 0;
}
[/CODE]
Re: Help with even and odd integer program
I've figured out the opening loop (num[index - 1] != 0) but the arrays for the even/odd ints still doesn't work.
Here's the updated code
[CODE]
#include <stdio.h>
int main(void)
{
int index, num2, num[20], nume[20], numo[20];
int sum1, odd = 0;
int sum2, even = 0;
float ave1, ave2;
printf("Enter up to 20 integers:\n");
printf("Enter 0 to quit\n");
for (index = 0; num[index - 1] != 0; index++) //reads input until 0 is entered
scanf("%d", &num[index]);
for (index = 0; num[index - 1] != 0; index++) // this set of nested loops is to find odd and
{ //even integers then putting them into seperate arrays
num2 = num[index];
for(; num2>1; num2-=2);
{
if(num2 == 1)
{ //to get the number back to its original value
odd++;
numo[odd] = (num2 += 2);
}
else
{ //to get the number back to its original value
even++;
nume[even] = (num2 += 2);
}
}
}
for (index = 0; index <= odd; index++) //to find the sum of all the odd integers
sum1 += numo[index];
for (index = 0; index <= even; index++) //to find the sum of all the even integers
sum2 += nume[index];
ave1 = sum1 / odd; //to find the average of the even integers
ave2 = sum2 / even; //to find the average of the odd integers
printf("%d %s even, %d %s odd.\n", even - 1, even == 1 ? "is" : "are", odd, odd == 1 ? "is" : "are");
printf("The average even number is %0.1f, the average odd number is %0.1f.\n", ave2, ave1);
system ("pause");
return 0;
}
[/CODE]
Here's the output. its counting the even/odd correctly but as you can see the arrays are obviously not correct.
Enter up to 20 integers:
Enter 0 to quit
1 2 3 4 5 6 7 8 9 10 11 0
5 are even, 6 are odd.
The average even number is 1072059.0, the average odd number is 2151805.0.
Press any key to continue . . .
Please any help would be greatly appreciated.
Re: Help with even and odd integer program
I've figured out a much simpler way to do this
[CODE]
#include
int main(void)
{
int num = 1;
int sum1 = 0, odd = 0;
int sum2 = 0, even = 0;
float ave1 = 0, ave2 = 0;
printf("Enter up to some integers for analysis:\n");
printf("Enter 0 to quit\n");
while (num != 0) //reads input until 0 is entered
{
scanf("%d", &num);
if ((num % 2) == 0) // if the div doesnt' have a remander then even else odd
{
if (num != 0) // to stop from adding 0 to the even sum
{
even++;
sum2 += num;
}
}
else
{
odd++;
sum1 += num;
}
}
if (odd != 0) //to find the average of the odd integers
ave1 = sum1 / odd;
if (even != 0) //to find the average of the even integers
ave2 = sum2 / even;
printf("%d %s even, %d %s odd.\n", even, even == 1 ? "is" : "are", odd, odd == 1 ? "is" : "are");
printf("The average even number is %0.1f, the average odd number is %0.1f.\n", ave2, ave1);
system ("pause");
return 0;
}
[/CODE]
Re: Help with even and odd integer program
#include<stdio.h>
main()
{
float eavg=0,oavg=0;
int num[10],i,m=0,n=0,t;
printf("enter numbers\n");
for(i=0;num[i]!=0;i++)
{
scanf("%d",&num[i]);
t=num[i];
if(t%2==0)
{
n++;
eavg+=num[i];
}
else
{
m++;
oavg+=num[i];
}
}
eavg/=n;
oavg/=m;
printf("average of even nums=%f\n",eavg);
printf("average of odd nums=%f\n",oavg);
}