What is simple n short program of union of two array in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is simple n short program of union of two array in c

25th Jun 2018, 8:45 PM
Gauri Thakare
Gauri Thakare - avatar
5 Answers
+ 4
This is the basic algorithm: Given sorted arrays of known length, result array could be allocated as the sum of the lengths. Declare three indexes all zero (one for each array.) While there are elements to put into the output, process them. If a and b both still have elements to process, compare them. If equal, move one to c and increment both indexes of a & b. If a's less, move it and increment it's index. Otherwise, move b's and increment it's index. Otherwise, one of the arrays is done so copy the rest of the other. Create a C source and link it here, if you need more help. I'll comment step by step updates to you to complete the working code.
25th Jun 2018, 9:23 PM
John Wells
John Wells - avatar
+ 3
I'm not exactly sure what you are trying to ask. Can you give a sample input and output?
25th Jun 2018, 8:52 PM
John Wells
John Wells - avatar
+ 2
How do original arrays get created? Hard coded or input? Do you know enough C to code it, if you were told how?
25th Jun 2018, 9:06 PM
John Wells
John Wells - avatar
+ 1
if a[1,2,3] and b[3,4,5] then output should be c[1,2,3,4,5]
25th Jun 2018, 8:54 PM
Gauri Thakare
Gauri Thakare - avatar
0
sir,here I just take a simple pre define array... once I get the logic I will try to take the input from user
25th Jun 2018, 9:10 PM
Gauri Thakare
Gauri Thakare - avatar