Go Program to Reverse an Array

Introduction

Reversing an array is a common operation in programming where the order of elements in the array is reversed, such that the first element becomes the last, the second becomes the second-last, and so on. This guide will show you how to write a Go program that reverses an array.

Problem Statement

Create a Go program that:

  • Prompts the user to enter the number of elements in an array.
  • Takes input for the elements of the array.
  • Reverses the array.
  • Displays the reversed array.

Example:

  • Input: Array elements: [1, 2, 3, 4, 5]
  • Output: Reversed array: [5, 4, 3, 2, 1]

Solution Steps

  1. Import the fmt Package: Use import "fmt" to include the fmt package for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Input the Number of Elements: Use fmt.Scanln to take input from the user for the number of elements in the array.
  4. Input the Array Elements: Use a loop to input the elements of the array from the user.
  5. Reverse the Array: Implement a loop to swap elements from the beginning and end of the array until the middle is reached.
  6. Display the Reversed Array: Use fmt.Println to display the reversed array.

Go Program

package main

import "fmt"

/**
 * Go Program to Reverse an Array
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare a variable to hold the number of elements in the array
    var n int

    // Step 2: Prompt the user to enter the number of elements
    fmt.Print("Enter the number of elements in the array: ")
    fmt.Scanln(&n)

    // Step 3: Declare an array to hold the elements
    array := make([]int, n)

    // Step 4: Input the elements of the array
    fmt.Println("Enter the elements of the array:")
    for i := 0; i < n; i++ {
        fmt.Scanln(&array[i])
    }

    // Step 5: Reverse the array
    for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
        array[i], array[j] = array[j], array[i]
    }

    // Step 6: Display the reversed array
    fmt.Println("Reversed array:", array)
}

Explanation

Step 1: Declare Variables

  • The variable n is declared to store the number of elements in the array.

Step 2: Input the Number of Elements

  • The program prompts the user to enter the number of elements using fmt.Print. The fmt.Scanln function reads the input and stores it in the n variable.

Step 3: Declare the Array

  • An array array is created using make to hold n integer elements.

Step 4: Input the Array Elements

  • The program uses a for loop to prompt the user to enter each element of the array, storing the input in the corresponding index of array.

Step 5: Reverse the Array

  • The program uses a for loop with two pointers, i and j, to swap elements from the beginning (i) and end (j) of the array until they meet in the middle. This effectively reverses the array.

Step 6: Display the Reversed Array

  • The reversed array is displayed using fmt.Println.

Output Example

Example 1:

Enter the number of elements in the array: 5
Enter the elements of the array:
1
2
3
4
5
Reversed array: [5 4 3 2 1]

Example 2:

Enter the number of elements in the array: 4
Enter the elements of the array:
10
20
30
40
Reversed array: [40 30 20 10]

Conclusion

This Go program demonstrates how to reverse an array by swapping elements from the beginning and end of the array. It covers basic programming concepts such as arrays, loops, and swapping elements. This example is useful for beginners learning Go programming and understanding how to manipulate arrays in Go.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top