[英]'For loop' to arrange Numbers in ascending order. [ERROR] pointer and integer comparision
This is a very simple program, to arrange numbers in ascending order. 这是一个非常简单的程序,可以按升序排列数字。 Now here I don't know how it says there is the comparison between integer and pointer, in all the for loops.
现在,我不知道在所有的for循环中它如何表示整数和指针之间存在比较。 I am a noob by the way.
顺便说一句,我是菜鸟。
#include <stdio.h>
int main()
{
int number[100],total,i,j,temp;;
printf("Enter the quantity of the numbers you want \n");
scanf("%d",&total);
printf("Enter the numbers \n");
for(i=0; i<total; i++){
scanf("%d",&number[i]);
}
for(i=0; i < (number-1); i++){
for(j=(i+1); j < number; j++){
if(number[i]>number[j]){
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
for(i=0; i < number; i++){
printf("%d \n", number[i]);
}
return 0;
}
You are using i < number
where i
is int
and number
is an array
您正在使用
i < number
,其中i
是int
并且number
是一个array
Just change those lines to below 只需将这些行更改为下面
for(i=0; i < (total-1); i++){
for(j=(i+1); j < total; j++){
for(i=0; i < total; i++){
the following proposed code: 以下建议的代码:
stderr
stderr
And now, the proposed code: 现在,建议的代码:
#include <stdio.h> // scanf(), perror(), printf()
#include <stdlib.h> // exit(), EXIT_FAILURE
int main( void )
{
int total;
printf("Enter the quantity of the numbers you want \n");
if( 1 != scanf( "%d", &total ) )
{
perror( "scanf for count of numbers failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
if( !total )
{ // then 0 entered
printf( "Exiting, no numbers to enter\n" );
return 0;
}
// implied else, some numbers to enter
int number[ total ]; // << uses the VLA feature of C
for( unsigned i = 0; i < total; i++ )
{
printf( "Enter number %u: ", i+1 );
if( 1 != scanf( "%d", &number[i] ) )
{
perror( "scanf for a number failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
}
for( int i = 0; i < (total - 1); i++ )
{
for( int j = (i+1); j < total; j++ )
{
if( number[i] > number[j] )
{
int temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
for( unsigned i = 0; i < total; i++ )
{
printf("%d \n", number[i]);
}
return 0;
} // end function: main
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.