A small example of an array pointer in C

  • 2020-04-02 01:09:07
  • OfStack

1. Function: input the scores of 5 courses of 6 students, and calculate the average score of each student and the average score of each course.
2, C language implementation code :(in fact, is to use two-dimensional array to achieve, two-dimensional array reference transfer using array pointer to complete)

#include <stdio.h>
#define STUDENT 5
#define SCORE 6
void input_array(float (*score)[STUDENT]);
void avg_score(float (*score)[STUDENT]);
void avg_course(float (*score)[STUDENT]);

int main(){
    float a[SCORE][STUDENT];
    input_array(a);
    avg_course(a);
    avg_score(a);
}
void input_array(float (*score)[STUDENT]){
    int i, j;
    for(i=0; i<SCORE; i++){
        printf("input the %d student score:", i+1);
        for(j=0; j<STUDENT; j++){
            scanf("%f", score[i] + j);
        }
    }
}
void avg_course(float (*score)[STUDENT]){
    int i,j;
    float s;
    for(j=0; j<STUDENT; j++){
        printf("course%d  ", j);
    }
    printf("n");
    for(i=0; i<SCORE; i++){
        s=0;
        for(j=0; j<STUDENT; j++){
            printf("%f  ", *(score[i] + j));
            s += *(score[i] + j);
        }
        printf("ts=%f, avg=%fn" , s,s/STUDENT);
    }
}
void avg_score(float (*score)[STUDENT]){
    int i,j;
    float s;
    for(i=0; i<STUDENT; i++){
        s = 0;
        for(j=0; j<SCORE;j++){
            s+= *(score[j]+i);
        }
        printf("%f  " , s/SCORE);
    }
}

Related articles: