2

该程序应该存储输入的 3 名学生的成绩并打印平均成绩。但是,如果我要求学生 B 的平均值,它会打印学生 A 的平均值。学生 A 的平均值为 0。我似乎找不到哪里出错了。请帮助,例如 Student_A = {7,7, 7},Student_B = {8,8,8};ave(Student_B) = 7

#include<stdio.h>

int i;
char j;
int student_A[4];
int student_B[4];
int student_C[4];
float grade_input(int student[]);
float ave(int student[]);

main(){
    printf("For Student A:\n");
    grade_input(&student_A[4]);
    printf("For Student B:\n");
    grade_input(&student_B[4]);
    printf("For Student C:\n");
    grade_input(&student_C[4]);
    do{
        printf("Whose average grade do you want to see, a ,b ,c?  ");
        getchar();
        scanf("%c", &j);

        if(j=='a'){
            printf("%.2f\n", ave(student_A));
        }
        if(j=='b'){
            printf("%.2f\n", ave(student_B));
        }
        if(j=='c'){
            printf("%.2f\n", ave(student_C));
        }
    }while(j=='a' || j=='b' || j=='c');
}

float grade_input(int student[]){
    int i;
    for(i=0; i<3; i++){
        printf("Enter grade %d: ", i+1);
        scanf("%d", &student[i]);
    }
}

float ave(int student[]){
    return (student[0] + student[1] + student[2])/3.0;
}
4

2 回答 2

1

您遇到的问题是传递&student_X[4]grade_input(). 这完全是非法的,因为它是一个 4 元素数组;未定义访问带有下标 4 的元素。由于它们是连续定义的,因此您最终会student_B在调用grade_input()函数时有效地发送指针,&student_A[4]而这正是您所看到的!

您应该将指针传递给数组。我在下面稍微重写了您的代码以进行说明。

#include<stdio.h>

int i;
char j;
int student_A[4];

/* ******* */
/* note that student_A is defined as a 4 element array */

int student_B[4];
int student_C[4];
float grade_input(int student[]);
float ave(int student[]);

main(){
  printf("For Student A:\n");

  /* ******* */
  /* note that student_A is defined as a 4 element array */
  /* a pointer to the array is just student_A, not &student_A[4] */
  grade_input(student_A);
  printf("For Student B:\n");
  grade_input(student_B);
  printf("For Student C:\n");
  grade_input(student_C);
  do{
    printf("Whose average grade do you want to see, a ,b ,c?  ");
    scanf("%c", &j);

    if(j=='a'){
      printf("%.2f\n", ave(student_A));
    }
    else if(j=='b'){
      printf("%.2f\n", ave(student_B));
    }
    else if(j=='c'){
      printf("%.2f\n", ave(student_C));
    }
    else
      printf ( "Enter a, b or c\n" );

  }while(j != 'q');
}

float grade_input(int student[]){
  int i;
  for(i=0; i<3; i++){
    printf("Enter grade %d: ", i+1);
    scanf("%d", &student[i]);
  }
}

float ave(int student[]){
  return (student[0] + student[1] + student[2])/3.0;
}
于 2013-08-22T11:04:05.617 回答
0

没有必要在 你应该和谁一起清理缓冲区getchar();之前做scanf("%c", &j);fflush(stdout);

char c;
while ((c = getchar()) != '\n');

所以你的完整代码应该是这样的:

#include<stdio.h>

int i;
char j;
int student_A[4];
int student_B[4];
int student_C[4];
float grade_input(int student[]);
float ave(int student[]);

main(){
    printf("For Student A:\n");
    grade_input(&student_A[4]);
    printf("For Student B:\n");
    grade_input(&student_B[4]);
    printf("For Student C:\n");
    grade_input(&student_C[4]);
    do{
        printf("Whose average grade do you want to see, a ,b ,c?  ");
        fflush(stdout); // THE CHANGE
        scanf("%c", &j);

        if(j=='a'){
            printf("%.2f\n", ave(student_A));
        }
        if(j=='b'){
            printf("%.2f\n", ave(student_B));
        }
        if(j=='c'){
            printf("%.2f\n", ave(student_C));
        }
    }while(j=='a' || j=='b' || j=='c');
}

float grade_input(int student[]){
    int i;
    for(i=0; i<3; i++){
        printf("Enter grade %d: ", i+1);
        scanf("%d", &student[i]);
    }
}

float ave(int student[]){
    return (student[0] + student[1] + student[2])/3.0;
}
于 2013-08-22T10:51:27.830 回答