Home > Back-end >  Keyboard input M people, a total of N each semester, the average score and total average for each su
Keyboard input M people, a total of N each semester, the average score and total average for each su

Time:09-15

Why the output is 0 o code if there is any wrong please bosses give directions
#include
Int main ()
{
Int M, N;
The scanf (" % d % d ", & amp; M, & amp; N);
Int a [M] [N], [N] b={0}, I, j, sum;
for(i=0; ifor(j=0; j<=N - 1; J++)
The scanf (" % d ", & amp; A [I] [j]);
for(i=0; i{
for(j=0; j<=N - 1; J++)
B=b [j] + [j] a [I] [j];
}
for(i=0; ifor(j=0; j<=N - 1; J++)
The sum +=a, [I] [j].
for(j=0; j<=N - 1; J++)
Printf (" % 2 f \ n ", [j] b/M);
Printf (" % 2 f \ n ", the sum/(M * n));
return 0;
}

CodePudding user response:

Define the size of the array can only use constant specified latitude

You can use the malloc dynamic application memory
Int arr=(int) * * * * malloc (sizeof (int) * M * N);

Arr [I] [j] -- -- -- -- -- * (* (arr + I) + j)

CodePudding user response:

You literally outputs a first location of the array to see if successful entry

CodePudding user response:

You literally outputs a first location of the array to see if successful entry

CodePudding user response:

In general, your program exist the following problems:
1, program int a [M] [N], int b [N] way illegal, the definition of your this way only for M, N is a constant when applicable, when M, N is variable, can't define an array, and need dynamic application memory, can use malloc or new to apply, but new cause memory leaks, so it is better to use malloc:, specific implementation ways are detailed in the program code, and the results show that, there is not in detail;
2, sum not initialized in the program, because the sum will enter the sum +=a [I] [j] operation, if there is no initialization, will generate a random value, this is the place where many scholars casual make mistakes! Be sure to remember!
3, output the average score is a problem with the way, the printf (" %, 2 f \ n ", [j] b/M), because the C language of division/division, and mathematical procedure of two integer division, get the business, it is an integer, the integer part of the value of mathematics in the sense of business, so when b [j] less than M, get forever is 0, so need to [j] * 1.0 b/M of a decimal, note that if you simply type to float digital output is not getting the right values, this way is just change the value of the display on a pure! This also is many scholars ignore a problem, is also a very common problem, must learn basic knowledge well!!!!!! Can look at this article, some help to you! https://blog.csdn.net/weixin_43956598/article/details/90062550

Attach the modified program, has marked the wrong place:
 
#include
#include
#include
#include
using namespace std;
Int main () {
Int M, N;
Int I, j, sum;
The scanf (" % d % d ", & amp; M, & amp; N);
//int a [M] [N], [N] b={0};//this way define an array of technically incorrect
//when the dimension of the array is a variable, it is best to dynamic application way of the space, as follows:
Int * b=(int *) malloc sizeof (int) * N);//dynamic application of length N a one-dimensional array b
//dynamic application of two-dimensional array
Int a=(int) * * * * malloc (sizeof (int *) * M);//apply for M line space
for(i=0; iA [I]=(int *) malloc (sizeof (int) * N);

for(i=0; i<=N - 1; Initialized i++)//an array b
[I] b=0;
for(i=0; ifor(j=0; j<=N - 1; J++)
The scanf (" % d ", & amp; A [I] [j]);
for(i=0; ifor(j=0; j<=N - 1; J++) {
B=b [j] + [j] a [I] [j];
}
}
Sum=0;//variable must be initialized, otherwise it will generate a random number
for(i=0; ifor(j=0; j<=N - 1; J++)
The sum +=a, [I] [j].
for(j=0; j<=N - 1; J++)
Printf (" % 2 f \ n ", [j] * 1.0 b/M);//pay attention to when the result is likely to be the decimal, must use multiplied by 1.0 to get the mathematical in the sense of value, or when the denominator is greater than the molecules get value is always 0, so get the dealer/division in C language,
Printf (" % 2 f \ n ", sum * 1.0/(M * n));
return 0;
}


CodePudding user response:

Int a [M] [N], [N] b={0}, I, j, sum;
Seems very new compiler, can support this kind of writing, heard that is a new c + + standard,
  • Related