Beruflich Dokumente
Kultur Dokumente
i
1 Vote
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:
https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 1/5
10/23/2017 UAB – 2006: Problem 1: Merging at the Brick Yard | Solved Programming Problems
Example 1:
Enter the order for garage 1: 1 1 2 3 4
Enter the order for garage 2: 2 2 3 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
1 // @BEGIN_OF_SOURCE_CODE
https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 2/5
10/23/2017 UAB – 2006: Problem 1: Merging at the Brick Yard | Solved Programming Problems
1 // @BEGIN_OF_SOURCE_CODE
2
3 #include <cstdio>
4 #include <cstdlib>
5
6 #define F(i, a, b) for ( int i = (a); i < (b); i++ )
7
8 int compare (const void *a, const void *b)
9 {
10 int *x = (int *) a;
11 int *y = (int *) b;
12
13 return *x - *y;
14 }
15
16 int main (int argc, char *argv [])
17 {
18 int array [10];
19
20 printf ("Enter the order for garage 1: ");
21 F (i, 0, 5) scanf ("%d", &array [i]);
22
23 printf ("Enter the order for garage 2: ");
24 F (i, 5, 10) scanf ("%d", &array [i] );
25
26 qsort (array, 10, sizeof (int), compare);
27
28 printf ("\nThe final merged order is:\n");
29
30 F (i, 0, 10) {
31 printf ("%d", array [i]);
32 if ( i < 9 ) printf (" ");
33 }
34
35 printf ("\n");
36
37
38 return 0;
39 }
40
41 // @END_OF_SOURCE_CODE
https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 3/5
10/23/2017 UAB – 2006: Problem 1: Merging at the Brick Yard | Solved Programming Problems
Advertisements
Easy Problems
1. fokoruddin
DECEMBER 31, 2011 AT 6:09 PM
well
i
Rate This
2. haughki
SEPTEMBER 10, 2016 AT 6:55 AM
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);
https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 4/5
10/23/2017 UAB – 2006: Problem 1: Merging at the Brick Yard | Solved Programming Problems
i
Rate This
https://tausiq.wordpress.com/2009/09/16/uab-2006-problem-1-merging-at-the-brick-yard/ 5/5