Question

In: Computer Science

Write a function that takes a numeric or integer vector and adds up only the numbers...

Write a function that takes a numeric or integer vector and adds up only the numbers whose integer parts are even.

Modify your answer to question above to include an option that allows you to choose whether to sum numbers whose integer parts are even or are odd.

Your function should have as a default that it gives the same output as the function in question 4. In other words, if the user doesn’t specify whether to sum evens or odds, the function should sum evens.

Hint: the function arguments should look like function(x, evens = TRUE)

Solutions

Expert Solution

PROGRAM CODE:

/*
* vectorSum.cpp
*
* Created on: 31-Jan-2017
* Author: kasturi
*/

#include <iostream>
#include <vector>
using namespace std;

/**
* Function to determine the sum of even or odd numbers in a vector
*/
int sum(vector<int> x, bool evens)
{
   int sum = 0;
   for(vector<int>::iterator it = x.begin(); it != x.end(); ++it)
   {
       int num = *it;
       if(evens)
       {
           if(num%2 == 0)
           sum += num;
       }
       else{
           if(num%2 != 0)
               sum += num;
       }
   }
   return sum;
}

int main()
{
   vector<int> numbers;
   for(int i=1; i<=20; i++)
       numbers.push_back(i);

   cout<<"Sum of even numbers is : "<<sum(numbers, true)<<endl;
   cout<<"Sum of odd numbers is : "<<sum(numbers, false)<<endl;
}

OUTPUT:

Sum of even numbers is : 110

Sum of odd numbers is : 100


Related Solutions

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
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Using Matlab, write a function that takes numeric data as its input argument and prints a...
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative. Please include a copiable code and the associated screenshots.
In R studio Write a function that takes as an input a positive integer and uses...
In R studio Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Use the lapply function, do not use any of the loop commands in your code.
Write a function that will accept one integer matrix E and one column vector F by...
Write a function that will accept one integer matrix E and one column vector F by reference parameters, and one integer I as a value parameter. The function will return an integer v, which is the i-th coefficient of row vector denoted by E*F (multiplication of E and F). For example, if V = E*F, the function will return v, which is equal to V[i]. Explain the time complexity of this function inside of the function code as a comment....
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing...
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing the smallest value contained in the vector, an integer representing the largest value contained in the vector, and a double that represents the average of the values in the vector. The compileStats function will not “return” a value, it will set the Pass By Reference parameters to the correct values (smallest, largest, and average). Use the following main function in driver1b.cpp as a starting...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT