- 27th Oct 2021
- 06:03 am
library(infix)
options(warn=-1)
#Exercise 1:
Enter_Hours <- as.numeric(readline(prompt="Enter Hours: ")) Enter_Rate <- as.numeric(readline(prompt="Enter Rate: ")) pay_computation_1 <- function(Enter_Hours, Enter_Rate){ return(Enter_Hours*Enter_Rate) } pay_computation_1(Enter_Hours, Enter_Rate)
#Exercise 2:
pay_computation_2 <- function(){ Enter_Hours <- as.numeric(readline(prompt="Enter Hours: ")) Enter_Rate <- as.numeric(readline(prompt="Enter Rate: ")) if(is.numeric(Enter_Hours)==T & is.numeric(Enter_Rate)==T){ if(is.na(Enter_Hours*Enter_Rate) == T){ return("Error, please enter numeric input") } return((Enter_Hours)*(Enter_Rate)) } } # Now we will test this function :
pay_computation_2()
20
nine
pay_computation_2()
forty
9
#Exercise 3 :
Grade_allocation <- function(){ score <- as.numeric(readline(prompt="Enter score: ")) if(is.na(score*1) == T ) return("Bad score") if(score > 1 || score < 0> return("Bad Score") } if(score >= 0.9) return("A") if(score >= 0.8) return("B") if(score >= 0.7) return("C") if(score >= 0.6) return("D") if(score < 0> } #Example
Grade_allocation()
0.95
Grade_allocation()
perfect
Grade_allocation()
10
Grade_allocation()
0.75
#Exercise 4
summary_1 <- function(){ i = 1 number = c() while(1) { number[i] <- (readline(prompt="Enter a number: ")) if(number[i] == "done"){ break; } if(is.na(as.numeric(number[i])*1) == T){return("Invalid input")} i = i + 1 } number = as.numeric(number)[-length(number)] return( cat(paste("count is:", length(number), "\n" , "total is:", sum(number), "\n", "average is:", mean(number),"\n" ,sep = " ")) ) }
summary_1()
4
5
"bad data"
summary_1()
7
done
#Exercise 5:
summary_2 <- function(){ i = 1 number = c() while(1) { number[i] <- (readline(prompt="Enter a number: ")) if(number[i] == "done"){ break; } if(is.na(as.numeric(number[i])*1) == T){return("Invalid input")} i = i + 1 } number = as.numeric(number)[1:(length(number) - 1)] return( cat(paste("Maximum is:", max(number), "\n" , "Minimum is:", min(number), "\n" ,sep = " ")) ) }
summary_2()
1
1
1
2
3
done