Am trying to write a c program to do the following;
• Write on the screen: ``Please enter a positive number: ''.
• Accept a number from the user.
• Keep prompting for more numbers until -1 is entered
• Output the sum of all entered numbers excluding -1.
• Output the integer part of the average of all entered numbers excluding -1.
• The program should properly check all input provided by the user and fail appropriately
if bad input is given. The user should not be able to induce a segmentation fault or
unintended behaviour from the program
I have written some code but the output seems wrong. here is my code below;
#include <stdio.h>
#include <stdlib.h>
#define LENGHT 10
int main(void) {
int pnumbers[LENGHT] = {0};
int i,j,k, sum = 0, input, count = 0, integers = 0;
float check;
i = 0;
while (i < LENGHT) {
i++;
printf("Please enter a positive number:");
scanf("%d", &input);
if(input == -1)
{ break;}
if(input%2 == 0 || input%2 != 0)
pnumbers[i] = input;
else
break;
}
for(j = 0; j < LENGHT; j++) {
sum = sum + pnumbers[j];
}
printf("\nSum = %d", sum);
for(k = 0; k < LENGHT; k++) {
if(pnumbers[k]%2 == 0 || pnumbers[k]%2 != 0) {
count = count + 1;
integers = integers + pnumbers[k];
}
}
printf("\nAverage of integers = %d", integers/count);
return 0;
}