Reputation: 5297
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.
Upvotes: 1
Views: 140
Reputation: 51998
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
Upvotes: 1
Reputation: 1222
#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
Upvotes: 2
Reputation: 8292
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;
}
Upvotes: 2