1

Today I tried to make a program (algorithm) which search into an Array for the next number which is bigger than preview number found and print it.

The number has to be printed only if the next numbers are not smaller.

Fist example: 4,2,3,9,4,6,5: Output should be: 2,3,4,5.

Second example: 2,3,6,4,5,1 The output should be 1.

Third example: 1,9,8,7,6,5 The output should be 1,5.

Example, if array has the following elements 1,4,2,3,6,4,5,8,6 the output should be:

  1 2 3 4 5 6

and if array has the following elements 1,4,2,3 the output should be:

1,2,3

Here is the program:

#include <stdio.h>

int sortArr(int* array, int n, int next){
    int i,min = array[0];
    int lang;

    if(next==0){
        for (i=0;i<n;i++){
            if(array[i]<min){
                min=array[i];
            }
        }
    }

    if(next != 0){
        lang=next;
        while(lang==next){
            for (i=0;i<n;i++){
                if(array[i]<min){
                    min=array[i];
                }
            }

            lang++;

            if(lang== next){
                break;
            }
        }
        min=lang;
    }

    return min;
}

int main(void){
    int a[] = {1,4,2,3,6,4,5,8,6};
    int i,l = sizeof a / sizeof a[0];
    int next = 0;
    int min = sortArr(a,l,next);

    for(i=0;i<l;i++){
        if(min < a[i]){
            if(min < a[i]){
                min = sortArr(a,l,next);
                printf("%d ",min);
            }
            next++;
        }
    }
    printf("\n");
    return 0;
}

I think that, the Output has to do with the fact that the program it is working only if the first element is the smallest from the whole array elements.

EDIT: I tried the following too:

#include <stdio.h>

int sortArr(int* array, int n, int next){
    int i,min = array[0];
    int lang;

    if(next==0){
        for (i=0;i<n;i++){
            if(array[i]<min){
                min=array[i];
            }
        }
    }

    if(next != 0){
        lang=next;
        while(lang==next){
            for (i=0;i<n;i++){
                if(array[i]<min){
                    min=array[i];
                }
            }

            lang++;

            if(lang== next){
                break;
            }
        }
        min=lang;
    }

    return min;
}

int main(void){
    int a[] = {2,1,3,4};
    /*int a[] = {9,4,2,3,6,4,5,8,6};*/
    int i,l = sizeof a / sizeof a[0];
    int next = 0;
    int min = sortArr(a,l,next);

    for(i=0;i<l;i++){
        if(min == a[i]){
            continue;
        }

        if(min < a[i]){
            next++;
            min = sortArr(a,l,next);

        }
        printf("%d ",min);
    }
    printf("\n");
    return 0;
}

The output should be 2,3,4 but i get 2,2,3,4.

21
  • 1
    why not qsort()? Commented Aug 30, 2015 at 10:28
  • 2
    Please define "next biggest number found" Commented Aug 30, 2015 at 10:29
  • 2
    @Michi shouldn't it for "int a[] = {9,7,2,3,5,4,8,7);" be 2,3,4 instead of 2,3,4,7, if not I can't recognize any logical pattern, if not you should explain the algorithm you are trying to implement first Commented Aug 30, 2015 at 10:29
  • 1
    ah now I get it you are trying to implement some kind of lookahead behavior searching for the "next smallest" number and ignoring all numbers that a bigger then the "next smallest" Commented Aug 30, 2015 at 10:39
  • 2
    I really don't see why the output for 2,3,1,4 should start with 2 rather than 1 Commented Aug 30, 2015 at 10:53

3 Answers 3

2

Here is a simple C code that does the required.The logic is straight forward.

