C比较数字、存储在数组中以及打印

C比较数字、存储在数组中以及打印,c,arrays,printing,io,compare,C,Arrays,Printing,Io,Compare,我需要做的是输入一行数字,例如: 7 5 3 4 1 2 我希望能够扫描和比较每个数字,删除最高和最低的数字,然后重新打印列表(与原来的顺序相同) 示例输出: 5 3 4 2 以下是我目前的代码: #include<stdio.h> main( ){ int low = 10000; int high = -10000; int current; char c; int array[6] = { }; int i = 0;

我需要做的是输入一行数字,例如:

7 5 3 4 1 2
我希望能够扫描和比较每个数字,删除最高和最低的数字,然后重新打印列表(与原来的顺序相同)

示例输出:

5 3 4 2
以下是我目前的代码:

#include<stdio.h>

main( ){
    int low = 10000;
    int high = -10000;
    int current;
    char c;
    int array[6] = { };
    int i = 0;
    scanf( "%i", &low );
    high = low;
    while ( ( c = getchar( ) ) != EOF ){
        scanf( "%i", &current );
        if ( current < low ){
            array[i] = low;
            low = current;
            i++;
        }
        else if ( current > high ){
            array[i] = high;
            high = current;
            i++;
        }
    }
    for ( i = 0; i < 4; i++ ) {
        printf( "%i ", array[i] );
    }
}

下面的代码是您的问题

if(current < low){
  array[i] = low;
  low=current;
  i++;
}
else if(current > high){
  array[i] = high;
  high=current;
  i++;
if(电流<低){
数组[i]=低;
低=电流;
i++;
}
否则,如果(当前>高){
数组[i]=高;
高=电流;
i++;
}

这会将旧的高点或低点置于新的高点或低点所在的位置。因此,每次找到一个新的最大值或最小值时,都要对数组重新排序

您似乎也在奇怪地读取输入。scanf功能是printf的输入模拟,在相反方向提供许多相同的转换设施。第一个scanf将7放入low,getchar拾取空格,然后将5放入low,将7放入数组,因此它显示为数组中的第一个元素。它无法检测到它应该是最大值

因为你使用了一个else,如果你不能得到一个可能的高,直到某个东西不能成为一个可能的低。因此,您的代码认为4是最大值


数组的最后一个元素是0,因为while在2之后退出,而没有将其放入数组中。因此,这是编译器的一个工件。旧版本的C会向您显示内存单元中发生的任何事情,除非您显式设置初始值。

您应该更改这些行:
int low=10000;int高=-10000。在标题
limits.h
中,存在具有
int
s的最高和最低值的宏:
int low=int_MAX;int high=int_MIN。如果将空的花括号分配给
数组[6]
,这是如何工作的呢?很抱歉,代码看起来毫无希望。如果你认为这会有帮助的话,我可以从头开始写,并附上解释。也许你应该跳回一个更简单的任务…如果我用错误的方法来做这件事,那会帮助我解决很多问题,谢谢。
if(current < low){
  array[i] = low;
  low=current;
  i++;
}
else if(current > high){
  array[i] = high;
  high=current;
  i++;