UAB – 2006: Problem 1: Merging at the Brick Yard


The Indianapolis 500 race is just a few weeks away. They need your help in creating a program that will provide the order in which the cars are to appear at the starting line (ordering is according to the number placed on the car).

There are two garages where the race cars are stored. Each garage will form a line of 5 cars that are sorted by the number on the car. As the cars from the two garages merge to form a single start line, your program must maintain the ordering such that the smallest numbered cars are earlier in the line than those with larger numbers.

Note: Some cars may share the same number, and the numbers range from 1 to 10. You may assume that there are always 5 cars in each garage prior to the merging and each garage is already sorted.

Example 1:
Enter the order for garage 1: 1 1 2 3 4
Enter the order for garage 2: 2 2 3 4 5

The final merged order is:
1 1 2 2 2 3 3 4 4 5

Example 2:
Enter the order for garage 1: 1 2 3 4 5
Enter the order for garage 2: 5 6 7 8 9

The final merged order is:
1 2 3 4 5 5 6 7 8 9


Enter the order for garage 1: 1 1 1 1 1
Enter the order for garage 2: 1 1 1 1 1

The final merged order is:
1 1 1 1 1 1 1 1 1 1

Enter the order for garage 1: 5 4 3 2 1
Enter the order for garage 2: 1 2 3 4 5

The final merged order is:
1 1 2 2 3 3 4 4 5 5

Enter the order for garage 1: 9 8 7 6 5 
Enter the order for garage 2: 4 3 2 1 0

The final merged order is:
0 1 2 3 4 5 6 7 8 9

Enter the order for garage 1: 1 2 3 4 5
Enter the order for garage 2: 9 8 7 6 5

The final merged order is:
1 2 3 4 5 5 6 7 8 9

Enter the order for garage 1: 2 3 2 3 3
Enter the order for garage 2: 2 3 3 2 2

The final merged order is:
2 2 2 2 2 3 3 3 3 3

// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstdlib>

#define F(i, a, b) for ( int i = (a); i < (b); i++ )

int compare (const void *a, const void *b)
{
    int *x = (int *) a;
    int *y = (int *) b;
    
    return *x - *y;
}

int main (int argc, char *argv [])
{
    int array [10];
    
    printf ("Enter the order for garage 1: ");
    F (i, 0, 5) scanf ("%d", &array [i]);
    
    printf ("Enter the order for garage 2: ");
    F (i, 5, 10) scanf ("%d", &array [i] );
    
    qsort (array, 10, sizeof (int), compare);
    
    printf ("\nThe final merged order is:\n");
    
    F (i, 0, 10) {
        printf ("%d", array [i]);
        if ( i < 9 ) printf (" ");
    }
    
    printf ("\n");
    

    return 0;
}

// @END_OF_SOURCE_CODE

2 thoughts on “UAB – 2006: Problem 1: Merging at the Brick Yard

  1. class CarMerge {
    private static int LIST_LENGTH = 5;
    String mergeCars(List list1, List list2){
    String result = “”;
    int index1 = 0;
    int index2 = 0;
    while (index1 < LIST_LENGTH || index2 = LIST_LENGTH) {
    result += getRest(list2, index2);
    break;
    }
    if (index2 >= LIST_LENGTH) {
    result += getRest(list1, index1);
    break;
    }
    Integer current1 = list1.get(index1);
    Integer current2 = list2.get(index2);
    if (Objects.equals(current1, current2)) {
    result += current1.toString();
    result += current2.toString();
    index1++;
    index2++;
    } else if (current1 < current2) {
    result += current1;
    index1++;
    } else {
    result += current2;
    index2++;
    }
    }
    System.out.println(result);
    return result;
    }

    private String getRest(List list, int index) {
    String result = “”;
    while (index < LIST_LENGTH) {
    result += list.get(index);
    index++;
    }
    return result;
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s