Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a function oddSum that calculates and returns the sum of all odd positive integers less than a given number . So, if we call

write a function oddSum that calculates and returns the sum of all odd positive integers less than a given number. So, if we call the function with 6 as its argument, we should get 9 (since 1 + 3+ 5 = 9). This should require the use of a loop.

//////////////////////////////////////////////////////////// // Included headers //////////////////////////////////////// #include using namespace std;

//////////////////////////////////////////////////////////// // Function declaration for Assignment 3 ///////////////////

/** * Function to calculate and return the sum of all odd * positive integers less than the given number. * * @param num - the target number we sum up to @pre > 0 * * @returns sum of all odd positive integers < num */ int oddSum(int num);

//////////////////////////////////////////////////////////// // Main function with some basic tests ///////////////////// int main() { // Test case 1: sum of odd numbers less than 6 // is 1 + 3 + 5 == 9. double res = oddSum(6); if (res == 9) { cout << "Test case 1 OK" << endl; } else { cout << "Test case 1 FAIL: calculated " << res << " instead of 9" << endl; } // Test case 2: sum of odd numbers < 17 is // 1 + 3 + 5+ 7 + 9 + 11 + 13 + 15 = 64 res = oddSum(17) if (res == 64) { cout << "Test case 2 OK" << endl; } else { cout << "Test case 2 FAIL: calculated " << res << " instead of 64" << endl; } // Try some more test cases yourself... // Program done return 0; }

//////////////////////////////////////////////////////////// // Implementations/definitions of your functions ///////////

// Your code to complete the assignment goes here!

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

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

5.5 Summarize external recruitment methods.

Answered: 1 week ago

Question

1. Prepare a flowchart of Dr. Mahalees service encounters.

Answered: 1 week ago