Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

find_smallest_number_in_an_array #4761

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Function to find the smallest element in a matrix
find_smallest_element <- function(matrix_data) {
min_value <- min(matrix_data)
return(min_value)
}

# User input for matrix dimensions
rows <- as.integer(readline(prompt = "Enter the number of rows: "))
cols <- as.integer(readline(prompt = "Enter the number of columns: "))

# Create an empty matrix to store user input
matrix_input <- matrix(0, nrow = rows, ncol = cols)

# Input matrix elements
for (i in 1:rows) {
for (j in 1:cols) {
matrix_input[i, j] <- as.integer(readline(prompt = paste("Enter element at [", i, ",", j, "]: ")))
}
}

# Display input matrix
cat("Entered matrix:\n")
print(matrix_input)

# Find the smallest element in the matrix
smallest_element <- find_smallest_element(matrix_input)

# Display the smallest element
cat("\nThe smallest element in the matrix is:", smallest_element, "\n")
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Function to find the smallest number in an array
find_smallest_in_array <- function(arr) {
min_value <- min(arr)
return(min_value)
}

# Example array
arr <- c(9, 4, 7, 2, 5, 10, 8, 3, 6)
# Replace this example array with your own data

# Find the smallest number in the array
smallest_number <- find_smallest_in_array(arr)

# Display the smallest number
cat("The smallest number in the array is:", smallest_number, "\n")