title : Enter the scores of the M students ' n courses, calculate the average scores of each student, and enter the student number to output the results of each course.
First directly on the source code: (There is the wrong source code)
#include <stdio.h>#include<stdlib.h>#include<stdbool.h>int**pointer_counterpart =NULL;intMain () {voidInput_number (int*m,int*N); //Input m students n course results voidAllocationintMintN); //void display (int **pointer, int m, int n); voidCalculation (int**pointer,intMintN); //Calculate average score voidSearch (int**pointer,intMintN); //Check student results voidFree (int**pointer,intm); intStudents_number, Subjects_number; Input_number (&students_number, &subjects_number); Allocation (Students_number, subjects_number); //display (Pointer_counterpart, Students_number, subjects_number);calculation (Pointer_counterpart, Students_number, Subjects_number); Search (Pointer_counterpart, Students_number, Subjects_number); Free (Pointer_counterpart, students_number); return 0;}voidInput_number (int*m,int*N) {printf ("Please input the numbers of students and subjects:\n"); scanf ("%d%d", M, n);}voidAllocationintMintN) { int*Pointer[m]; for(inti =0; I < m; ++i) {Pointer[i]= (int*)malloc(n *sizeof(int)); } for(inti =0; I < m; ++i) {printf ("Please input%d students%d grades:\n", i +1, N); for(intj =0; J < N; ++j) {scanf ("%d", * (pointer + i) +j); } } /** After the complete run is complete, come back and look here for (int i = 0; i < m; ++i) {Pointer[i] = (int*) malloc (n * sizeof (int)); printf ("Please input%d students%d grades:\n", i + 1, n); for (int j = 0; J < N; ++j) {scanf ("%d", * (pointer + i) + j); } }*/ /*Print to try for (int i = 0; i < m; ++i) {printf ("The%d student ' s grades are:\n", I + 1); for (int j = 0; J < N; ++j) {printf ("%d", * (* (pointer + i) + j)); } printf ("\ n"); }right****/Pointer_counterpart=pointer;}/*void display (int **pointer, int m, int n) {printf ("matrix:\n"); for (int i = 0; i < m; ++i) {for (int j = 0; J < N; ++j) {printf ("%d", * (* (pointer + i) + j)); } printf ("\ n"); }}*/voidCalculation (int**pointer,intMintN) { intaverage; intsum; ///Print to tryprintf ("%p%p\n", Pointer_counterpart, *Pointer_counterpart); printf ("%p%p\n", Pointer, *pointer); ///End for(inti =0; I < m; ++i) {sum=0; Average=0; for(intj =0; J < N; ++j) {Sum+ = * (* (pointer + i) + j);/// number of students = 3, number of subjects = 3The third average here is wrong .} average= SUM/N; printf ("%d students ' average is%d.\n", i +1, average); }}voidSearch (int**pointer,intMintN) { intnum; BOOLCondition =true; while(condition) {printf ("Please input the student ' s number to search for his grades:\n"); scanf ("%d", &num); if(Num <1|| num >m) {CharFlag; printf ("ERROR, Reinput ' y ', exit ' n ':"); scanf ("%c", &flag); if(Flag = ='N'|| Flag = ='N') {Condition=false; Break; } Else{Condition=true; Break; } } Else{printf ("The %d students ' grades are:\n", num); for(inti =0; I < n; ++i) {//printf ("%p\n", *pointer);printf ("%d", * (* (pointer + num-1) +i)); ///printf ("%d", * (* (Pointer_counterpart + num-1) + i));///there are still errors in the output here} printf ("\ n"); Condition=false; } }}voidFree (int**pointer,intm) { for(inti =0; I < m; ++i) { Free(* (pointer +i)); * (pointer + i) =NULL; }}
First unresolved issue: Dynamic memory allocation problem in allocation function
voidAllocationintMintN) { int*Pointer[m]; for(inti =0; I < m; ++i) {Pointer[i]= (int*)malloc(n *sizeof(int)); } for(inti =0; I < m; ++i) {printf ("Please input%d students%d grades:\n", i +1, N); for(intj =0; J < N; ++j) {scanf ("%d", * (pointer + i) +j); } } /** above using a for first pointer array memory is allocated well, then two for loop input data is successful. But there is an error in assigning the assignment as follows, and the error is in the function calculation, when
Number of students = 3, number of subjects = 3 o'clock, the third average error, calculation complete source has been marked, in the so far did not find out where there are errors to ...
For (int i = 0; i < m; ++i) { Pointer[i] = (int*) malloc (n * sizeof (int)); printf ("Please input%d students%d grades:\n", i + 1, n); for (int j = 0; J < N; ++j) { scanf ("%d", * (pointer + i) + j); } } There is a wrong */ *Print to try for (int i = 0; i < m; ++i) { printf ("The%d Studen T ' s grades are:\n ", I + 1); for (int j = 0; J < N; ++j) { printf ("%d", * (* (pointer + i) + j)); } printf ("\ n"); } Right to try the above input is correct, the result is no problem * * * * = pointer;}
The second unresolved issue:
voidSearch (int**pointer,intMintN) { intnum; BOOLCondition =true; while(condition) {printf ("Please input the student ' s number to search for his grades:\n"); scanf ("%d", &num); if(Num <1|| num >m) {CharFlag; printf ("ERROR, Reinput ' y ', exit ' n ':"); scanf ("%c", &flag); if(Flag = ='N'|| Flag = ='N') {Condition=false; Break; } Else{Condition=true; Break; } } Else{printf ("The %d students ' grades are:\n", num); for(inti =0; I < n; ++i) {//printf ("%p\n", *pointer);printf ("%d", * (* (pointer + num-1) +i)); ///printf ("%d", * (* (Pointer_counterpart + num-1) + i));///there is still an error in the output here, when the address of the pointer array is passed in here is not right, the head to explode. } printf ("\ n"); Condition=false; } }}
Unresolved issue: Pointer as parameter, double pointer, pointer array, dynamic memory allocation