The average of the first data set should be: (80+70+65+89+90)/5 and the second set (85+80+80+82+87)/5, but for some reason my code is not working.
第一个数据集的平均值应为:(80 + 70 + 65 + 89 + 90)/ 5和第二个数据集(85 + 80 + 80 + 82 + 87)/ 5,但由于某种原因,我的代码无效。
When I run the code I get 17.0 and 219886384 instead of 78.8 and 82.8.
当我运行代码时,我得到17.0和219886384而不是78.8和82.8。
int main(void)
{
int grades[2][5] = {{80, 70, 65, 89, 90}, {85, 80, 80, 82, 87}};
float average;
int sum;
int i;
int j;
for(i = 0; i < 2; i++)
{
sum = 0;
for(j = 0; j < 5; j++);
{
sum += grades[i][j];
}
average = sum / 5;
printf("The average grade for %d is: %f\n", i, average);
}
return 0;
}
3 个解决方案
#1
0
A ';' means the finish of the for loop so your code 'for(j = 0; j < 5; j++);' equals to j=5;
一个 ';'表示for循环的结束,所以你的代码'for(j = 0; j <5; j ++);'等于j = 5;
Array Index Out Of Bounds!
数组索引超出界限!
#2
2
The average is computed using integer arithmetics: sum / 5
. Either use sum / 5.0
or define sum
as a floating point variable.
使用整数算术计算平均值:sum / 5.使用sum / 5.0或将sum定义为浮点变量。
There is a silly mistake in this for
statement, the extra semicolon after the for clauses is an empty statement, reducing the for
to en empty loop followed by a single block, that accesses an entry beyond the end of the array.
在这个for语句中有一个愚蠢的错误,for子句后面的额外分号是一个空语句,将for循环减少到一个空循环,然后是一个块,它访问一个超出数组末尾的条目。
for(j = 0; j < 5; j++); <--- spurious semicolon!
{
sum += grades[i][j];
}
If you were to put the opening brace on the same line, a style known as Kernighan and Ritchie, such a mistake would become quite unlikely:
如果你把开口支架放在同一条线上,一种称为Kernighan和Ritchie的风格,那么这样的错误就不太可能了:
for (j = 0; j < 5; j++) {
sum += grades[i][j];
}
Similarly, if you define the loop index inside the for
clause, this mistake becomes a syntax error as the loop index would be out of scope in the block:
类似地,如果在for子句中定义循环索引,则此错误将成为语法错误,因为循环索引将超出块中的范围:
#include <stdio.h>
int main(void) {
int grades[2][5] = {{80, 70, 65, 89, 90}, {85, 80, 80, 82, 87}};
for (int i = 0; i < 2; i++) {
float average, sum = 0;
for (int j = 0; j < 5; j++) {
sum += grades[i][j];
}
average = sum / 5;
printf("The average grade for %d is: %f\n", i, average);
}
return 0;
}
#3
0
As @woz says, part of your problem is that you never run sum += grades[i][j];
in a loop from 0 to 4.
正如@woz所说,你问题的一部分是你永远不会运行sum + = grades [i] [j];在0到4的循环中。
Another problem is that sum
and 5
are of the type int
, so even after you remove the extra semicolon from the inner loop, you will still have an incorrect output. You should instead do: average = sum / 5.0f;
另一个问题是sum和5都是int类型,所以即使从内部循环中删除了额外的分号,你仍然会有一个不正确的输出。你应该这样做:average = sum / 5.0f;
Thus, your code should be:
因此,您的代码应该是:
#include "stdio.h"
int main(void)
{
int grades[2][5] = {{80, 70, 65, 89, 90}, {85, 80, 80, 82, 87}};
float average;
int sum;
int i;
int j;
for(i = 0; i < 2; i++)
{
sum = 0;
for(j = 0; j < 5; j++) // CHANGE 1: remove semicolon
{
sum += grades[i][j];
}
average = sum / 5.0f; // CHANGE 2: ensure that the result is a floating point number
printf("The average grade for %d is: %f\n", i, average);
}
return 0;
}
You can run it here.
你可以在这里运行它。
<script src="//repl.it/embed/JLAN/0.js"></script>
In the future, when you come across such differences between your expected and actual results, add breakpoints to your code to see what it is actually doing before searching for solutions online.
将来,当您遇到预期结果和实际结果之间的差异时,请在代码中添加断点,以便在联机搜索解决方案之前查看其实际执行的操作。
The reason you get the extremely large value is that the integer in memory that immediately follows your array (at the hypothetical 5
position is used to compute the "sum" and "average"). (see relevant SO post)
你获得极大值的原因是紧跟在你的数组之后的内存中的整数(在假设的5位置用于计算“sum”和“average”)。 (见相关SO帖子)
#1
0
A ';' means the finish of the for loop so your code 'for(j = 0; j < 5; j++);' equals to j=5;
一个 ';'表示for循环的结束,所以你的代码'for(j = 0; j <5; j ++);'等于j = 5;
Array Index Out Of Bounds!
数组索引超出界限!
#2
2
The average is computed using integer arithmetics: sum / 5
. Either use sum / 5.0
or define sum
as a floating point variable.
使用整数算术计算平均值:sum / 5.使用sum / 5.0或将sum定义为浮点变量。
There is a silly mistake in this for
statement, the extra semicolon after the for clauses is an empty statement, reducing the for
to en empty loop followed by a single block, that accesses an entry beyond the end of the array.
在这个for语句中有一个愚蠢的错误,for子句后面的额外分号是一个空语句,将for循环减少到一个空循环,然后是一个块,它访问一个超出数组末尾的条目。
for(j = 0; j < 5; j++); <--- spurious semicolon!
{
sum += grades[i][j];
}
If you were to put the opening brace on the same line, a style known as Kernighan and Ritchie, such a mistake would become quite unlikely:
如果你把开口支架放在同一条线上,一种称为Kernighan和Ritchie的风格,那么这样的错误就不太可能了:
for (j = 0; j < 5; j++) {
sum += grades[i][j];
}
Similarly, if you define the loop index inside the for
clause, this mistake becomes a syntax error as the loop index would be out of scope in the block:
类似地,如果在for子句中定义循环索引,则此错误将成为语法错误,因为循环索引将超出块中的范围:
#include <stdio.h>
int main(void) {
int grades[2][5] = {{80, 70, 65, 89, 90}, {85, 80, 80, 82, 87}};
for (int i = 0; i < 2; i++) {
float average, sum = 0;
for (int j = 0; j < 5; j++) {
sum += grades[i][j];
}
average = sum / 5;
printf("The average grade for %d is: %f\n", i, average);
}
return 0;
}
#3
0
As @woz says, part of your problem is that you never run sum += grades[i][j];
in a loop from 0 to 4.
正如@woz所说,你问题的一部分是你永远不会运行sum + = grades [i] [j];在0到4的循环中。
Another problem is that sum
and 5
are of the type int
, so even after you remove the extra semicolon from the inner loop, you will still have an incorrect output. You should instead do: average = sum / 5.0f;
另一个问题是sum和5都是int类型,所以即使从内部循环中删除了额外的分号,你仍然会有一个不正确的输出。你应该这样做:average = sum / 5.0f;
Thus, your code should be:
因此,您的代码应该是:
#include "stdio.h"
int main(void)
{
int grades[2][5] = {{80, 70, 65, 89, 90}, {85, 80, 80, 82, 87}};
float average;
int sum;
int i;
int j;
for(i = 0; i < 2; i++)
{
sum = 0;
for(j = 0; j < 5; j++) // CHANGE 1: remove semicolon
{
sum += grades[i][j];
}
average = sum / 5.0f; // CHANGE 2: ensure that the result is a floating point number
printf("The average grade for %d is: %f\n", i, average);
}
return 0;
}
You can run it here.
你可以在这里运行它。
<script src="//repl.it/embed/JLAN/0.js"></script>
In the future, when you come across such differences between your expected and actual results, add breakpoints to your code to see what it is actually doing before searching for solutions online.
将来,当您遇到预期结果和实际结果之间的差异时,请在代码中添加断点,以便在联机搜索解决方案之前查看其实际执行的操作。
The reason you get the extremely large value is that the integer in memory that immediately follows your array (at the hypothetical 5
position is used to compute the "sum" and "average"). (see relevant SO post)
你获得极大值的原因是紧跟在你的数组之后的内存中的整数(在假设的5位置用于计算“sum”和“average”)。 (见相关SO帖子)