这个平均值不对啊
#include
#define SIZE 10
#define PAR 72
int main(void)
{
int index, score[SIZE];
int sum = 0;
float average;
printf("enter %d golf scores :\n", SIZE);
for (index = 0; index < SIZE; index++)
scanf_s("%d", & score[index]);
printf("the scores read in are as fallows:\n");
for (index = 0; index < SIZE; index++)
printf("%5d",score[index]);
printf("\n");
sum += score[index];
average = (float)sum / SIZE;
printf("sum of average=%.2f scores=%d ", average, score);
printf("that's a handicap of %.02f.\n", average - PAR);
return 0;
}
运行结果
一个警告严重性 代码 说明 项目 文件 行 禁止显示状态
警告 C6273 调用 "printf" 实际类型: "int [10]" 时需要整数时,非整数传递为 Param(3)。 Project3 C:\Users\JIASHIJIE\Desktop\hello word\Project3\Project3\源.cpp 28
sum += score[index];
average = (float)sum / SIZE;
sum你得用循环计算总和啊
代码运行到你这行的时候,由于上面循环结束,这时候index的值是10,本身score[index]就越界访问,因此score[10]实际是个垃圾值,自然除以SIZE后还是垃圾值了
#include <stdio.h>
#define SIZE 10
#define PAR 72
int main(void)
{
int index, score[SIZE];
int sum = 0;
float average;
printf("enter %d golf scores :\n", SIZE);
for (index = 0; index < SIZE; index++)
{
scanf_s("%d", & score[index]);
sum += score[index]; //放到输入循环里,直接统计总分就好了
}
average = (float)sum / SIZE;
printf("the scores read in are as fallows:\n");
for (index = 0; index < SIZE; index++)
printf("%5d",score[index]);
printf("\n");
printf("sum of average=%.2f scores=%d ", average, sum); //输出scores应该是总成绩吧,变量是sum,不是score数组地址啊
printf("that's a handicap of %.02f.\n", average - PAR);
return 0;
}
sum要放到循环中
for (index = 0; index < SIZE; index++){
sum += score[index];
printf("%5d",score[index]);
}
printf("\n");
average = (float) sum / SIZE;
printf("sum of average=%.2lf scores=%d ", average, score);
你的scanf_s("%d", & score[index]);错了吧,就是scanf(),把_s去掉