Average, max, and min program in C

前端 未结 8 685
梦毁少年i
梦毁少年i 2021-01-21 02:55

So I\'m coding in C, and I need to come up with code that will take n numbers from the user, and find their minimum, maximum, average, and sum of squares for for their values. S

相关标签:
8条回答
  • 2021-01-21 03:16
    int marks , marks_count=0 , max=0 , min=100 , marks_number=1;
    float total , avg;
    
    printf("Hit enter to input marks of 10 student.\n\n");
    getchar();
    
    do
    {
        printf("Input %d Mark : " , marks_number);
        scanf("%d" ,& marks);
    
    
        if (marks>max)
        {
            max=marks;
        }
    
        else if (marks<min)
        {
            min=marks;
        }
    
        marks_count++;
        marks_number++;
    
        total=total+marks;
    }
    
    while (marks_count<10);
    while (marks_number<10);
    
    
    avg=total/marks_count;
    
    printf("\n\nAverage marks are : %.2f\n" , avg);
    printf("Maximum marks are : %d\n" , max);
    printf("Minimum marks are : %d\n\n\n" , min);
    
    0 讨论(0)
  • 2021-01-21 03:17

    Add a boolean, moved giving the values min, max 0 are the start of loop

    #include<stdio.h>
    int main()
    {
        float average;
        int i, n, count=0, sum=0, squaresum=0, num, min, max;
    
        bool first = true;
        printf("Please enter the number of numbers you wish to evaluate\n");
        scanf_s("%d",&n);
    
        printf("Please enter %d numbers\n",n);
            min=0;
            max=0;
    
        while(count<n)
        {
                scanf_s("%d",&num);
    
            if (first) {
               first = false;
               min = max = num;
            }
                if(num>max)
                   max=num;
                if(num<min)
                   min=num;
    
    
            sum = sum+num;
            squaresum = squaresum + (num*num);
    
            count++;
        }
            average = 1.0*sum/n;
    
        printf("Your average is %.2f\n",average);
        printf("The sum of your squares is %d\n",squaresum);    
    
    
    
        printf("Your maximum number is %d\n",max);
        printf("Your minimum number is %d\n",min);
    
    return(0);
    }
    

    Should also consider to check the return value of scanf

    0 讨论(0)
提交回复
热议问题