c - Sum and get the average of the values in the array -
sum , average of values in array, , stop ask number when sum exceeds 10,000.i need clear example of these conditions.
the language fit need example in c
my code
```
#include <stdio.h> int main() { int array[10]; int i, num, negative_sum = 0, positive_sum = 0; float total = 0.0, average; printf ("enter value of n \n"); scanf("%d", &num); printf("enter %d numbers (negative, positve , zero) \n", num); (i = 0; < num; i++) { scanf("%d", &array[i]); } printf("input array elements \n"); (i = 0; < num; i++) { printf("%+3d\n", array[i]); } (i = 0; < num; i++) { if (array[i] < 0) { negative_sum = negative_sum + array[i]; } else if (array[i] > 0) { positive_sum = positive_sum + array[i]; } else if (array[i] == 0) { ; } total = total + array[i] ; } average = total / num; printf("\n sum of negative numbers = %d\n", negative_sum); printf("sum of positive numbers = %d\n", positive_sum); printf("\n average of input numbers = %.2f\n", average); }
```
answer of question
#include <stdio.h> int main() { int array[10]; int num, negative_sum = 0, positive_sum = 0,j; static int i=0; float total = 0.0, average; label: j=i; printf ("enter value of n \n"); scanf("%d", &num); printf("enter %d numbers (negative, positve , zero) \n", num); (i; < (j+num); i++) { scanf("%d", &array[i]); } i=j; printf("input array elements \n"); (i; < (j+num); i++) { printf("%d\n", array[i]); } i=j; (i; < (j+num); i++) { if (array[i] < 0) { negative_sum = negative_sum + array[i]; } else if (array[i] > 0) { positive_sum = positive_sum + array[i]; } else if (array[i] == 0) { ; } total = total + (float)array[i] ; } average = total / num; printf("\n sum of negative numbers = %d\n", negative_sum); printf("sum of positive numbers = %d\n", positive_sum); printf("\n average of input numbers = %.2f\n", average); if(total<10000) { printf("total less 10000 , total : %.2f\n",total); goto label; }}
it answer of question.program run when total exceed upto 10000.
Comments
Post a Comment