Question

In: Computer Science

In this homework assignment, you will be writing three `void` functions. These functions each take a...

In this homework assignment, you will be writing three `void` functions. These functions each take a single integer parameter. The functions accomplish the following tasks.

* The first function prints out the sum of the numbers from 1 up to and including the number passed to it as an argument.

* The second function prints out the sum of the even numbers from 2 up to and including the number passed to it as an argument.

* The third function prints out the sum of the odd numbers from 1 up to and including the number passed to it as an argument.

To help you get started, the `main` program for this assignment has been provided to you (below). You should not alter the code in the `main` function. Your task is just to define the three missing functions within this same file.

Your solution to this problem must meet the following criteria.

1. You must define the three functions called in the `main` program provided.

2. You may not alter the `main` program code in any way.

3. Your code must be formatted correctly (use the "Format Document" command to do this).

4. Finally, your output **must** match the output of the included program, ``solution.o``. Run this program several times using different options to make sure you understand what that output should look like.

To run the program, you can enter a full path to the executable in a bash shell:

Main Code:

#include <iostream>

using namespace std;

// function definitions

// main program

int main() {

int number;

char sumType, doAgain;

bool sumTypeFlag;

// we will do this until the user is done

do {

// collect the number from the user

do {

cout << "Enter a positive integer: ";

cin >> number;

if (number < 1) {

cout << "Error! Invalid number." << endl;

}

} while (number < 1);

// ask for the type of sum

do {

cout << "Which numbers should I sum? (a=all, e=even, o=odd): ";

cin >> sumType;

sumTypeFlag = sumType != 'a' && sumType != 'e' && sumType != 'o';

if (sumTypeFlag) {

cout << "Error! Invalid sym type." << endl;

}

} while (sumTypeFlag);

// Make the function call

switch (sumType) {

case 'a':

printSumAll(number);

break;

case 'e':

printSumEven(number);

break;

case 'o':

printSumOdd(number);

break;

}

// should we do this again?

cout << "Another Sum? (y/n): ";

cin >> doAgain;

} while (doAgain == 'y');

return 0;

}

Solutions

Expert Solution

You can follow below simple approach for the three functions:

Approach for printSumAll():

Take a variable say sum and loop through all values from 1 to n and add them to sum

Code:

void printSumAll(int number){
   int sum=0;
   for(int i=1;i<=number;i++){
       sum+=i;
   }
   cout<<sum<<endl;
}

Approach for printSumEven()

Take a variable say sum and loop through all values from 1 to n and check if i is even then add it to sum.

Code:

void printSumEven(int number){
   int sum=0;
   for(int i=2;i<=number;i++){
       if(i%2==0)
           sum+=i;
   }
   cout<<sum<<endl;
}

Approach for printSumOdd():

Take a variable say sum and loop through all values from 1 to n and check if i is odd then add it to sum.

Code:

void printSumOdd(int number){
   int sum=0;
   for(int i=2;i<=number;i++){
       if(i%2==1)
           sum+=i;
   }
   cout<<sum<<endl;
}

Hope this might helped you to understand the concepts.

Have a nice day.


Related Solutions

Problem 5: writing a faster implementation For this homework assignment, you will write a faster implementation...
Problem 5: writing a faster implementation For this homework assignment, you will write a faster implementation of do_insertions_simple (which we will call do_insertions_fast). We won't need anything extra (no special modules, no advanced algorithms, no Numpy) in order to obtain a considerable speedup. Let's think about what makes do_insertions_simple slow, and about how we can rewrite the whole thing in a faster way. The biggest problem with do_insertions_simple is that it calls insert once for every element of the insertions...
In writing the background for this weekend’s homework assignment, I realized that I didn’t need to...
In writing the background for this weekend’s homework assignment, I realized that I didn’t need to spend much time with the introduction of the company. Everyone knows Amazon, as a matter of fact, most of us shop on Amazon for many things that we use in our daily life. The company is one of the largest in the world and has over 500,000 employees. The CEO and founder of Amazon, Jeff Bezos, was interviewed at an awards ceremony in 2016...
Wondering where to start on this C++ homework assignment involving friend functions and overloading operators implemented...
Wondering where to start on this C++ homework assignment involving friend functions and overloading operators implemented in a Rational class. These are the instructions: Your class will need to store two internal, integer values for each Rational number, the numerator (top) and denominator (bottom) of the fraction. It will have three constructor functions, with zero, one and two arguments, used as follows:     Rational test1, test2(10), test3(1, 2); The declaration for test1 calls the default (no argument) constructor, which should...
Write functions for each of the following problems. Each problem should be solved by writing a...
Write functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. (a) Write a function that uses recursion to raise a number to a power. The function should take two arguments, the number to be raised to the power (floating point) and the power (a non-negative int). (10 Points) (b) Write a boolean function named isMember that takes two arguments: an array of...
There are 3 SPSS outputs in this homework assignment. The questions for each output are listed...
There are 3 SPSS outputs in this homework assignment. The questions for each output are listed below. Please type your answers into this word document and submit it as an attachment in the assignment tab. Q1. Researchers were interested in determining whether background music helped or hindered students’ performance on a math test. Students were randomly assigned to 1 of 3 groups: 1) no music; 2) music only; and 3) music with lyrics. Students were then given a math exam,...
(In Java) Write three static functions that each take two double values which represent the two...
(In Java) Write three static functions that each take two double values which represent the two smaller sides of a right triangle. The first will calculate and return the area of the triangle, the second will calculate and return the length of the hypotenuse and the third will calculate and return the perimeter of the triangle. (Note: to calculate the perimeter, you need the value of the hypotenuse, so you must be calling that method from inside the other method.)...
List and describe the three functions of money. 2. You take $100 you had kept under...
List and describe the three functions of money. 2. You take $100 you had kept under your mattress and deposit it in your bank account. If this $100 stays in the banking system as reserves and if banks hold reserves equal to 10 percent of deposits, by how much does the total amount of deposits in the banking system increase? By how much does the money supply increase?
Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
This is an individual assignment. In writing a paper about each problem, identify the consequences of...
This is an individual assignment. In writing a paper about each problem, identify the consequences of the actions taken, and then determine whether the actions taken represented a greater good, who would benefit from the good, and whether the consequences ethically justify the decisions and actions. The Mayor of a large city was given a free membership in an exclusive golf club by people who have received several city contracts. He also accepted gifts from organizations that have not done...
Java Question: Write three static functions that each take two double values which represent the two...
Java Question: Write three static functions that each take two double values which represent the two smaller sides of a right triangle. The first will calculate and return the area of the triangle, the second will calculate and return the length of the hypotenuse and the third will calculate and return the perimeter of the triangle. (Note: to calculate the perimeter, you need the value of the hypotenuse, so you must be calling that method from inside the other method.)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT