Home > Mobile >  Create function min() by manual calculations
Create function min() by manual calculations

Time:10-05

I'm completely new to coding and trying to figure out a task which tell us to:

Create 3 functions to compute the minimum, maximum and average temperature By using manual calculations. I've tried to find answer here and i got this which tells me the max number, but i dont really understand the code, can someone explain this code please?

A1 <- as.numeric(readline(prompt = "Write the temperature for Tokyo: "))
B2 <- as.numeric(readline(prompt = "Write the temperature for Johannesburg: "))
C3 <- as.numeric(readline(prompt = "Write the temperature for Stockholm: "))
D4 <- as.numeric(readline(prompt = "Write the temperature for Oslo: "))
E5 <- as.numeric(readline(prompt = "Write the temperature for Amsterdam: "))
F6 <- as.numeric(readline(prompt = "Write the temperature for Mexico city: "))
G7 <- as.numeric(readline(prompt = "Write the temperature for Hanoi: "))
H8 <- as.numeric(readline(prompt = "Write the temperature for Canberra: "))
I9 <- as.numeric(readline(prompt = "Write the temperature for Tbilisi: "))
J10 <- as.numeric(readline(prompt = "Write the temperature for New Delhi: "))


data = data.frame(A1,B2,C3,D4,E5,F6,G7,H8,I9,J10)
datamatrix <- as.matrix(data) #Converts data into a matrix

y=-Inf
z=0
for(i in datamatrix) {
  
  if(i>y){
    y=i
    
    if(y!=Inf){z=i}
  }
  else{y=Inf}
}
print(paste0("maximum number is: ", z))

CodePudding user response:

A1 <- as.numeric(readline(prompt = "Write the temperature for Tokyo: "))

And the rest to J10 are variables where the value is defined by the user. If you run the above line in the console, it expects you to insert a number.

data = data.frame(A1,B2,C3,D4,E5,F6,G7,H8,I9,J10)
datamatrix <- as.matrix(data) #Converts data into a matrix 

This prepares the data

y=-Inf
z=0
for(i in datamatrix) {
  
  if(i>y){
    y=i
    
    if(y!=Inf){z=i}
  }
  else{y=Inf}
}
print(paste0("maximum number is: ", z))

This part is a 'for loop' meaning it loops (Goes through) every value in datamatrix (A1 to J10) and assigns it to the i variable and does some calculation on it. The code below the for(i in datamatrix) between the { and } is a chunk of code that is run everytime i is assigned.

That code chunk checks if i is larger than y: if(i > y) which in this case is negative infinity. It assigns i to the y and z variables every time i is larger than y, but in case that y is bigger than i in any iteration, the z variable will not change again as y will be assigned to Inf and i cannot be larger than that.

What this means is that your function does NOT always find the maximum number. You can test this by assigning values to A1 to J10. Only in very specific circumstances will you get the maximum number.

z=-Inf
for(i in datamatrix) {   
  if(z < i)
    z <- i
}
print(paste0("maximum number is: ", z))

This for-loop will find the maximum number. I do not claim it is optimal but it should be simpler to understand.

It will loop through the values and check if z is smaller than i. If yes, then i value is assigned to z. In case that a larger number comes along in the loop, it will reassign the i, but only if it is larger, meaning you end upp with the highest value.

Hope this helps.

  •  Tags:  
  • r
  • Related