Trying to program
Trying to program

Reputation: 15

high low values in array print in between

code is used to scan integers into an array the loop is to stop scanning when 0 is input. after values are scanned in the highest and lowest values inside the array are to be found after finding high and low , print the values between the the array indexs of high and low

so if input 5,2,8,7,6,12,6,4,5 output should be 2,7,6,12

my program fails after it scans in the input values and 0 is input to end the loop

#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]);
}

}

Upvotes: 0

Views: 1511

Answers (3)

Sorcrer
Sorcrer

Reputation: 1644

I've got your code working after some minor changes

    #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]);
         }

    }

Upvotes: 0

BLUEPIXY
BLUEPIXY

Reputation: 40145

#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]);
    }
}

Upvotes: 0

G&#225;bor Buella
G&#225;bor Buella

Reputation: 1920

The for loop checks the condition before each iteration, so in

 array[i]!=0

you are checking uninitalized value, before reading in array[i]. If it doesn't happen to find a zero hanging around somewhere in memory, this can go on reading more than 25 values, it can even go on and on till you get a stack overflow.

Also, in the other for loops, you probably meant

 i < count

The condition

 low<high

is just really not appropriate.

Here is a version that should work more like expected:

   #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");
                   }
           }
   }

Upvotes: 1

Related Questions