From c1fe9ee2ab2ebcfb057c9b7d9422f737f9da58f1 Mon Sep 17 00:00:00 2001 From: psharma-gif Date: Wed, 30 Nov 2022 21:37:51 -0500 Subject: [PATCH 1/2] first commit --- Sharma_Exercise10.R | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Sharma_Exercise10.R diff --git a/Sharma_Exercise10.R b/Sharma_Exercise10.R new file mode 100644 index 0000000..be7dbc4 --- /dev/null +++ b/Sharma_Exercise10.R @@ -0,0 +1,65 @@ +#Using the score-by-score information from this game summarized in +#“UWvMSU_1-22-13.txt” generate a graph similar to the one I show above. +#Don’t worry about how pretty your graph is. Focus more on the control structures +#required in your script used to generate the plot. + +score <- read.table("UWvMSU_1-22-13.txt", header = TRUE) +score_MSU <- integer(nrow(score)) +score_UW <- integer(nrow(score)) +time <- 1:nrow(score) +for(i in 1:nrow(score)){ + time[i] <- score$time[i] +} +score_MSU <- 0 +score_UW <- 3 +for(i in 2:nrow(score)){ + if(score$team[i]=="UW"){ + score_UW[i] <- score_UW[i-1]+score$score[i] + score_MSU[i] <- score_MSU[i-1] + }else if(score$team[i]=="MSU"){ + score_MSU[i] <- score_MSU[i-1]+score$score[i] + score_UW[i] <- score_UW[i-1] + } +} +#create dataframe for time, MSU score and UW score. +score_dataframe <- data.frame(time, score_MSU, score_UW) +#loading ggplot2 package +library(ggplot2) +ggplot(score_dataframe, aes(time))+ + geom_line(aes(y=score_MSU), color=1)+ + geom_line(aes(y=score_UW), color=2)+ + xlab("Time")+ + ylab("Score")+ + ggtitle("Cummulative score for MSU and UW") + + +#2. Write a game called “guess my number”. The computer will generate a random +#number between 1 and 100. The user types in a number and the computer replies +#“lower” if the random number is lower than the guess, “higher” if the random +#number is higher, and “correct!” if the guess is correct. The player can continue +#guessing up to 10 times. + +i <- 1 +#pick a random number from 1 to 100 +Guess <- sample(1:100,1) +Guess <- as.numeric(Guess) +while (i <= 10) { + reply <- readline(prompt = "Enter a number between 1 and 100:") + reply <- as.numeric(reply) + if(Guess < reply && i < 10){ + print("Guess a lower number, please!") + i <- i+1 + }else if(Guess > reply && i < 10){ + print("Guess a higher number, please!") + i <- i + 1 + }else if (Guess == reply){ + print("You win") + break + } + else if(i == 10){ + print("Sorry :(, you cant make guess for more than 10 times") + break + } +} + + From c0895c2f179949df2a574147510f8e1cff212089 Mon Sep 17 00:00:00 2001 From: psharma-gif Date: Fri, 2 Dec 2022 09:35:11 -0500 Subject: [PATCH 2/2] some minor changes --- Sharma_Exercise10.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Sharma_Exercise10.R b/Sharma_Exercise10.R index be7dbc4..ee7bf5b 100644 --- a/Sharma_Exercise10.R +++ b/Sharma_Exercise10.R @@ -39,24 +39,24 @@ ggplot(score_dataframe, aes(time))+ #number is higher, and “correct!” if the guess is correct. The player can continue #guessing up to 10 times. -i <- 1 +count <- 1 #pick a random number from 1 to 100 Guess <- sample(1:100,1) Guess <- as.numeric(Guess) -while (i <= 10) { +while (count <= 10) { reply <- readline(prompt = "Enter a number between 1 and 100:") reply <- as.numeric(reply) - if(Guess < reply && i < 10){ + if(Guess < reply && count < 10){ print("Guess a lower number, please!") - i <- i+1 - }else if(Guess > reply && i < 10){ + count <- count+1 + }else if(Guess > reply && count < 10){ print("Guess a higher number, please!") - i <- i + 1 + count <- count + 1 }else if (Guess == reply){ print("You win") break } - else if(i == 10){ + else if(count == 10){ print("Sorry :(, you cant make guess for more than 10 times") break }