i cannot seem figure out why not getting correct values large , small. supposed find range of n numbers given, have set large , small before loop?
here code:
#include <stdio.h> int main() { //declaring variables int i, number, sum = 0, value, large, small; double avg; //user input printf("enter number of elements want compute.\n"); scanf("%d", &number); printf("enter %d values.\n", number); //loop grab values for(i=0;i<number;i++) { scanf("%d", &value); large = value; small = value; if(value > large) large = value; if(value < small) small = value; //sum calculation sum = sum + value; //average calculation avg = (((double)sum)/number); } printf("the sum of %d numbers is: %d\n", number,sum); printf("the avg of %d numbers is: %.2f\n", number, avg); printf("the largest number is: %d\n", large); printf("the smallest number is: %d\n", small); return 0; }
large = value; small = value; // point if(value > large) large = value; if(value < small) small = value;
at point above, large
, small
, , value
contain same value. subsequent if
statements won't ever take effect. wanted first 2 lines when i == 0
.
Comments
Post a Comment