I have an assigment I\'m trying to complete but I\'m not getting the right resul
ID: 3598421 • Letter: I
Question
I have an assigment I'm trying to complete but I'm not getting the right results with the code I have. I am to write a C program that reads in a list of integers and the prints out the count of the number f integers inputted. This is the code I have so far:
#include <stdio.h>
#include <math.h>
#define SIZE ((int) (sizeof(a) / sizeof(a[0])))
int main(void) {
int a[1000];
int i, count;
for (i = 0; i < 1000; i++) {
scanf("%d", &a[i]);
int length = sizeof(a) / sizeof(a[i]);
i++;
break;
}
printf("The number count is %d", a);
return 0;
}
The user can input any number into the array, my job is to print out the number count and the average of the numbers. I know this is the right approach but I'm doing something wrong. Please help.
Explanation / Answer
Answer:-
#include <stdio.h>
#include <math.h>
int main(void) {
int a[1000];
int i, count;
char c;
for (i = 0; i < 1000; i++)
{
printf("Do you want to Enter a number: (Y/y) or (N/n) ");//asking user to input next integer.
scanf("%c",&c);
if(c=='Y' || c=='y')
{
scanf("%d", &a[i]);
}
else
break;
fflush(stdin);
}
int j;
int sum=0;
for(j=0;j<i;j++){
sum=sum+a[j];
}
printf("The number count is %d and average is : %f ", i,(float)sum/(float)i);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.