#include<stdio.h>
#include<stdlib.h>
int is_valid(int,int [],int);
void main()
{
 int a[]={1,4,2,3,6,4,5,8,6 };
 int i;
 for(i=0;i<sizeof(a)/sizeof(a[0])-1;++i)
 {
   if(is_valid(i,a,sizeof(a)/sizeof(a[0])))
    printf("%d ",a[i]);
 }
 printf("%d ",a[i]);
}
int is_valid(int i,int a[],int l)
{
 int j;

 for(j=i+1;j<l;++j)
  if(a[j]>=a[i])
   continue;
  else return 0;
 return 1;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Well i did already noticed that i made a wrong approach but your code works fine and does what i need.
if this code works fine then @John Coleman s assumption was applicable and int a[]={2,3,1,4} starts with a 1 and gives the output 1,4 instead of the 2,3,4
@aldr Sir, yes, there i made a mistake and created an Unwanted confusion .for 2,3,1,4 the output should be indeed 1,4, Thank you.
Well i learn fast, but not enough fast. I'm not Young anymore :))
@Michi But 3 is smaller than 4 and 4 comes before 3.
|
2
#include <stdio.h>

int main(){
    int a[] = {10,2,6,3,5,9,8,4,7};

   int l = sizeof a / sizeof a[0];
   int i,j;
   int check=1;

    for(i=0;i<l;i++){
        check=1;
        for(j=i+1;j<l;j++){
            if(a[i]>=a[j]){
               check=0;
            }
        }
        if(check)
            printf("%d",a[i]);
    }
    printf("\n");
    return 0;
}

Output: 2,3,4,7

10 Comments

"error: iteration 8u invokes undefined behavior [-Werror=aggressive-loop-optimizations]|"
i get wrong output if i use 10,2,6,3,5,9,8,4,7, the output which i get is 2 3 5 4 7, why 5 is there ?
5 is bigger than 4 that's why. 2,3,4,7 Should be @sumeet gave me the Answer , please check it,
Your code fails if i use int a[]={9,2,-1,4,3,7,8,4,3};, the output is -1, 3 and it should be -1, 3, 4. How can i fix this ?
@Mirchi For the output to be -1,3,3 just change the a[i]>=a[j] to a[i]>a[j]. The >= thing was just avoiding any number if it was repeating in the series.
|
1

On edit: tweaked to handle negative numbers.

Here is an answer that implements a find_next function which, when you pass it the array, the index of the last found entry and the value of this entry (or 0 in the initial pass) returns either the index of the next entry or the length of the array:

#include <stdio.h>

int find_next(int a[], int n, int i, int p){
    int j,k,min,initialized = 0;
    for(j = i; j < n; j++){
        if(!initialized){
            if(a[j] > p){
                min = a[j];
                k = j;
                initialized = 1;
            }
        }
        else{
            if(p < a[j] && a[j] < min){
                min = a[j];
                k = j;
            }
        }
    }
    return initialized?k:n;
}

int main(void){
   int a[] = {10,2,6,3,-1,5,9,8,4,7};
   int i,j,p, n = sizeof(a)/sizeof(a[0]);

   //find and print first item
   p = a[0];
   i = 0;
   for(j = i+1; j < n; j++){
       if(a[j] < p){
           i = j;
           p = a[i];
       }
   }
   printf("%d ",p);

   //loop using find_next to find subsequent
   i = find_next(a,n,i+1,p);
   while(i < n){
       p = a[i];
       printf("%d ",p);
       i = find_next(a,n,i+1,p);
   }
   printf("\n");
   return 0;
}

When run this prints -1 4 7

4 Comments

Yes, the program does what i need.
Fails if i add -1 like: 10,2,6,3,-1,5,9,8,4,7 i get the same output: 2,3,4,7 when should be -1,4,7
You never explained how to "boot up" your sequence. You referred to numbers smaller than the previous number but never said what to do when there is no previous number. Since all of your examples without exception involved positive integers I took p = 0 as a reasonable initial value for the previous argument. In your new example -- in main start with p = -2. In general -- let p = min(a) -1. This can easily be determined by a preliminary loop if need be.
Sir, i know, i just discovered now, sorry. I did not think that it will be different for positive and negative

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.