【问题标题】:high low values in array print in between数组打印中的高低值介于两者之间
【发布时间】:2014-03-21 20:46:42
【问题描述】:

代码用于将整数扫描到数组中,当输入0时循环停止扫描。 在值被扫描后,数组内的最高值和最低值将被找到 找到 high 和 low 后,打印数组索引 high 和 low 之间的值

所以如果输入 5,2,8,7,6,12,6,4,5 输出应该是 2,7,6,12

我的程序在扫描输入值并输入 0 以结束循环后失败

#include <stdio.h>


int main(){

int high=4;
int low,i;
int array[25];
int count=0;

printf("Please input numbers for array:");    

for(i=0;array[i]!=0;i+=1){
    scanf("%d",&array[i]);
    count+=1;
   }

for(i=0;i<count;i+=1){
    if(array[i]>array[high]){
        high=i;
    }
}

low=high;

for(i=0;i<count;i+=1){   
     if(array[i]<array[low]){
        low=i;  
     }
}

for(i=low;low<=high;i+=1){
    printf("%d,",array[i]);
}

}

【问题讨论】:

  • 我的水晶球告诉我:i=count 不是你想要的 for 循环条件表达式。
  • 缓冲区溢出 t 减 26...
  • 是的,谢谢你的小疏忽

标签: c arrays loops


【解决方案1】:

for循环在每次迭代之前检查条件,所以在

 array[i]!=0

在读取数组[i] 之前,您正在检查未初始化的值。如果它没有碰巧在内存中的某个地方找到一个零,这可以继续读取超过 25 个值,它甚至可以继续直到你得到堆栈溢出。

另外,在其他 for 循环中,您可能的意思是

 i < count

条件

 low<high

真的不合适。

这是一个更符合预期的版本:

   #include <stdio.h>


   int main(){

           int high;
           int low,i;
           int array[25];
           int count=0;
           int start;
           int end;

           printf("Please input numbers for array:");

           for (i = 0; scanf("%d", &array[i]), array[i] != 0; i += 1) {
                   count+=1;
                   if (i >= 25) {
                           printf("Unable to handle more than 25 input values\n");
                           break;
                   }
           }

           high = 0;
           for (i = 1; i < count; i += 1) {
                   if (array[i] > array[high]) {
                           high = i;
                   }
           }

           low = 0;
           for (i = 1; i < count; i += 1) {
                   if (array[i] < array[low]) {
                           low=i;
                   }
           }

           if (low < high) {
                   start = low;
                   end = high;
           }
           else {
                   start = high;
                   end = low;
           }

           for (i = start; i <= end; i += 1) {
                   printf("%d", array[i]);
                   if (i != end) {
                           printf(",");
                   }
                   else {
                           printf("\n");
                   }
           }
   }

【讨论】:

    【解决方案2】:
    #include <stdio.h>
    
    int main(){
        int low, high, array[25];
        int i, count=0;
    
        printf("Please input numbers for array:");  
    
        for(i=0;i<25;++i){
            int data;
            scanf("%d", &data);
            if(data==0)
                break;
            array[count++]=data;
        }
    
        low = high = 0;
        for(i=1;i<count;++i){
            if(array[i]<array[low])
                low=i;  
            if(array[i]>array[high])
                high=i;  
        }
    
        for(i=low;i <= high;i++){
            printf("%d,", array[i]);
        }
    }
    

    【讨论】:

      【解决方案3】:

      经过一些小改动后,您的代码可以正常工作

          #include <stdio.h>
      
          int main()
          {
      
          int high=0;     // high value assumed to be zero for ease of understanding
          int low=0,i;    // low value assumed to be zero for ease of understanding
          int array[25];
          int count=0;
      
          printf("Please input numbers for array:");    
      
          for(i=0;;i+=1){
              scanf("%d",&array[i]);
              count+=1;
              if(array[i]==0)        // Whenever is zero is read the loop is immediately exited, otherwise looping is continued
                  break;
             }
            count--;                // decrement one count (count of zero)
      
          for(i=1;i<count;i+=1){         
              if(array[i]>array[high]){
                  high=i;
              }
            }
      
      
          for(i=1;i<count;i+=1){   
               if(array[i]<array[low]){
                  low=i;  
               }
             }
      
          for(i=low;i<=high;i+=1){            //printing from low value upto high value
              printf("%d,",array[i]);
               }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        • 2018-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-19
        相关资源
        最近更新 更多