这是一个非常简单的程序,用于按升序排列数字。现在我不知道在所有的for循环中它是如何在整数和指针之间进行比较的。顺便说一句,我是个菜鸟。
#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;
}
答案 0 :(得分:0)
您正在使用i < number
i
为int
而number
为array
只需将这些行更改为
for(i=0; i < (total-1); i++){
for(j=(i+1); j < total; j++){
for(i=0; i < total; i++){
答案 1 :(得分:0)
以下提议的代码:
stderr
现在,建议的代码:
#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