【问题标题】:Finding Min, Max and Median for Survey program查找调查程序的最小值、最大值和中值
【发布时间】:2015-05-25 17:21:59
【问题描述】:

一个初学者 C 程序,用于查找三星 s5 手机的最低、最高和平均评级。评分为 1-10。

我是一个初学者,所以认为这看起来很简单,但我总是得到错误的最小值、最大值和平均值的答案。我认为我所做的尝试是有道理的,但我不明白为什么结果不能正确返回。请帮忙。

//Assignment #1 Structured Programming
//Samsung Galaxy S5 Rating program
#include <stdio.h>
#include <conio.h>
#define SIZE 3

//Function to calculate average rating

int mean(const int rating[]);
int max(const int rating[]);
int min(const int rating[]);


int main (void)
{
    int response[SIZE];
    int count;

    for (count=0;count<SIZE;count++)
    {
        printf ("Please enter your rating for the Samsung Galaxy S5 %d\n",count);
        scanf ("%d",&response[count]);
    }
    for (count=0;count<=SIZE;count++)
    {
      printf("%d\n",response[count]);
    }

    mean(response);
    max(response);
    min(response);

    getch();
    return 0;
}

int mean(const int rating[])
{
    int average;
    int x;//counter for totalling array elements
    int total=0;//variable to hould the sum of the array elements

    for (x=0;x<SIZE;x++)
    {
         total+=rating[x];
    }
    average=total/SIZE;
    printf ("The average rating for Samsung Galaxy S5 is %d\n",average);
}
int max(const int rating[])
{
    int max;
    int x;

    for (x=0;x<=SIZE;x++)
    {
        if (rating[x]>rating[x+1])
        {
            max=rating[x];
        }        
    }
    printf ("Max rating for Samsung Galaxy S5 is %d\n",max);

}
int min(const int rating[])
{
    int min;
    int x;

    for (x=0;x<=SIZE;x++)
    {
        if (rating[x]<rating[x+1])
        {
            min=rating[x];
        }        
    }
    printf ("Min rating for Samsung Galaxy S5 is %d\n",min);
}

【问题讨论】:

  • 请下次完美缩进您的代码,以便更好地理解

标签: c


【解决方案1】:

这是正确的编写程序

//Samsung Galaxy S5 Rating program
#include <stdio.h>

#define SIZE 3

//Function to calculate average rating


void get_mean(const int rating[])
{
    float average;
    int x;//counter for totalling array elements
    int total=0;//variable to hould the sum of the array elements

    for (x=0;x<SIZE;x++)
    {
        total+=rating[x];
    }
    average=(float)total/SIZE;
    printf ("The average rating for Samsung Galaxy S5 is %.2f\n",average);
}

void get_max(int rating[])
{
    int maximal = rating[0];
    int x;

    for (x=0; x<SIZE; x++)
    {
        if (rating[x]>maximal)
        {
            maximal = rating[x];
        }
    }
    printf ("Max rating for Samsung Galaxy S5 is %d\n",maximal);

}

void get_min(int rating[])
{
    int minimal=rating[0];
    int x;

    for (x=0;x<SIZE;x++)
    {
        if (rating[x]<minimal)
        {
            minimal=rating[x];
        }
    }
    printf ("Min rating for Samsung Galaxy S5 is %d\n",minimal);

}

int main (void)
{
    int response[SIZE];
    int count;


    for (count = 0; count < SIZE; count++)
    {
        printf ("Please enter your rating for the Samsung Galaxy S5 %d\n",count+1);
        scanf ("%d",&response[count]);
    }
    for (count = 0; count < SIZE; count++)
    {
        printf("%d\n",response[count]);
    }

    get_mean(response);
    get_max(response);
    get_min(response);

    return 0;
}

【讨论】:

    【解决方案2】:

    你的错误对于新手来说很常见。您的问题在于 for 语句和初始化变量。有时循环迭代 4 次,有时 3 次。

    在循环中你需要记住一些事情-

    • 当您创建 for(i=0;i&lt;=3;i++) 时,循环实际上会迭代 4 次
    • 在声明数组时,您声明了 response[SIZE],它是一个 3 的数组,但您迭代的循环有时大于 3
    • max()min()的逻辑不正确

    更改行在行尾标记为已更改

    #include <stdio.h>
    #define SIZE 3
    
    //Function to calculate average rating
    
    int mean(const int []);
    int max(const int []);
    int min(const int []);
    
    
    int main (void)
    {
        int response[SIZE+1];//changed
        int count;
    
    
        for (count=0;count<=SIZE;count++)//changed
        {
            printf ("Please enter your rating for the Samsung Galaxy S5 %d\n",count);
            scanf ("%d",&response[count]);
        }
        for (count=0;count<=SIZE;count++)
            printf("%d\n",response[count]);
    
        mean(response);
        max(response);
        min(response);
        return 0;
    }
    
    int mean(const int rating[])
    {
        int average=0;//changed
        int x;//counter for totalling array elements
        int total=0;//variable to hould the sum of the array elements
    
        for (x=0;x<=SIZE;x++)//changed
             total+=rating[x];
        average=total/(SIZE+1); //changed
        printf ("The average rating for Samsung Galaxy S5 is %d\n",average);
    }
    
    int max(const int rating[])
    {
        int max=rating[0];//changed
        int x;
    
        for (x=0;x<=SIZE;x++)
        {
            if (rating[x]>max)//changed
                max=rating[x];
        }
        printf ("Max rating for Samsung Galaxy S5 is %d\n",max);
    }
    
    int min(const int rating[])
    {
        int min=rating[0];//changed
        int x;
    
        for (x=0;x<=SIZE;x++)
        {
        if (rating[x]<min)//changed
            min=rating[x];     
        }
        printf ("Min rating for Samsung Galaxy S5 is %d\n",min);
    }
    

    【讨论】:

    • 完美运行。和非常简单的解释。谢谢。
    • 下次通过适当的缩进整齐地编写代码:) @RaphaelJones
    • 我试过了。对不起。不确定什么是“整齐”或“适当的缩进”。
    • Indentation 意味着当你在里面写代码时使用制表符,你会看到代码到处都是写的方式,它们在行前跟随制表符或额外的空格,以便于调试和理解,简洁的意思是,不要使用不必要的输入或换行。你明白? :) @RaphaelJones
    猜你喜欢
    • 2012-09-16
    • 2015-12-24
    • 2017-04-20
    • 2013-11-12
    • 2019-02-25
    • 2014-12-24
    • 2021-09-15
    • 2014-06-17
    • 2013-09-28
    相关资源
    最近更新 更多