[英]Why %d is showing 0 in my c program output?
The program is to find the largest number amongst all the entered integers.该程序是在所有输入的整数中找到最大的数。 It asks the user to enter a number 10 times or press 0 to quit, whichever is earlier.
它要求用户输入一个数字 10 次或按 0 退出,以较早者为准。 But, the output is not as expected.
但是,output 不如预期。 I will appreciate it if you can help a newbie.
如果您能帮助新手,我将不胜感激。
#include <stdio.h>
int main()
{
int num[10];
int max, a;
for (a = 0; a < 10; a++)
{
printf("Enter the integer: ");
scanf("%d", &num[a]);
if (num[a] == 0)
break;
}
for(a = 0; a < 10; a++)
{
num[0] = max;
if(max < num[a])
{
num[a] = max;
}
}
printf("This is the largest integer: %d", max); //The output is coming wrong here.
return 0;
}
#include <limits.h>
int max = INT_MIN;
Also you need to change your second for loop as follows so as to update the max variable as you iterate, and not the num[] variables.此外,您还需要按如下方式更改第二个 for 循环,以便在迭代时更新 max 变量,而不是 num[] 变量。 Loop variable starts with 1 if you already assumed max to be first element before, else loop variable will start with 0.
如果您之前已经假设 max 是第一个元素,则循环变量从 1 开始,否则循环变量将从 0 开始。
for(a = 1; a < 10; a++) // a=0 if max was initialized to INT_MIN above
{
if(num[a]>max)
{
max = num[a];
}
}
You never assign the max
variable.您永远不会分配
max
变量。
What you want to do is to check if the value entered is greater than each one you've previously entered , so you would need to use the following condition:您要做的是检查输入的值是否大于您之前输入的每个值,因此您需要使用以下条件:
if (num[a] > max)
max = num[a];
You also need to initialize max
to some value (let's say, if you expect to have only positive integers, it could be 0
, but have a look at Jigsaw answer for a better solution): int max = 0;
您还需要将
max
初始化为某个值(比方说,如果您希望只有正整数,它可能是0
,但请查看Jigsaw答案以获得更好的解决方案): int max = 0;
. .
And eventually add an if-condition
that checks if max
is 0
, that way you know if no values have been entered:并最终添加一个
if-condition
来检查max
是否为0
,这样您就知道是否没有输入任何值:
if(max == 0)
printf("No values have been entered.");
else printf("This is the largest integer: %d", max);
Notice that you can assign the elements of num
and update max
in the same for loop, therefore the second for becomes completely useless and you can remove it:请注意,您可以在同一个 for 循环中分配
num
和 update max
的元素,因此第二个 for 变得完全无用,您可以将其删除:
#include <stdio.h>
int main()
{
int num[10];
int max = 0, a;
for (a = 0; a < 10; a++)
{
printf("Enter the integer: ");
scanf("%d", &num[a]);
if (num[a] == 0)
break;
if (num[a] > max)
max = num[a];
}
if(max == 0)
printf("No values have been entered.");
else printf("This is the largest integer: %d", max);
return 0;
}
I suggest you to turn on your compilers warning, especially -Wall
and -Wextra
, so you would notice problems like these:我建议你打开你的编译器警告,尤其是
-Wall
和-Wextra
,这样你会注意到这样的问题:
<source>: In function 'main':
<source>:17:16: warning: 'max' may be used uninitialized [-Wmaybe-uninitialized]
17 | num[0] = max;
| ~~~~~~~^~~~~
<source>:6:9: note: 'max' was declared here
6 | int max, a;
| ^~~
You neither initialise nor ever write any value at all to the variable max
.您既不初始化也不向变量
max
写入任何值。 Using uninitialised variables is undefined behaviour.使用未初始化的变量是未定义的行为。
You seem to know how to write 0 into a
.您似乎知道如何将 0
a
. But for max
you seem to have it reversed.但是对于
max
,你似乎把它颠倒了。 Remember that with =
the value on the right of it goes into the variable on the left of it.请记住,with
=
右边的值进入到左边的变量中。
To fix the problem, turn any something = max;
要解决此问题,请转动任何
something = max;
which occurs in your code into max = something;
在您的代码中出现
max = something;
. .
For starters this for loop对于初学者来说,这个 for 循环
for (a = 0; a < 10; a++)
{
printf("Enter the integer: ");
scanf("%d", &num[a]);
if (num[a] == 0)
break;
}
can enter less than 10
elements in the array num
due to the condition由于条件,可以在数组
num
中输入少于10
元素
if (num[a] == 0)
break;
So the next loop has to traverse exactly a
elements of the array not 10
.所以下一个循环必须准确地遍历数组
a
元素而不是10
。 So in the next loop you have to use another variable for the index as for example因此,在下一个循环中,您必须为索引使用另一个变量,例如
for( int i = 0; i < a; i++)
The variable max
was not initialized变量
max
未初始化
int max, a;
So this for loop invokes undefined behavior where the variable max
is assigned to elements of the array or where it is compared with elements of the array.因此,此 for 循环调用未定义的行为,其中变量
max
被分配给数组元素或与数组元素进行比较。
for(a = 0; a < 10; a++)
{
num[0] = max;
if(max < num[a])
{
num[a] = max;
}
}
Moreover the variable max
is not changed within the for loop.此外,变量
max
在 for 循环中没有改变。 So the loop in whole does not make sense.所以整个循环没有意义。
Pay attention to that the user can enter 0
at once.注意用户可以一次输入
0
。 So the array will not contain any valid values.因此该数组将不包含任何有效值。 In this case the array does not have a maximum value.
在这种情况下,数组没有最大值。
Your program can look for example the following way例如,您的程序可以通过以下方式查看
#include <stdio.h>
int main( void )
{
enum { N = 10 };
int num[N];
int i = 0;
do
{
printf( "Enter the integer (0 - stop): " );
scanf( "%d", &num[i] );
} while ( num[i] != 0 && ++i < N );
int max_i = 0;
for ( int j = 1; j < i; j++ )
{
if ( num[max_i] < num[j] )
{
max_i = j;
}
}
if ( i == 0 )
{
puts( "You have not entered numbers unequal to zero." );
}
else
{
printf("This is the largest integer: %d", num[max_i] );
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.