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
- 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 the array. - Input the Array Elements: Use a loop to input the elements of the array from the user.
- Find the Largest Element: Use a loop to iterate through the array and find the largest element.
- Display the Largest Element: Use
fmt.Printlnto 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
nis 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. Thefmt.Scanlnfunction reads the input and stores it in thenvariable.
Step 3: Declare the Array
- An array
arrayis created usingmaketo holdninteger elements.
Step 4: Input the Array Elements
- The program uses a
forloop to prompt the user to enter each element of the array, storing the input in the corresponding index ofarray.
Step 5: Find the Largest Element
- The program initializes
largestwith the first element of the array. It then uses aforloop with therangekeyword to iterate over the array, comparing each element withlargest. If an element is greater thanlargest, thelargestvariable 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.