c - 打印数组结果

标签 c arrays

我正在为学校编写一个程序,它从数据文件中获取信息并填充一个数组。然后它会找到最高值、最低值并按列计算数字的平均值。我在弄清楚如何打印结果方面遇到了最大的麻烦。文件中的数据为 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/

相关文章:

javascript - 为什么此 HTTP 请求返回数组长度而不是内容?

ios - 在 Swift 中创建一个随机数数组

java - JOptionPane 数组条件

c++ - 嵌入式系统中固有、内联、外部的区别?

c++ - 有人可以解释 malloc(20 * c | -(20 * (unsigned __int64)(unsigned int)c >> 32 != 0)) 的含义

c - 执行另一个程序 + child 死亡

c - 从命令行打开的 .txt 文件中读取整数

c - 为什么对象 (ELF) 文件中没有堆栈段?

c - 使用指针查找数组中的最大数字并将其位置转移到另一个数组

PHP 内爆 explode 第一个和最后一个数组值