2

This program is supposed to store inputted grades of 3 students and prints the average. However, if I ask for the average of student B, it prints the average of student A. and Student A's average is 0. I can't seem to find where I went wrong.. Please help e.g. 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;
}
2
  • Your problem is an out-of-bounds access by referencing element 4 of a 4 element array; see detailed answer below. The issue is not to do with scanf() at all; scanf is doing just fine, you are giving it a bad pointer to use. Commented Aug 22, 2013 at 11:07
  • Concerning a potential input problem that may remain after using the accepted answer: Replace scanf("%c", &j); with scanf(" %c", &j); (An extra space). This will consume any previous \n. Commented Aug 22, 2013 at 11:47

2 Answers 2

1

The problem you have is passing &student_X[4] to grade_input(). This is just straight illegal as it is a 4 element array; accessing an element with subscript 4 is not defined. Since they are defined contiguously, you end up effectively sending the pointer to student_B when you call the grade_input() function with &student_A[4] and that's exactly what you are seeing!

You should instead pass a pointer to the array. I've slightly rewritten your code below to illustrate.

#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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

there is no need to do getchar(); before scanf("%c", &j); whoever you should clean the buffer with fflush(stdout); or do

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

so you're full code should be like:

#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;
}

7 Comments

the problem still exists. :/ if I chose to see student B's grade, it prints student A's ave
@JohnNoelMaape how do you know it's a's ave?
if I unputted 3 7's for A and 3 8's for B, I get 7 when I ask for B's average
@JohnNoelMaape i've tested and got the right result... i really donno what's wrong, are you sure it's not the input? do you put "B" or "b" when you ask for B's avg?
i enter small characters. so i input "b"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.