Create an array of N integers. ask from the user how many element he/she wants to input in the array. Determine the sum of all numbers, the average of all even numbers and count how many are odd numbers and how many zero's were inputed ? at the end, print the accumulated and counted values of the array.
the problem is I dont know how to make a code for how many odd numbers were inputed and I am confused for the line print the accumulated and counted values of the array.
#include<stdio.h>
#include<conio.h>
main()
{
int num[]={};
int max=0,i,sumofzero=0,zero=0,sumodd=0;
float sum=0;
float ave=0;
printf("How many elements you want? : ");
scanf("%d", &max);
for(i=0;i<max;i++)
{
printf("Enter number %d : \n",i );
scanf("%d", &num[i]);
// sum+=num[i];
// ave=(sum/max);
}
for(i=0;i<max;i++)//for even average numbers
{
if(num[i]%2==0)
sum+=num[i];
ave=(sum/max);
}
for(i=0;i<max;i++)//for odd numbers
{
if(num[i]%2!=0)
sumodd+=num[i]+1;
}
for(i=0;i<max;i++)//for zero numbers
{
if(num[i]==0)
sumofzero+=num[i]+1;
}
printf("The sum of all numbers is : %.2f \n", sum);
printf("The average of all even numbers is %.2f \n", ave);
printf("The sum of all odd numbers is : %d \n", sumodd);
printf("The sum of all 0 numbers is : %d \n", sumofzero);
getch();
}
You can't make an array the way you are trying to. You must declare the size at compile time or use dynamic memory allocation, unless you are using C99, which I believe allows you to specify the size with a variable. And it is int main() not simply main().
the program is now ok except for the last statement of the problem that states : at the end, print the accumulated and counted values of the array. <-- i am still confused about this line, and i think that is this what the number of elements I inputed ?.. am I right for this assumption?..