0% found this document useful (0 votes)
8 views8 pages

Activity 02 (Vu22csci0100119)

Uploaded by

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

Activity 02 (Vu22csci0100119)

Uploaded by

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

ACTIVITY-2

VU22CSCI0100119

P.SUMANTH

VECTORS
 AIM: - write an R program to create a vector which contains 10
elements check whether an element exists in a vector or not and
display the position or index of the element.
PROGRAM:-
# Define initial variables
Registration_no <- "VU22CSCI0100119"
Name <- "P.SUMANTH"

# Print Registration No and Name


print(Registration_no)
print(Name)

# Initialize an empty vector


v <- c()

# Loop to read elements into the vector


for (i in 1:10) {
ele <- readline("Enter element: ")
ele <- as.numeric(ele)
v <- append(v, ele) # Append the element to the vector
}

# Read value to check in the vector


value <- readline("Enter element to check: ")
value <- as.numeric(value)

# Check if the value is in the vector and print the index or a message
if (is.element(value, v)) {
cat("Index:", which(v == value), "\n")
} else {
print("Element not found")
}

OUTPUT:-

 AIM: - write an R program to create a vector prime number from


1to200 and return the number of elements in a vector. Delete the 6 th
position element and print the final vector.
PROGRAM:-
# Define initial variables
Registration_no <- "VU22CSCI0100119"
Name <- "P.SUMANTH"

# Print Registration No and Name


print(Registration_no)
print(Name)

# Initialize an empty vector to store prime numbers


v <- c()

# Loop to find prime numbers from 1 to 200


for (i in 1:200) {
c <- 0 # Counter for divisors
for (j in 1:i) {
if (i %% j == 0) {
c <- c + 1 # Increment counter for each divisor
}
}
if (c == 2) { # A prime number has exactly two divisors: 1 and itself
v <- append(v, i) # Append the prime number to the vector
}
}

# Print the length of the vector and its contents


cat("Length of vector:", length(v), "\n")
print(v)

# Remove the sixth element from the vector if it exists


if (length(v) >= 6) {
v <- v[-c(6)]
}

# Print the modified vector


print(v)

OUTPUT:-
 AIM: - write a R program to display the elements in columns in a 2-D
mateix elements in columns in a 2-D matrix using vectors.
PROGRAM:-
# Define initial variables
Registration_no <- "VU22CSCI0100119"
Name <- "P.SUMANTH"

# Print Registration No and Name


print(Registration_no)
print(Name)

# Initialize a vector
v <- c(2, 4, 6, 8, 10, 12)

# Create a matrix from the vector


m <- matrix(v, nrow = 2, ncol = 3)

# Print the matrix


cat("MATRIX:\n")
print(m)

# Access and print specific elements and rows/columns of the matrix


cat("\nElement at 3rd column and 2nd row:", m[2, 3], "\n")
cat("\nElement at 1st row:", m[1, ], "\n")
cat("\nElement at 2nd row:", m[2, ], "\n")
cat("\nElement at 1st column:", m[, 1], "\n")
cat("\nElement at 2nd column:", m[, 2], "\n")
cat("\nElement at 3rd column:", m[, 3], "\n")
OUTPUT:-

 AIM: - write a R program to find the length of a vector by using


function length().
PROGRAM:-

# Define initial variables


Registration_no <- "VU22CSCI0100119"
Name <- "P.SUMANTH"

# Print Registration No and Name


print(Registration_no)
print(Name)

# Initialize vectors
x <- c(218, 217, 213, 214, 215)
cat("Length of x:", length(x), "\n")
y <- c("sowmya", "kala", "prasanna", "mani")
cat("Length of y:", length(y), "\n")

z <- c(TRUE, FALSE, TRUE, TRUE)


cat("Length of z:", length(z), "\n")
OUTPUT:-

 AIM: -write a R program to perform sorting elements of a vector.


PROGRAM:-

# Define initial variables


Registration_no <- "VU22CSCI0100119"
Name <- "P.SUMANTH"

# Print Registration No and Name


print(Registration_no)
print(Name)

# Initialize vector X
X <- c(8, 2, 7, 1, 11, 2)

# Sort in ascending order


A <- sort(X)
cat('Ascending order:', A, '\n')

# Sort in descending order


B <- sort(X, decreasing = TRUE)
cat('Descending order:', B)
OUTPUT:-

 AIM: -write a R program to know the datatype that stored in an


vector.
PROGRAM:-
# Define initial variables
Registration_no <- "VU22CSCI0100119"
Name <- "P.SUMANTH"

# Print Registration No and Name


print(Registration_no)
print(Name)

# Initialize vector v1
v1 <- c(4, 5, 6, 7)
cat("Type of v1:", typeof(v1), "\n")

# Initialize vector v2
v2 <- c('geeks', '2', 'hello')
cat("Type of v2:", typeof(v2), "\n")

# Initialize vector v3
v3 <- c(TRUE, FALSE, TRUE, NA)
cat("Type of v3:", typeof(v3), "\n")

OUTPUT:-

You might also like