Q:

Ruby program to merge two integer arrays without using library function

belongs to collection: Ruby Arrays Programs

0

In this program, we will create two arrays of integers and then we will merge both arrays without using the library function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to merge two integer arrays without using the library function is given below. The given program is compiled and executed successfully.

# Ruby program to merge two integer arrays without 
# using library function

arr1 = [10,20,30,40,50];
arr2 = [60,70,80];
arr3 = Array.new(8);

count=0;
count1=0;

while(count<8)
    if(count<arr1.size)
        arr3[count]=arr1[count];
    else
        arr3[count]=arr2[count1];
        count1=count1 + 1;
    end
    count = count + 1;
end

print "Merged array: \n";
i=0;
while(i<8)
    print arr3[i]," ";
    i=i+1;
end

Output:

Merged array: 
10 20 30 40 50 60 70 80 

Explanation:

In the above program, we created two arrays of integer elements. Then we merged both arrays and assigned the result to the arr3 without using the library function. After that, we printed the merged array.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to read and print two-dimensional arr... >>
<< Ruby program to insert an item into the array with...