In: Computer Science
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;
}
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.