Go Program to Find the Largest Element in an Array

Introduction

Finding the largest element in an array is a common task in programming. This involves iterating through the array and comparing each element to determine the maximum value. This guide will demonstrate how to write a Go program that identifies the largest element in 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.
  • Finds and displays the largest element in the array.

Example:

  • Input: Array elements: [1, 3, 7, 2, 5]
  • Output: Largest element: 7

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. Find the Largest Element: Use a loop to iterate through the array and find the largest element.
  6. Display the Largest Element: Use fmt.Println to display the largest element.

Go Program

package main

import "fmt"

/**
 * Go Program to Find the Largest Element in 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: Find the largest element
    largest := array[0]
    for _, value := range array {
        if value > largest {
            largest = value
        }
    }

    // Step 6: Display the largest element
    fmt.Println("Largest element:", largest)
}

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: Find the Largest Element

  • The program initializes largest with the first element of the array. It then uses a for loop with the range keyword to iterate over the array, comparing each element with largest. If an element is greater than largest, the largest variable is updated with that element.

Step 6: Display the Largest Element

  • The largest element found 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
3
7
2
5
Largest element: 7

Example 2:

Enter the number of elements in the array: 4
Enter the elements of the array:
15
42
7
10
Largest element: 42

Conclusion

This Go program demonstrates how to find the largest element in an array by iterating through the array and comparing each element. It covers basic programming concepts such as arrays, loops, and conditional statements. This example is useful for beginners learning Go programming and understanding how to perform comparisons on array elements to identify the maximum value.

Leave a Comment

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

Scroll to Top