0
Guys help am given a question on creating a function called rangeSum and adds all the numbers from x to y so l used a for loop
input <- readLines('stdin') x <- as.integer(input[1]) y <- as.integer(input[2]) #define the function rangeSum <-function(x,y){ res<-0 for(i in x:y){ res=res+i } print(res) } This is my code
3 Antworten
+ 2
Thandolwenkosi
res <- res + i
and call rangeSum function with input value
rangeSum(x, y)
+ 2
Thandolwenkosi
You cannot use "=" symbol to assign value in R language
You should use <-
So in function there should be
res <- res + i
Now you didn't call that function.
Just creating a function is not enough, you have to call that function to perform operation
As your function accepts two arguments so when you call function you have to pass x and y in that function so that function can accepts argument value
So you should call function after creating function like this:
rangeSum(x, y)
0
You need to create a function that takes two parameters and returns the sum of all numbers between the two parameters inclusive.
The given code takes two numbers from input and passes them to a function called rangeSum().
Task
Define the rangeSum() function, so that the code works as expected.
Sample Input
4
9
Sample Output
[1] 39
Explanation
For the inputs 4 and 9, the returned value should be 39.
This is the question am lost l didn't understand you