【发布时间】: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