在2D数组中排列Char

时间:2015-09-18 07:36:06

标签: c arrays formatting char

我的问题与学生的分数排在一起。它以适当的间距打印出分数,但是当它到达if语句以指定字母等级时,它会像ABC一样打印它们而不是以列为中心。任何帮助,将不胜感激。问题出在calc_grades函数if语句中。我无法弄清楚如何让成绩与列

对齐
#include <stdio.h>
#include <string.h>

int numStus;
int numGrades;

void get_students(char stuNames[numStus][10]) //Get students names
{
  int i;
  int j;
    for (i=0; i<numStus; i++)
    {
        printf("Enter the students name.");
        scanf("%6s", &stuNames[i]);
    }

}
void get_grades(int grades[numGrades][numStus], char stuNames[numStus]  [10])//enter each students grades
  {
  int x;
  int z;
    for (x=0; x < numGrades; x++)
    {
    for (z = 0; z < numStus; z++)
    {
      printf("Enter score for %s assignment %d:  ", stuNames[z],x);
      scanf("%d", &grades[x][z]);
    }
  }
}

void calc_grades(int grades[numGrades][numStus])
{
  int sum = 0;
  int sum2 = 0;
  int j;
  int k;
    for(j = 0; j < numStus; j++)
    {
    for(k = 0; k < numGrades; k++)
    {
      sum= sum + grades[k][j];
      sum2 = sum / numGrades;
    }
    if (sum2 >= 90)
    {
      printf("A   ");
    }
    if (sum2 < 90 && sum2 >= 80)
    {
      printf("B   ");
    }
    if (sum2 < 80 && sum2 >= 70)
    {
      printf("C   ");
    }
    if (sum2 < 70 && sum2 >= 60)
    {
      printf("D   ");
    }
    if (sum2 < 60)
    {
      printf("F   ");
    }
    sum = 0;
  }
}
void print_report(int grades[numGrades][numStus], char stuNames[numStus][10])
{
  int i;
  int j;
  int k;
    for(i = 0; i < numStus; i++)
    {
      printf("%6s",stuNames[i]);

    }
  printf("\n");
  for(j = 0; j < numGrades; j++)
{
    for(k = 0; k < numStus; k++)
    {
      printf("%6i",grades[j][k] );
    }
    printf("\n");
}

}

int main()
{
  printf("How many students?");
  scanf("%d", &numStus);
  printf("How many assignments?");
  scanf("%d", &numGrades);

//Ask how many students

//Ask how many assignments

//Initializations...
  char stuNames[numStus][10];
  int grades[numGrades][numStus];
  char finalGrades[numStus];

//Get Student Names
  get_students(stuNames);
  get_grades(grades, stuNames);
  print_report(grades, stuNames);
  calc_grades(grades);
  int x;
  int z;


//Get Grades

//Calculate Final Grades

//Print Report

return 0;
}

1 个答案:

答案 0 :(得分:3)

问题是如何在if语句中打印出成绩。查看你的print_report函数,你的格式是正确的,那么为什么不以同样的方式保持格式呢?

你有什么:

if (sum2 >= 90)
    {
    printf("A   ");
    }

应该是什么:

if (sum2 >= 90)
    {
    printf("%6c", 'A');
    }