Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the program in C++ only. FOLLOW THE SPECIFICATIONS BELOW TO create the following functions to calculate and display the sum or product of the

Write the program in C++ only.

FOLLOW THE SPECIFICATIONS BELOW TO create the following functions to calculate and display the sum or product of the integers from 1 to the number input. (Remember you already coded calculating the sum in Assignment 6, now you have to put that code in a function)

getNum Receives no input. Prompts the user to enter a number, use a loop to validate. Returns the result.

calcSum Receives an integer as input. USE AN ACCUMULATOR to calculate the sum of the integers from 1 to this number. Returns the result.

calcProd Receives an integer as input. USE AN ACCUMULATOR to calculate the product of the integers from 1 to this number. Returns the result.

Main should:

Prompt for a character that determines if you want to calculate the sum (S or s) or product (P or p). Use a switch to process the choices. Upper and lower case values should work the same way. Use default to give an error message if anything else is entered.

if a valid option is entered, use getNum to prompt for the number

Note that calcSum and calcProduct RETURN the result to main, and the output is printed in main.

For example, if you input 5 as the number, and S, the program should calculate 1 + 2 + 3 + 4 + 5 and display the sum as 15. If you input 5 and P the program should calculate 1 x 2 x 3 x 4 x 5 and display the product as 120. . The process should repeat as long as the user wants to continue. This loop should be in main. DO NOT use System.exit command to end the program. . See sample output below.

WHERE TO START? Do NOT focus on main, user input, switch, loop to start! Code and test calcSum first! You already did this problem in Assignment 6 so you should have the algorithm. You should test it very simply with something like this:

int result;

result = calcSum(3); // what should the answer be?

cout << The answer is << result << endl;

Once this works, calcProduct is very similar to code.

Sample program output

(Test case 1)

Enter S for sum, P for prod:z

Invalid choice

Again(y/n)? y

Enter S for sum, P for prod:s

Enter an integer greater than 1: 1

Must be greater than 1, re-enter: -3

Must be greater than 1, re-enter: 3

The sum of the numbers from 1 to 3 is 6

Again(y/n)? y

Enter S for sum, P for prod:p

Enter an integer greater than 1: 4

The product of the numbers from 1 to 4 is 24

Again(y/n)? n

(continue with data below)

Test Data: show in one run the cases above, as well as

S, 15

P, 8

S, 25

P, 12

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions