-Declare three arrays of integers: --array one of size n --array two of size m -
ID: 3639207 • Letter: #
Question
-Declare three arrays of integers:--array one of size n
--array two of size m
--array three of size k
k = n + m and n is different from m
a-Code a function that prompts the user to enter integers to populate each of array 1 and array 2.
b-Code a function that accepts array 1 and 2 as parameters (among other info) this function should merge array 1 and 2 onto array 3.
c-Code a function that display the contents of array1, array2, and the merged array 3.
d-code another function that prompt the user to enter an integer to search for in array 3. You must display if the integer is found and where it is found: assume no duplicate exist
Important! your program must be fully modular and well documented., and you must implement only the techniques covered so far (as of last session). Both your inpuit and output should be processed in a very elegant way (format/messages...etc). The overall design is up to you.
Extra credits: implement a routine that would prevent the user from entering duplicate (array 1 plus array2 should have no duplicated integers)
Explanation / Answer
please rate - thanks
#include<stdio.h>
#include<conio.h>
int merge(int[], int , int[], int , int[] );
int fill(int[], int, int[],int,int);
void print(int[],int,char[]);
void populate(int[],int,char);
void search(int[],int);
int main()
{int A[5],B[3],C[20];
int asize=5,bsize=3,csize=0;
populate(A,asize,'A');
populate(B,bsize,'B');
csize=merge(A,asize,B, bsize,C);
print(A,asize,"Original Array 1");
print(B,bsize,"Original Array 2");
print(C,csize,"Merged Array");
search(C,csize);
getch();
return 0;
}
void search(int A[],int len)
{int val,i;
printf("%d ",len);
printf("What would you like to search for? ");
scanf("%d",&val);
for(i=0;i<len;i++)
{if(A[i]==val)
{ printf("%d found at index location %d ",val,i);
return;
}
}
printf("%d not found in the array ",val);
}
void populate(int A[],int len,char name)
{int i;
printf("Enter values for array %c ",name);
for(i=0;i<len;i++)
{printf("Enter element %d: ",i+1);
scanf("%d",&A[i]);
}
}
void print(int A[], int len,char mess[])
{int i;
printf("%s ",mess);
for(i=0;i<len;i++)
printf("%d ",A[i]);
printf(" ");
return;
}
int merge(int L[], int lsize, int M[], int msize, int res[])
{int lpt=0,mpt=0,rsize=0,same=0;
while(rsize<(lsize+msize)-same)
{if(L[lpt]<M[mpt])
{res[rsize++]=L[lpt++];
if(lpt==lsize)
rsize=fill(res,rsize,M,msize,mpt);
}
else if(L[lpt]>M[mpt])
{res[rsize++]=M[mpt++];
if(mpt==msize)
rsize=fill(res,rsize,L,lsize,lpt);
}
else
{res[rsize++]=M[mpt++];
same++;
lpt++;
if(mpt==msize)
rsize=fill(res,rsize,L,lsize,lpt);
if(lpt==lsize)
rsize=fill(res,rsize,M,msize,mpt);
}
}
return rsize;
}
int fill( int res[], int rsize, int A[], int isize,int pt)
{int r=rsize;
while(pt<isize)
res[r++]=A[pt++];
return r;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.