Question

Write the following functions, using for, while, and repeat loops.( using R program) a. input: a...

Write the following functions, using for, while, and repeat loops.( using R program)

a. input: a number and a vector, output: the (first) position of the number in the vector

b. input: a number x, output: the first n Natural numbers which sum is more than x

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a ans:

a<-function(number,vector){
for(i in 1:length(vector)){
if(vector[i]==number){
return(i)
}
}
}
cat("The position:",a(1,c(0,2,1,3,1,4)))
  

#output:

b ans:

natural<-function(x){
sum=1
i=0
repeat{
if(sum>x){
break
}
else{
sum=sum+i
i=i+1


}
}
for(m in 1:i){
print(m)
}
  
}
natural(7)

#output:

#if you have any doubt comment below.if you like give thumbs up...

Add a comment
Know the answer?
Add Answer to:
Write the following functions, using for, while, and repeat loops.( using R program) a. input: a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT