sum = 0
list = c(2:5)
for(i in list){
sum = sum + i
}Practice Exam 1
Practice Exam, and general R practice
Practice Exam
- You run the following code
What would be the output of
print(list)?When this function finishes running, what is
sumequal to?
- You run the following code
x = 10
y = 5
while(x>y){
x = x-2
}When you finish running this code, what will x equal?
What will y equal?
Consider this very slightly different code (remember, != means ‘not equal’). What is the problem with it?
x = 10
y = 5
while(x!=y){
x = x-2
}- Consider the following function, which takes the sum of a given number of pennies, nickels, dimes, and quarters:
coin_sum = function(p, n, d, q){
c = p + 5*n + 10*d + 25*q
if(c >= 100){
return(c/100)
}
else{
return (c)
}
}Calculate
coin_sum(1,1,1,1)Calculate
coin_sum(0,0,0,4)What does the variable
nmean in the function?Explain the purpose of the conditional (
if c>=100...) in human terms. Why might we have it?