Q1

Write R scripts using the selection flow control for each of the following.

  1. Determine the biggest number among three numbers.

  2. A switch statement that displays Sunday, Monday, …, Saturday, if the number is 0, 1, … 6.

  3. Determine whether the year is a leap year. A leap year is divisible by 4 but not by 100. A leap year is also divisible by 400.

#a
a<- 1
b<- 2
c<- 3
if(a>b&a>c){
  cat("the biggest number is:", a)
}else if(b>a&b>c){
  cat("the biggest number is:", b)
}else{
  cat("the biggest number is:", c)
}
## the biggest number is: 3
#b
n<- c("0", "1", "2", "3", "4", "5", "6")
for (i in n)
  print(
    switch(i,
      "0" = "Sunday",
      "1" = "Monday",
      "2" = "Tuesday",
      "3" = "Wednesday",
      "4" = "Thursday",
      "5" = "Friday",
      "6" = "Saturday"
    )
  )
## [1] "Sunday"
## [1] "Monday"
## [1] "Tuesday"
## [1] "Wednesday"
## [1] "Thursday"
## [1] "Friday"
## [1] "Saturday"
#c
year<- c(2012, 2013, 2014, 4000)
for(i in year)
  if(i%%4==0|i%%400==0){
    cat(i, "is leap year\n")
  }else{
    cat(i, "is not leap year\n")
  }
## 2012 is leap year
## 2013 is not leap year
## 2014 is not leap year
## 4000 is leap year

Q2

Write R scripts using the ifelse() function for each of the following.

  1. Determine the positive and negative number in the vector.

  2. Determine whether a character in the vector is uppercase or lowercase letter.

  3. Compare the numbers from two vectors to determine whether a number is larger than, smaller than or equal to another number.

#a
i<- 10
a<- ifelse(i%%2==0, "positive", "negative")
print(a)
## [1] "positive"
#b
n<- c("a", "b", "c", "A", "B", "C")
for(i in n){
  m<- ifelse(i %in% LETTERS,"uppercase","lowercase")
  cat(i, "is", m, "\n")
}
## a is lowercase 
## b is lowercase 
## c is lowercase 
## A is uppercase 
## B is uppercase 
## C is uppercase
#c
a<- 10
b<- 20
m<- ifelse(a>b, "a is larger than b", ifelse(a<b, "a is smller than b", "a is equal to b"))
print(m)
## [1] "a is smller than b"

Q3

Create an R file named calculator.r that stimulates a simple calculator. It reads two numbers and an operator. If the operator is +, the sum is printed; if it is -, the difference is printed; if it is x, the multiplication is printed; if it is /, the quotient is printed.

#cat("Enter two numbers:", "\n")
#a<-as.numeric(readLines("stdin", 1))
#b<-as.numeric(readLines("stdin", 1))
#cat("Enter operator:")
#sign<-as.character(readLines("stdin", 1))
#if(sign=="+"){
#  cat(a, "+", b, "=", a+b, "\n")
#}else if(sign=="-"){
#  cat(a, "-", b, "=", a-b, "\n")
#}else if(sign=="*"){
#  cat(a, "*", b, "=", a*b, "\n")
#}else{
#  cat(a, "/", b, "=", a/b, "\n")
#}

Q4

Create an R file named circle.r. The script will ask user to enter the radius of a circle and a coordinate point (x, y). Determine whether the point is inside or outside the circle centered at (0, 0).

#cat("Enter radius of a circle:")
#r<-as.numeric(readLines("stdin", 1))
#cat("Enter coordinate x and y:")
#x<-as.numeric(readLines("stdin", 1))
#y<-as.numeric(readLines("stdin", 1))
#if(x*x+y*y<r*r){
#  cat("(", x, ",", y, ")", "is in the circle\n")
#}else{
#  cat("(", x, ",)", y, "is out of the circle\n")
#}

Q5

Write R statements using loop flow control for each of the following

  1. Find the largest integer n so that n3 is less than 2000.

  2. Compute the sum of the series: 1/25+2/24+3/23 … + 25/1 in two decimal places.

  3. Display the first ten values of the Fibonacci sequence. Given the formula f1 = 1, f2 =1, fn = fn-1 + fn-2.

#a
i<- 0
while(i^3<2000){
  i<-i+1
}
print(i-1)
## [1] 12
#b
a<- 1
b<- 25
total<- 0
for(i in c(1:25) ){
  m<- a/b
  total=total+m
  a=a+1
  b=b-1
}
sprintf("%0.2f", total)
## [1] "74.21"
#c
Fibonacci <- numeric(10)
Fibonacci[1] <- Fibonacci[2] <- 1
for (i in 3:10) Fibonacci[i] <- Fibonacci[i - 2] + Fibonacci[i - 1]
print("First 10 Fibonacci numbers:")
## [1] "First 10 Fibonacci numbers:"
print(Fibonacci)
##  [1]  1  1  2  3  5  8 13 21 34 55

Q6

Create an R file named score.r. The script will calculate the minimum, maximum, average and standard deviation (s) of the exam score in a subject. The program will accept the score and quit if negative score is entered.

#a<- numeric(6)
#for(i in c(1:6)){
#  cat("Enter a score:")
#  a[i]<-as.numeric(readLines("stdin", 1))
#}
#cat("Minimun Score", min(a), "\n")
#cat("Maxmimum Score", max(a), "\n")
#cat("Average Score", mean(a), "\n")
#cat("Standard Deviation", sd(a), "\n")

Q7

Create an R file named matrix.r. The script will ask user to enter M and N. Create a matrix with M rows and N columns with random numbers 1-50. Display the matrix and then count the number of odd and even numbers in the matrix.

#cat("Enter M and N:")
#a<-as.numeric(readLines("stdin", 1))
#b<-as.numeric(readLines("stdin", 1))
#c<- sample(1:50, a*b)
#matrix1<- matrix(c, a, b)
#matrix1
#cat("Number of odd numbers in the matrix is", sum(c%%2==1), "\n")
#cat("Number of even numbers in the matrix is", sum(c%%2==0), "\n")