How to sort two vectors (ascending order) in a third vector using only one loop?

2

My teacher has posed this problem and I am not able to sort in ascending order just by using a loop to sort.

#include<stdio.h>

main(){
    int i,a[5]={1,4,8,9,11},b[5]={3,6,7,10,15},c[10];

    for(i=0;i<10;i++){

        if(a[i]<b[i] || b[i]>a[i]){

        }
    }

    for(i=0;i<10;i++){
        printf("%d\n",c[i]);            
    }   
}
    
asked by anonymous 14.04.2018 / 20:03

1 answer

-1
#include <stdio.h>
int main() {
    int i, j=0, k=0, a[5]={1,4,8,9,11}, b[5]={3,6,7,10,15}, c[10];
    for(i=0; i<10; i++) {
        if (a[j] < b[k]) {
            c[i] = a[j];
            j++;
        }
        else {
            c[i] = b[k];
            k++;
    }
    for (i=0; i<10; i++) {
        printf("%d\n",c[i]);
    }
    return 0;
}
    
16.04.2018 / 16:19