Introduction
Merging two arrays is a common operation where elements from both arrays are combined into a single array. This guide will demonstrate how to write a Go program that merges two arrays into one.
Problem Statement
Create a Go program that:
- Prompts the user to enter the number of elements in two arrays.
- Takes input for the elements of both arrays.
- Merges the two arrays into a single array.
- Displays the merged array.
Example:
- Input: Array 1:
[1, 3, 5], Array 2:[2, 4, 6] - Output: Merged array:
[1, 3, 5, 2, 4, 6]
Solution Steps
- Import the fmt Package: Use
import "fmt"to include the fmt package for formatted I/O operations. - Write the Main Function: Define the
mainfunction, which is the entry point of every Go program. - Input the Number of Elements: Use
fmt.Scanlnto take input from the user for the number of elements in both arrays. - Input the Array Elements: Use loops to input the elements of both arrays from the user.
- Merge the Arrays: Concatenate the two arrays into a single array.
- Display the Merged Array: Use
fmt.Printlnto display the merged array.
Go Program
package main
import "fmt"
/**
* Go Program to Merge Two Arrays
* Author: https://www.javaguides.net/
*/
func main() {
// Step 1: Declare variables to hold the number of elements in the arrays
var n1, n2 int
// Step 2: Prompt the user to enter the number of elements in the first array
fmt.Print("Enter the number of elements in the first array: ")
fmt.Scanln(&n1)
// Step 3: Declare the first array and input its elements
array1 := make([]int, n1)
fmt.Println("Enter the elements of the first array:")
for i := 0; i < n1; i++ {
fmt.Scanln(&array1[i])
}
// Step 4: Prompt the user to enter the number of elements in the second array
fmt.Print("Enter the number of elements in the second array: ")
fmt.Scanln(&n2)
// Step 5: Declare the second array and input its elements
array2 := make([]int, n2)
fmt.Println("Enter the elements of the second array:")
for i := 0; i < n2; i++ {
fmt.Scanln(&array2[i])
}
// Step 6: Merge the two arrays
mergedArray := append(array1, array2...)
// Step 7: Display the merged array
fmt.Println("Merged array:", mergedArray)
}
Explanation
Step 1: Declare Variables
- The variables
n1andn2are declared to store the number of elements in the first and second arrays, respectively.
Step 2: Input the Number of Elements for the First Array
- The program prompts the user to enter the number of elements in the first array using
fmt.Print. Thefmt.Scanlnfunction reads the input and stores it in then1variable.
Step 3: Declare and Input the Elements of the First Array
- An array
array1is created usingmaketo holdn1integer elements. The program then uses aforloop to prompt the user to enter each element of the array, storing the input in the corresponding index ofarray1.
Step 4: Input the Number of Elements for the Second Array
- The program repeats the process for the second array by prompting the user to enter the number of elements using
fmt.Printandfmt.Scanln, storing the result in then2variable.
Step 5: Declare and Input the Elements of the Second Array
- An array
array2is created usingmaketo holdn2integer elements. The program uses anotherforloop to prompt the user to enter each element ofarray2.
Step 6: Merge the Two Arrays
- The
appendfunction is used to concatenatearray2toarray1, creating themergedArray.
Step 7: Display the Merged Array
- The merged array is displayed using
fmt.Println.
Output Example
Example 1:
Enter the number of elements in the first array: 3
Enter the elements of the first array:
1
3
5
Enter the number of elements in the second array: 3
Enter the elements of the second array:
2
4
6
Merged array: [1 3 5 2 4 6]
Example 2:
Enter the number of elements in the first array: 4
Enter the elements of the first array:
10
20
30
40
Enter the number of elements in the second array: 2
Enter the elements of the second array:
50
60
Merged array: [10 20 30 40 50 60]
Conclusion
This Go program demonstrates how to merge two arrays by concatenating them into a single array. It covers basic programming concepts such as arrays, loops, and array manipulation. This example is useful for beginners learning Go programming and understanding how to work with multiple arrays and combine their elements.