Home > Back-end >  Could you tell me how to change the C program should I?
Could you tell me how to change the C program should I?

Time:10-02


# include & lt; stdio.h>
#include
Int aver (int scores [5] [4])
{
Double business=0;
Int sum=0;
For (int I=0; I & lt; 5; I++)
{
For (int j=0; J & lt; 4. J++)
{
The sum +=scores [I] [j];
}
Business=sum/4.0;
Printf (" % d a schoolmate's average grade: % c \ n ", I + 1, business);
}
}
Typedef struct student
{
Int num.
Char name [7];
Char sex;
int age;
Int scores [4].
} STUDENT;
Int main ()
{
Int ret.
STUDENT stu [5]={{135, "Chou", "m", 19,80,80,80,80}, {136, "yellow", 'm', 19,80,80,80,80}, {137, "zheng", "m", 19,80,80,80,80}, {138, leijiacha etc, 'm', 19,80,80,80,80}, {139, "xiao zhu", "m", 20,80,80,80,80}};
STUDENT * p;
Printf (" student id name gender age line generation calculus program design English \ n ");
For (p=stu. P & lt; Stu + 5; P++)
{
Printf (" % 5 d % s % 5 d % d % % 2 c 6 7 8 d % d % d % \ n ", p - & gt; Num, p - & gt; The name, p - & gt; Sex, p - & gt; Age, p - & gt; Scores [0], p - & gt; Scores [1], p - & gt; Scores [2], p - & gt; Scores of [3]);
}
Ret=aver (p - & gt; Scores [0], p - & gt; Scores [1], p - & gt; Scores [2], p - & gt; Scores of [3]);
System (" PAUSE ");
return 0;
}

CodePudding user response:

I and you don't run the same program, I compiled directly however, aver definition digital, calling the four values

CodePudding user response:

You confuse the two arrays, array structure on students, and each student's score array,
Your code is trying to put the two cross an array a two-dimensional array of intellectual confusion



Aside to problems in the design of the logic, you define the function of int aver (int scores [5] [4])
This function is actually only one parameter, is a two-dimensional array (pointer), but when you call the four parameters, this itself is also a problem

Get to the point, do demand analysis, you first understanding of demand should be wrong
Average, usually for a course group's grade point averages, rather than for a person to calculate, (your code in the calculation of average is 4)
Such as English, mathematics, mathematical hypothesis is out in the university, English is 150 scale, you calculate the personal average count
The general statistics is one of a class of average, or more generally, the total average score of all people

In order to simplify the problem, don't do too much complicated things, for your reference, can be controlled under the
 # include & lt; stdio.h> 
# include & lt; Stdlib. H>

/*
In order not to complicate the problem, continue to use your mind
Scores [0], [1] [2] [3] represent
Line generation calculus program design English
-
But from the point of design thought, this is not good,
Implicit information, only you know the relation between
He can't use the program to constraint, establishes the corresponding relations between
For beginners language features of you, that first
*/
Struct student_st
{
Int num.
Char name [16].
Char sex;
int age;
Int scores [4].
};
Typedef struct student_st student;

/* set according to the student, every subject to calculate average
Stored in the avg_score,
Here there are some design details, such as to use the
Function, the incoming array to hold the student course number
By more very, here the default with a strict one-to-one correspondence relationship,
Leave you to think, these thinking can help you design a
Better software
*/
Int aver (student * s, double * avg_score)//and aver (student s [], int avg_score []) is the same as
{
Int sum=0;
For (int I=0; I & lt; 4. I++)//4 courses, according to the average course
{
Sum=0;//to initialize every time, or it will accumulate to the result of the last
For (int j=0; J & lt; 5; J++)
{
/* nested relations, array, array containing unlock access it step by step a good
To access the first j a student data to do, s [j] first,
The first j a student's score, s [j]. Score */
/*
Here a spoof, Pointers are a senior as elegant as you want to?

*/
The sum +=* ((s + j) - & gt; Scores + I);//use s [j]. Scores [I]; For the fix, often with the array subscript access to make your life easier
}
* (avg_score + I)=(double) sum/5;//avg_score [I]=sum/4.0;
//printf (" % d course grade point average: % f \ n ", I + 1, * (avg_score + I));
}
}
Int main ()
{
Int ret.
/* branch alignment, look good, eye protect brain
Soon found the problem, names have a space in the
Help you to change the data, embody the value of your program,
Can calculate the average, or scores are all the same, need not calculate all know average
Finally you would like to become that last students with excellent grades, send you the female, woman! */
Student stu [5]={{1, "Chou", 'm', 19, 81, 40, 70, 84},
{136, the "yellow", 'm', 19, 40, 50, 88, 30},
{137, "zheng", "m", 19, 60, 20, 89, 0},
{13822, "little", 'm', 19, 80, 70, 66, 77},
{139, "xiao zhu", "f", 20, 97, 100, 99, 99}};
Student * p;
Printf (" \ n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - student information overview -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n \ n ");
/* * a simple output alignment method/
Printf (" % s \ % s \ \ t t t % s % s \ \ t t % s % s \ \ n \ t t % s % s ", "student id", "name", "gender", "age", "line", "calculus", "programming", "English");
/* it is good to use Numbers directly, but from practice, familiar with Pointers is nothing wrong with */
For (p=stu. P & lt; Stu + 5; P++)
{
/* a single character with % c, string with % s
Character, string,,, Chinese characters printf output alignment is a nightmare, when not neat and aligned \ t */
Printf (" % d % % s \ \ t t c \ \ t t % d % d % d % d \ \ t t t % d \ \ t \ n ", p - & gt; Num, p - & gt; The name, p - & gt; Sex, p - & gt; Age,
P - & gt; Scores [0], p - & gt; Scores [1], p - & gt; Scores [2], p - & gt; Scores of [3]);
}
/* average
Used in front of the implied agreement,
[0], [1] [2] [3] represent
Line generation calculus program design English
- to know trouble doing so, each relating to the subject result processing (programming), have to look back to see
What confirmation once, on behalf of
*/
Double avg_score [4].

Ret=aver (stu, avg_score);

Printf (" \ n \ n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- average grade in all the subjects -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n \ n ");
Printf (" % s % s \ \ t \ n \ t t % s % s ", "line", "calculus", "programming", "English");
Printf (" % 2 f \ t % 2 f \ t % 2 f \ t \ t % 2 f \ n \ n ", avg_score [0], avg_score [1],
Avg_score avg_score [2], [3]);
System (" PAUSE ");
return 0;
}



CodePudding user response:

Thank you for your heart, for a beginner to help a lot!
  • Related