Ruby program to merge two integer arrays without using library function

Ruby Example: Write a program to merge two integer arrays without using library function.
Submitted by Nidhi, on January 22, 2022

Problem Solution:

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

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.

Ruby Arrays Programs »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.