Home > Back-end >  Function and problems
Function and problems

Time:12-09

Don't know why, the reference to the avr function is only two values, consult bosses see answer
Could it be the place to write the wrong ah//marked points


# include "stdio.h"//calculate various average
# define Pnum 3//person
# define Snum 3//subject
Float avr (float data [])
{
float sum=0;
int i=0;
Do {
The sum +=data [i++];
} while (I==sizeof (data)/(sizeof (float)));//markers in
Return the sum/I;
}
//int Max (float * data)
Int main ()
{
Float stu [Pnum], gra [Snum], list [Pnum] [Snum];
Int I, j;
//enter
For (I=0; I & lt; Pnum; I++) {
For (j=0; J & lt; Snum; J++) {
Scanf_s (" % f ", * (the list + I) + j));
}
}
//print
For (I=0; I & lt; Pnum; I++) {
For (j=0; J & lt; Snum; J++) {
Printf (" % 6.1 f ", * (* (the list + I) + j));
If (j==Snum - 1) {
printf("\n");
}
}
}

For (I=0; I & lt; Pnum; I++) {
For (j=0; J & lt; Snum; J++) {
Gra [j]=list [I] [j];//each student's report card
}
Printf (" % d student of average can be divided into: % 5.2 f \ n ", I + 1, avr (gra));
}

For (j=0; J & lt; Snum; J++) {
For (I=0; I & lt; Pnum; I++) {
Stu [I]=list [I] [j];
}
Printf (" % d average of the course can be divided into: % 5.2 f \ n ", j + 1, avr (stu));
}
return 0;

CodePudding user response:

Is write wrong,
Writing array name parameter is actually a compatible type of pointer, sizeof is a pointer to the size of the array size or other incoming parameters, or global come in an array, or variable incoming array encapsulated in the structure,

CodePudding user response:

According to the thinking of the building Lord, modified as follows, for your reference:
 # include & lt; Stdio. H> 
# define Pnum 3//person
# define Snum 3//subject

Float avr (float data [], int num)//function increase or decrease in a parameter
{
float sum=0;
int i=0;
Do {
The sum +=data [i++];
} while (I & lt; Num);//==sizeof (data)/(sizeof (float)));//markers in
Return the sum/I;
}

//int Max (float * data)

Int main (int arg c, char * argv [])
{
Float stu [Pnum], gra [Snum], list [Pnum] [Snum];
Int I, j;
//enter
For (I=0; I & lt; Pnum; I++) {
For (j=0; J & lt; Snum; J++) {
The scanf (" % f ", * (the list + I) + j));
}
}
//print
For (I=0; I & lt; Pnum; I++) {
For (j=0; J & lt; Snum; J++) {
Printf (" % 6.1 f ", * (* (the list + I) + j));
If (j==Snum - 1) {
printf("\n");
}
}
}

For (I=0; I & lt; Pnum; I++) {
For (j=0; J & lt; Snum; J++) {
Gra [j]=list [I] [j];//each student's report card
}
Printf (" % d student of average can be divided into: % 5.2 f \ n ", I + 1, avr (gra, Snum));
}

For (j=0; J & lt; Snum; J++) {
For (I=0; I & lt; Pnum; I++) {
Stu [I]=list [I] [j];
}
Printf (" % d average of the course can be divided into: % 5.2 f \ n ", j + 1, avr (stu, Pnum));
}
return 0;
}

CodePudding user response:

reference 1st floor forever74 response:
is write wrong,
Writing array name parameter is actually a compatible type of pointer, sizeof is a pointer to the size of the array size or other incoming parameters, or global come in an array, or the array encapsulated in the structure of variable incoming,

Understood, thank the bosses

CodePudding user response:

refer to the second floor QZJHJXJ response:
according to the thinking of the building Lord, modified as follows, for reference:
 # include & lt; Stdio. H> 
# define Pnum 3//person
# define Snum 3//subject

Float avr (float data [], int num)//function increase or decrease in a parameter
{
float sum=0;
int i=0;
Do {
The sum +=data [i++];
} while (I & lt; Num);//==sizeof (data)/(sizeof (float)));//markers in
Return the sum/I;
}

//int Max (float * data)

Int main (int arg c, char * argv [])
{
Float stu [Pnum], gra [Snum], list [Pnum] [Snum];
Int I, j;
//enter
For (I=0; I & lt; Pnum; I++) {
For (j=0; J & lt; Snum; J++) {
The scanf (" % f ", * (the list + I) + j));
}
}
//print
For (I=0; I & lt; Pnum; I++) {
For (j=0; J & lt; Snum; J++) {
Printf (" % 6.1 f ", * (* (the list + I) + j));
If (j==Snum - 1) {
printf("\n");
}
}
}

For (I=0; I & lt; Pnum; I++) {
For (j=0; J & lt; Snum; J++) {
Gra [j]=list [I] [j];//each student's report card
}
Printf (" % d student of average can be divided into: % 5.2 f \ n ", I + 1, avr (gra, Snum));
}

For (j=0; J & lt; Snum; J++) {
For (I=0; I & lt; Pnum; I++) {
Stu [I]=list [I] [j];
}
Printf (" % d average of the course can be divided into: % 5.2 f \ n ", j + 1, avr (stu, Pnum));
}
return 0;
}

Uh huh, already know, the length of the array need to preach, sizeof the incoming array doesn't work

CodePudding user response:

Float avr (float data []) {... }
Parameters of float data [], is actually float * data
Look at the sizeof (float *) and sizeof (float), is clear about

Change the function for
Float avr (float * data, int size) {... }!
  • Related