🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Understanding the range of values in a dataset can provide insights into the data's characteristics. Two fundamental metrics that help in understanding this range are the maximum and minimum values. In this blog post, we will walk through a Go program that identifies these two values from a given array.
Program Steps
1. Import Necessary Packages: We'll kick things off by importing the fmt package. This package offers us the tools for input and output operations.
2. Define the Functions to Locate Maximum and Minimum Values: To keep our code clean and readable, we'll define two separate functions, findMax, and findMin, which will return the maximum and minimum values from an array, respectively.
3. Main Function: Within the main function, we'll define our array, and then utilize our two functions to determine its max and min values. The results will be displayed to the user.
Code Program
package main
import "fmt"
// Function to find the maximum value in an array.
func findMax(arr []int) int {
maxVal := arr[0]
for _, value := range arr {
if value > maxVal {
maxVal = value
}
}
return maxVal
}
// Function to find the minimum value in an array.
func findMin(arr []int) int {
minVal := arr[0]
for _, value := range arr {
if value < minVal {
minVal = value
}
}
return minVal
}
// Main function to execute the program.
func main() {
array := []int{64, 34, 25, 12, 22, 11, 90}
fmt.Println("Array:", array)
maxValue := findMax(array)
minValue := findMin(array)
fmt.Println("Maximum Value:", maxValue)
fmt.Println("Minimum Value:", minValue)
}
Output:
Array: [64 34 25 12 22 11 90] Maximum Value: 90 Minimum Value: 11
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment