为调查计划寻找最小,最大和中位数

时间:2015-05-25 17:21:59

标签: c

初学者C程序,可查找三星s5电话的最低,最高和平均评分。评分是1-10。

我是一个初学者,所以认为这看起来非常简单,但我总是得到Min,Max和Mean的错误答案。我认为我的尝试是有道理的,但我不明白为什么结果没有正确返回。请帮忙。

//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);
}

2 个答案:

答案 0 :(得分:3)

这是正确的书面程序

//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;
}

答案 1 :(得分:2)

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

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

  • 当您public interface IMiddleware { // No syntax for this Task Invoke(... any); } // No syntax for this either, unless you know in advance what the parameters will be await serviceProvider.InjectAndInvoke(middleware.Invoke); 时,循环实际迭代4次
  • 在声明数组时,你声明了响应[SIZE],它是一个3的数组,但是你有时候迭代循环大于3
  • for(i=0;i<=3;i++)max()的逻辑不正确

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

min()
相关问题