在看 the c primer plus 中与数组有关程序 某部分看不懂 麻烦大家了
// scores_in.c -- uses loops for array processing
#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("%d", &score[index]); // read in the ten scores
printf("The scores read in are as follows:\n");
for (index = 0; index < SIZE; index++)
printf("%5d", score[index]); // verify input
printf("\n");
for (index = 0; index < SIZE; index++)
sum += score[index]; // add them up
average = (float) sum / SIZE; // time-honored method
printf("Sum of scores = %d, average = %.2f\n", sum, average);
printf("That's a handicap of %.0f.\n", average - PAR);
return 0;
}
程序里面的 for (index = 0; index < SIZE; index++)
scanf("%d", &score[index]); // read in the ten scores
这部分看不懂, for 循环的流程我也知道 就是 不知道这个&score[index] 具体含义 而且 那个 index++是 把数组score[index] 加1还是 指的把index 加1
还有score[index] 需要 定义什么类型么 我看 它只吧int score[SIZE];定义了是 写错了 还是 怎么着
谢谢大家了
[解决办法]
&score[index] 是取score[index]的地址,scanf输入时的格式是这样的
index++ 因为是score[index],index加了1之后,自然是score数组指向下一个元素,而不是其值加1.
score[index]是score[SIZE]的一个元素啊,当然是int型。。LZ还需多认真学习啊。
[解决办法]
书名没有the;贴代码看到发帖时编辑区上方的#标志;
1楼说的挺全的,再罗嗦两句。
- C/C++ code
int index, score[SIZE];
[解决办法]
scanf("%d", &score[index]); == scanf("%d", score+index);
就是读取一个整形放入score[index]