0% found this document useful (0 votes)
12 views

R Programming

4th semester R programming assignment

Uploaded by

avishekbin7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

R Programming

4th semester R programming assignment

Uploaded by

avishekbin7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Program to print 'Hello World' to the screen


R
Copy code
# Program to print 'Hello World'
print("Hello World")

2. Program that asks the user for a number n and prints the sum of the
numbers 1 to n
R
Copy code
# Program to sum numbers from 1 to n
sum_to_n <- function() {
n <- as.integer(readline(prompt="Enter a number: "))
sum <- sum(1:n)
print(paste("The sum of numbers from 1 to", n, "is", sum))
}

sum_to_n()

3. Program that prints a multiplication table for numbers up to 12


R
Copy code
# Program to print multiplication table up to 12
multiplication_table <- function() {
for (i in 1:12) {
for (j in 1:12) {
cat(sprintf("%4d", i * j))
}
cat("\n")
}
}

multiplication_table()

4. Function that returns the largest element in a list


R
Copy code
# Function to find the largest element in a list
largest_element <- function(lst) {
return(max(lst))
}

# Example usage
lst <- c(3, 5, 7, 2, 8, 1)
print(largest_element(lst))

5. Function that computes the running total of a list


R
Copy code
# Function to compute running total of a list
running_total <- function(lst) {
return(cumsum(lst))
}

# Example usage
lst <- c(1, 2, 3, 4, 5)
print(running_total(lst))

6. Function that tests whether a string is a palindrome


R
Copy code
# Function to test if a string is a palindrome
is_palindrome <- function(str) {
return(str == paste(rev(strsplit(str, NULL)[[1]]), collapse=""))
}

# Example usage
str <- "radar"
print(is_palindrome(str))

7. Implement the following sorting algorithms: Selection sort, Insertion sort,


Bubble Sort

Selection Sort

R
Copy code
# Selection Sort implementation
selection_sort <- function(lst) {
n <- length(lst)
for (i in 1:(n-1)) {
min_index <- i
for (j in (i+1):n) {
if (lst[j] < lst[min_index]) {
min_index <- j
}
}
if (min_index != i) {
temp <- lst[i]
lst[i] <- lst[min_index]
lst[min_index] <- temp
}
}
return(lst)
}

# Example usage
lst <- c(64, 25, 12, 22, 11)
print(selection_sort(lst))

Insertion Sort

R
Copy code
# Insertion Sort implementation
insertion_sort <- function(lst) {
n <- length(lst)
for (i in 2:n) {
key <- lst[i]
j <- i - 1
while (j > 0 && lst[j] > key) {
lst[j + 1] <- lst[j]
j <- j - 1
}
lst[j + 1] <- key
}
return(lst)
}

# Example usage
lst <- c(12, 11, 13, 5, 6)
print(insertion_sort(lst))

Bubble Sort

R
Copy code
# Bubble Sort implementation
bubble_sort <- function(lst) {
n <- length(lst)
for (i in 1:(n-1)) {
for (j in 1:(n-i)) {
if (lst[j] > lst[j + 1]) {
temp <- lst[j]
lst[j] <- lst[j + 1]
lst[j + 1] <- temp
}
}
}
return(lst)
}

# Example usage
lst <- c(5, 1, 4, 2, 8)
print(bubble_sort(lst))

8. Implement linear search


R
Copy code
# Linear Search implementation
linear_search <- function(lst, x) {
for (i in 1:length(lst)) {
if (lst[i] == x) {
return(i)
}
}
return(-1) # Return -1 if the element is not found
}

# Example usage
lst <- c(2, 3, 4, 10, 40)
x <- 10
print(linear_search(lst, x))

9. Implement binary search


R
Copy code
# Binary Search implementation
binary_search <- function(lst, x) {
low <- 1
high <- length(lst)
while (low <= high) {
mid <- floor((low + high) / 2)
if (lst[mid] == x) {
return(mid)
} else if (lst[mid] < x) {
low <- mid + 1
} else {
high <- mid - 1
}
}
return(-1) # Return -1 if the element is not found
}

# Example usage
lst <- c(2, 3, 4, 10, 40)
x <- 10
print(binary_search(lst, x))

10. Implement matrices addition, subtraction, and multiplication

Matrix Addition

R
Copy code
# Matrix Addition implementation
matrix_addition <- function(mat1, mat2) {
return(mat1 + mat2)
}

# Example usage
mat1 <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2)
mat2 <- matrix(c(5, 6, 7, 8), nrow=2, ncol=2)
print(matrix_addition(mat1, mat2))

Matrix Subtraction

R
Copy code
# Matrix Subtraction implementation
matrix_subtraction <- function(mat1, mat2) {
return(mat1 - mat2)
}

# Example usage
mat1 <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2)
mat2 <- matrix(c(5, 6, 7, 8), nrow=2, ncol=2)
print(matrix_subtraction(mat1, mat2))

Matrix Multiplication

R
Copy code
# Matrix Multiplication implementation
matrix_multiplication <- function(mat1, mat2) {
return(mat1 %*% mat2)
}

# Example usage
mat1 <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2)
mat2 <- matrix(c(5, 6, 7, 8), nrow=2, ncol=2)
print(matrix_multiplication(mat1, mat2))

You might also like