Change this program by getting inputs from the user and print the result
Program :
# Program to demonstrate basic data types in R
# Numeric (double)
num_value <- 10.5
print(paste("Numeric:", num_value))
print(typeof(num_value))
# Integer
int_value <- as.integer(10)
print(paste("Integer:", int_value))
print(typeof(int_value))
# Character (String)
char_value <- "Hello, R!"
print(paste("Character:", char_value))
print(typeof(char_value))
# Logical (Boolean)
bool_value <- TRUE
print(paste("Logical:", bool_value))
print(typeof(bool_value))
# Complex Number
complex_value <- 3 + 2i
print(paste("Complex:", complex_value))
print(typeof(complex_value))
# Raw (Converting a string to raw bytes)
raw_value <- charToRaw("R Programming")
print("Raw:")
print(raw_value)
print(typeof(raw_value))
Output:
Result:
You have to get inputs for the array from the users for matrix dimension and matrix values in the
following programs:
Program2;
# Binary Search Function
binary_search <- function(arr, target) {
low <- 1 # R arrays are 1-based
high <- length(arr)
while (low <= high) {
mid <- floor((low + high) / 2) # Compute middle index
if (arr[mid] == target) {
return(mid) # Return position (1-based index)
} else if (arr[mid] > target) {
high <- mid - 1 # Search left half
} else {
low <- mid + 1 # Search right half
}
return(-1) # Element not found
}
# Example Usage
arr <- c(2, 5, 8, 12, 16, 23, 38, 45, 56, 72) # Sorted array
target <- 23
# Call binary search function
result <- binary_search(arr, target)
# Display Result
if (result != -1) {
cat("Element found at position:", result, "\n")
} else {
cat("Element not found in the array.\n")
Output:
Result:
Program3:
# Define two matrices using arrays
matrix1 <- array(c(1, 2, 3, 4, 5, 6), dim = c(2, 3))
matrix2 <- array(c(6, 5, 4, 3, 2, 1), dim = c(2, 3))
# Matrix Addition
matrix_addition <- matrix1 + matrix2
# Display Result
cat("Matrix Addition Result:\n")
print(matrix_addition)
# Define two matrices for multiplication
matrixA <- array(c(1, 2, 3, 4, 5, 6), dim = c(2, 3))
matrixB <- array(c(1, 2, 3, 4, 5, 6), dim = c(3, 2))
# Matrix Multiplication
matrix_multiplication <- matrixA %*% matrixB
# Display Result
cat("Matrix Multiplication Result:\n")
print(matrix_multiplication)
Output:
Result: