Question

In: Statistics and Probability

Use the following two loop strategies to write a function that sums all even numbers before...

Use the following two loop strategies to write a function that sums all even numbers before an odd number is observed in any given numeric vector, and test your code in R. For example, if the input vector is 2, 4, -2, 3, 4, 5, then the first odd number appears in the fourth position, which is 3. The output should be the sum of the first to the third numbers, which will be 2 + 4 − 2 = 4. If the first number of the input vector is an odd number, then the output should be 0.

• for(){}

• while(){}

Solutions

Expert Solution

R code, using for()

------------
#using for()
sumEven<-function(x){
   #initialize the sum to 0
   s<-0
   #loop through all the elements
   for (i in x) {
       #check if the number of even
       if (i%%2==0) {
           #add the number to sum
           s<-s+i
       } else {
           #stop if the number is odd
           break
       }
   }
   return(s)
}

#set the numeric vector
x<-c(2, 4, -2, 3, 4, 5)
#call the function
sumEven(x)
#return 0, if the first element is odd
x<-c(19,2, 4, -2, 3, 4, 5)
#call the function
sumEven(x)


----

Get this

using while()

-------------

#using while()
sumEven<-function(x){
   #initialize the sum to 0
   s<-0
   #set the index to the first element
   i<-1
   #loop through the elements till odd
   while (x[i]%%2==0) {
       #add the number to sum
       s<-s+x[i]
       #increment the index
       i<-i+1
   }
   return(s)
}

#set the numeric vector
x<-c(2, 4, -2, 3, 4, 5)
#call the function
sumEven(x)
#return 0, if the first element is odd
x<-c(19,2, 4, -2, 3, 4, 5)
#call the function
sumEven(x)

----

Get this


Related Solutions

Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
USE PYTHON : # Problem Set 04: - Write a function to seek for all even...
USE PYTHON : # Problem Set 04: - Write a function to seek for all even numbers and odd numbers in the middle of two number A and B. Print even and odd numbers in 1 and 2020 (including both these two numbers) # Problem Set 05: - A website requests an user to input his account password. - Write a program to examize the validity of the password. - The valid password must consists of: - At least 1...
Using a “for loop” print all even numbers in range from 1 to 1000. Also, please...
Using a “for loop” print all even numbers in range from 1 to 1000. Also, please count and print how many even numbers you have found.
Write assembly instructions that compute the following: The sum of all even numbers between 2 and...
Write assembly instructions that compute the following: The sum of all even numbers between 2 and 100 (inclusive) -- the answer should be 2550 Prompt the user for 2 values, put them in variables a and b, then find the sum of all odd numbers between a and b. The sum of all the squares between 1 and 100 (inclusive) . -- the answer should be 338350 All of the statements above should print the answers using print_int MUST BE...
1.write a small program using a loop to add a series of numbers 2.write a function...
1.write a small program using a loop to add a series of numbers 2.write a function called "main" that performs several given steps. Be sure to call the main() function so that its code executes In python and doesn't have to be long. just long enough to do what it says. Thank you.
Write a Scheme function that takes two integers and returns the list of all integer numbers...
Write a Scheme function that takes two integers and returns the list of all integer numbers between these two integers (inclusively) in increasing order. (numbers 10 20) (10 11 12 13 14 15 16 17 18 19 20) Please explain every step.
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
Write a for loop in .java to display all numbers from 13 - 93 inclusive, ending...
Write a for loop in .java to display all numbers from 13 - 93 inclusive, ending in 3. • Write a for loop to display a string entered by the user backwards.
Python Write a for loop with a range function and format output as currency Use an...
Python Write a for loop with a range function and format output as currency Use an input statement to ask the user for # of iterations using the prompt: #? [space after the ?] & assign to a variable Convert the variable to an integer Use a for loop and a range function to display all whole numbers from 1 to the user entered number Display the value of the item variable on screen in each iteration in the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT