Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify project 2, #1, merge.c so that it includes the following function. Name your program merge2.c. void merge(int a1[], int size_a1, int a2[], int size_a2,

Modify project 2, #1, merge.c so that it includes the following function. Name your program merge2.c.

void merge(int a1[], int size_a1, int a2[], int size_a2, int unique[], int * size_u);

The merge function finds all the unique elements of a1 and a2 store them in array unique. Parameter size_a1 contains the size of the array a1 and parameter size_a2 contains the size of the array a2. The sixth parameter size_u points to a variable in which the function will store the numbers of unique elements of the a1 and a2.

In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, declare the array unique with the largest possible size (size of the first array + size of the second array), call the merger function, and then display the array unique using the actual number of elements of the array (the variable pointed by size_u).

This program will be graded based on whether the required functionality was implemented correctly instead of whether it produces the correct output, for the functionality part (80% of the grade).

Example input/output #1: Enter the length of array 1: 5 Enter the elements of the array: 8 4 1 3 9 Enter the length of array 2: 3 Enter the elements of the array: 5 4 8 Output array: 8 4 1 3 9 5

Example input/output #2: Enter the length of array 1: 4 Enter the elements of the array: 3 4 1 7 Enter the length of array 2: 2 Enter the elements of the array: 6 9 Output array: 3 4 1 7 6 9

Here's my code from merge.c:

#include #include

void merge_array(int arr1[], int size1, int arr2[], int size2);

int main(void){

int first_arr[100]; int second_arr[100]; int size1, size2; int i;

// asks user for the size of the first array printf("Enter the length of array 1: "); scanf("%d", &size1); printf("Enter the elements of the array: "); for(i = 0; i < size1; i++){ // scan the first array of elements scanf("%d", &first_arr[i]); } // asks user for the size of the second array printf("Enter the length of the array 2: "); scanf("%d", &size2); printf("Enter the elements of the array: "); for(i = 0; i < size2; i++){ // scan the second array of elements scanf("%d", &second_arr[i]); } printf("Output array: "); // call the merge_array function merge_array(first_arr, size1, second_arr, size2); return 0; }

// The search function is used to make sure that we dont // copy any duplicate elements into arr3 int search(int arr3[], int size1, int size2){

int i;

for(i = 0; i < size1; i++){ if(arr3[i] == size2){ return i; } } return -1; }

void merge_array(int arr1[], int size1, int arr2[], int size2){ int arr3[size1 + size2]; int i, k = 0; // loop through every element in the array // and remove any duplicate elements that // are found in both arrays. for(i = 0; i < size1; i++){

arr3[k] = arr1[i]; k++; } for(i = 0; i < size2; i++){ // search the elements in arr3 // if the elements are not present // then insert the element into arr3 if(search(arr3, size1, arr2[i]) == -1){ // copy the elements from arr3 into arr3 arr3[k] = arr2[i]; k++; } }

for(i = 0; i < k; i++){ printf("%d ", arr3[i]); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logic In Databases International Workshop Lid 96 San Miniato Italy July 1 2 1996 Proceedings Lncs 1154

Authors: Dino Pedreschi ,Carlo Zaniolo

1st Edition

3540618147, 978-3540618140

More Books

Students also viewed these Databases questions