C 中的平均值、最大值和最小值程序 [英] Average, max, and min program in C

查看:24
本文介绍了C 中的平均值、最大值和最小值程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用 C 编码,我需要想出一些代码,从用户那里获取 n 个数字,并找到它们的最小值、最大值、平均值和平方和的值.到目前为止,我有平方和的平均值和总和部分,但最小值和最大值正在咬我.

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. So far I have the average and sum of squares portion, but the minimum and maximum is biting me.

请记住,我处于非常初级的水平,还没有达到数组.我只知道逻辑运算符、函数、循环以及 stdlib.h、math.h 和 stdio.h 库的使用.

Keep in mind I'm at a very rudimentary level, and I have not reached arrays yet. All I know are logical operators, functions, loops, and the use of the stdlib.h, math.h, and stdio.h libraries.

这是我目前所拥有的.当我在编译期间尝试放置 float 和 double 时,平均函数给我带来了很多问题,因此将其乘以 1.0 修复了该问题.我拥有一切,只有最低限度和最高限度.我一直把最后一个条目作为我的最大值,0 代表我的最小值.

This is what I have so far. The average function gave me a lot of problems when I tried to put float and double during compiling, so multiply it by a 1.0 fixed that. I have everything, just the minimum and maximum. I keep getting the last entry as my maximum, and a 0 for my minimum.

#include<stdio.h>
int main()
{
    float average;
    int i, n, count=0, sum=0, squaresum=0, num, min, max;


    printf("Please enter the number of numbers you wish to evaluate\n");
    scanf_s("%d",&n);

    printf("Please enter %d numbers\n",n);

    while(count<n)
    {
        min=0;
        max=0;

            if(num>max)
               max=num;
            if(num<min)
               min=num;

            scanf_s("%d",&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);
}

推荐答案

你的算法不太对.下面是正确的实现:

Your algorithm is not quite right. Below is the correct implementation:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(void)
{
    float average;
    int n, num, count = 0, sum = 0, squaresum = 0;
    int min = INT_MAX, max = INT_MIN;
    bool gotAnswer = false;

    /* Don't Let User Enter Wrong Input */
    while(!gotAnswer)
    {
        printf("Please enter the number of numbers you wish to evaluate: ");
        if(scanf_s("%d", &n) != 1)
        {
            /* User Entered Wrong Input; Clean Up stdin Stream*/
            while(getchar() != '\n')
            {
                 continue;
            }
        }
        else
        {
            /* User Input Was Good */
            gotAnswer = true;
        }
    }

    /* Clear stdin Stream Just In Case */
    while(getchar() != '\n')
        continue;

    while(count < n)
    {
        /* Don't Let User Enter Wrong Input */
        gotAnswer = false;
        printf("Enter number %d: ", count + 1);
        if(scanf_s("%d", &num) != 1)
        {
            /* User Entered Wrong Input; Clean Up stdin Stream */
            while(getchar() != '\n')
                continue;

            /* Let User Try Again */
            continue;
        }
        else
        {
            /* User Input Was Correct */
            gotAnswer = true;

            /* Clear stdin Stream Just In Case */
            while(getchar() != '\n')
                continue;
        }

        if(num > max)
            max = num;
        if(num < min)
            min = num;

        sum += num;
        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);

    system("pause");
    return 0;
}

我添加了错误检查和恢复.如果您对逻辑有任何疑问,请提问.

I've added error checking and recovery. Please ask if you have any questions about the logic.

这篇关于C 中的平均值、最大值和最小值程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