问题描述
处理一个程序,它将从几个文件中读取数据,将其扫描到数组中,并最终在屏幕上打印13个名称,其中每个数字旁边都有4个数字,并在这些数字之后打上一个字母图表。
Working on a program that will read data from a few files, scan it into arrays, and eventually print to the screen 13 names, with 4 numbers next to each of them, and a letter after those numbers in a kind of grading chart.
然而,我正在使用其中一个函数的问题,其目的是计算平均值。它将一个学生测试的所有分数合并为一个单一值,然后将其除以4以找出平均值,然后将该平均值存储到不同数组的一个元素中。
However I'm having a problem with one of the functions I'm using whose purpose is to calculate the averages. It combines all of the scores for one students tests into a single value and then divides it by 4 to find the average, then stores that average into one element of a different array.
函数调用是:
The function call is:
avg(&scores, &average);
分数和平均值是这样定义的:
Scores and Average are defined like this:
int scores[13][4];
float average[13];
并且使用此循环填充了分数:
对于(j = 0;j≤4; j ++)$ b $(i = 0;i≤13; i ++)
{
)来说,
and scores has been populated using this loop:
for(i=0; i<=13; i++)
{
for(j=0; j<=4; j++)
{
fscanf(score, "%d", &scores[i][j]);
}
}
fclose(score);
仅供参考,所使用的文件打开语句为:
for reference, the file opening statement used is:
FILE *student, *score;
score = fopen("scores.dat", "r");
函数本身如下所示:
The function itself looks like this:
void avg(int *scores, float *average)
{
int total1 = scores[0][0] + scores[0][1] + scores[0][2] + scores[0][3];
int total2 = scores[1][0] + scores[1][1] + scores[1][2] + scores[1][3];
int total3 = scores[2][0] + scores[2][1] + scores[2][2] + scores[2][3];
int total4 = scores[3][0] + scores[3][1] + scores[3][2] + scores[3][3];
int total5 = scores[4][0] + scores[4][1] + scores[4][2] + scores[4][3];
int total6 = scores[5][0] + scores[5][1] + scores[5][2] + scores[5][3];
int total7 = scores[6][0] + scores[6][1] + scores[6][2] + scores[6][3];
int total8 = scores[7][0] + scores[7][1] + scores[7][2] + scores[7][3];
int total9 = scores[8][0] + scores[8][1] + scores[8][2] + scores[8][3];
int total10 = scores[9][0] + scores[9][1] + scores[9][2] + scores[9][3];
int total11 = scores[10][0] + scores[10][1] + scores[10][2] + scores[10][3];
int total12 = scores[11][0] + scores[11][1] + scores[11][2] + scores[11][3];
int total13= scores[12][0] + scores[12][1] + scores[12][2] + scores[12][3];
float avg1 = total1 / 4;
float avg2 = total2 / 4;
float avg3 = total3 / 4;
float avg4 = total4 / 4;
float avg5 = total5 / 4;
float avg6 = total6 / 4;
float avg7 = total7 / 4;
float avg8 = total8 / 4;
float avg9 = total9 / 4;
float avg10 = total10 / 4;
float avg11 = total11 / 4;
float avg12 = total12 / 4;
float avg13 = total13 / 4;
return;
}
这并不完整,我仍然必须告诉函数将avg1-avg13到阵列。但是,一旦我解决了这个错误,我会继续努力。
It's not complete, I still have to tell the function to assign avg1-avg13 to the array. But I'll be working on that once I fix this error.
试图按照原样运行程序给了我很多错误,所有这些错误都基本相同:
Attempting to run the program as is gives me a lot of errors, all of which are basically the same:
ghp11.c: In function 'avg':
ghp11.c:127: error: subscripted value is neither array nor pointer
我不确定如何解决它,以便它正常工作。我试图将4个数组值合并为一个整数值并将其存储在total1中,等等;以便它们可以被平均出来并存储。
I'm not exactly sure how to fix it so that it works properly. I'm trying to combine 4 of the array values into one integer value and store it in total1, etc; so that they can be averaged out and stored.
推荐答案
要将2D数组传递给函数,必须编写函数像这样定义
to pass your 2D array to a function, you have to write the function definition like this
void avg( int score[][4], float *average );
这篇关于下标值既不是数组也不是指针函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!