gpt4 book ai didi

c - 打印数组结果

转载 作者:行者123 更新时间:2023-11-30 15:21:38 25 4
gpt4 key购买 nike

我正在为学校编写一个程序,它从数据文件中获取信息并填充一个数组。然后它会找到最高值、最低值并按列计算数字的平均值。我在弄清楚如何打印结果方面遇到了最大的麻烦。文件中的数据为 6 列 15 行数字。这是到目前为止我的代码

#include <stdio.h>

/*Define maxes*/
#define STUDENTS 40
#define GRADES 5

/*Declare Functions*/
void getData(int ID[], int Scores[][GRADES], int* numStudents);
void calculate();
void printResults();
void calcHiScore();
void calcLoScore();
void calcAverage();


/*main function*/

int main()
{
/*Variable Declaration*/
int ID [STUDENTS];
int Scores [STUDENTS][GRADES];
int Hi [GRADES];
int Lo [GRADES];
double Avg [GRADES];
int numStudents;

/*getData function called*/
int getData()
{
int student_count;
int quiz_count;
FILE* spIn;


spIn = fopen("myfile.dat", "r");
student_count=0;
while (fscanf (spIn, "%d", ID[student_count]) != EOF)
{
for (quiz_count = 0; quiz_count < GRADES; quiz_count++)
{
fscanf (spIn, "%d", Scores[student_count][quiz_count]);
}
}
}

/*next calculate function is called*/
int calculate()
{
int calcHiScore (int Scores[][GRADES], int quiz_count, int numStudents)
{
int result;
int student_count;
result = Scores[0][quiz_count];
for (student_count=1; student_count< numStudents; student_count++)
{
if (Scores[student_count][quiz_count] > result)
result = Scores[student_count][quiz_count];
}
return result;
}
int calcLowScore (int Scores[][GRADES], int quiz_count, int numStudents)
{
int result;
int student_count;

result = Scores[0][quiz_count];
for (student_count = 1; student_count < numStudents; student_count++)
{
if (Scores[student_count][quiz_count]< result)
result = Scores[student_count][quiz_count];
}
return result;
}

/* printResults function called*/

void printResults(int arr[STUDENTS][GRADES])
{
int value[10];
0 int ind;
int r,c;

printf("Student\tQuiz1\tQuiz2\tQuiz3\tQuiz4\tQuiz5\n");


/* Print score of all the students in the table form using for loop.*/

for(r=0;r<15; r++)
{
for(c=0;c<6;c++)
printf("%d\t",arr[r][c]);
printf("\n");
}
/* Print the highest, lowest and everage score in each quiz*/

for(ind=0;ind<3;ind++)
{
int v;
if(ind==0)
{

printf("\nHigh score of quiz\n");
printf("High\t");
}

if(ind==1)

{
printf("\nLow score of quiz\n");
printf("Low\t");
}
if(ind==2)
{
printf("\nAverage score of quiz\n");
printf("Avarage\t");
}
/* going through each element of the array*/
for(c=1;c<6;c++)

{
float sum=0;
v=arr[1][c];
for(r=0;r<15;r++)
{
if(ind==0)
/* Finding highest score in each column*/
{
if(arr[r][c]>v)
v=arr[r][c];
}
if(ind==1)
/* Finding lowest score in each column*/
if(arr[r][c]<v)
v=arr[r][c];
if(ind==2)
{
/* Finding average score in each column*/
sum+=arr[r][c];
}
}
if(ind!=2)
/*print highest and lowest scores*/
printf("%d\t",v);
else
/*print average score in each column*/
printf("%2.2f\t", sum/15);
}
printf("\n");
}
}
}
}

该程序可以顺利编译,但无法运行。我完全迷路了,任何帮助将不胜感激。谢谢你!

最佳答案

标准 C 不允许嵌套函数。

此外,这个作业问题/问题与您昨天的帖子相同。

value is neither an array nor a pointer

关于c - 打印数组结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com