Recitation Exercise 03 Solution

Recitation Exercise 03 Solution

One-D Arrays and functions: Complete the following program. You will mainly implement SELECTION_SORT (...), MERGE(...), and PRINT_ARRAY(...) functions!

 

main()

{

/* 1.         Declare three integer arrays as follows */

 

int        a[50], b[70], c[120];

 

/* 2. implement a function set_array_rand(int x[], int n) and call it to generate the values in array a and b randomly. */

 

set_array_rand(a, 50);

set_array_rand(b, 70);

 

/* 3. using the SELECTION_SORT(double x[], int n) function (see ch02.ppt), sort the elements in a and b arrays. */

 

SELECTION_SORT(a, 50);

 

SELECTION_SORT(b, 70);

 

/* 4. implement a MERGE function and call it as follows to merge the values in arrays a and b into array c such that the values in c will be sorted after merging */

 

MERGE(a, 50, b, 70, c, 120);

 

/* 5. print the values in array c */ PRINT_ARRAY("Array c", c, 120);

}

 

 

void set_array_rand(int x[], int n)

{

 

/* 1. randomly generate elements of x array, e.g, */ for(int i=0; i< n; i++)

 

x[i] = rand_int(30, 100);

 

}

 

int rand_int(int a,int b)

{

 

return rand()%(b-a+1) + a;

}

/* YOUR CODE */

 

void SELECTION_SORT(int x[], int n)

{

int k,j,m;

double temp;

 

 

}

 

void MERGE(int a[], int na, int b[], int nb, int c[], int nc) {

 

/*      merge the values in a and b into c while keeping the values sorted. For example, suppose we have the following two Arrays a = { 3, 7, 9, 12} and b = {4, 5, 10}

 

When we merge these two arrays, we will get c = {3, 4, 5, 7, 9, 10, 12}

*/

 

 

 

 

}

 

PRINT_ARRAY(char *name, int x[], int nx)

 

{

 

/* YOUR CODE */

 

}

 

/*   

 

1.   Create a directory called LASTNAME_Recitation03 and do all your work under that directory

 

2.      First implement your program which can be named as rec03.c

 

3.     Then compile and run it. Copy/paste the result in an output file, say out03.txt.

 

3.     Finally zip your LASTNAME_Recitation03 directory as a single file LASNAME_Recitation03.zip and Go to BB Learn to submit it as attachment before the deadline.

 
Powered by