C++ PROGRAMMING
Assignment: For this assignment, you will construct a program which is capable of taking a user-given number, and adding up all of the numbers between 1 and the given number. So if someone inputs 12, it should add 1 + 2 + 3 + 4 + … 9 + 10 + 11 + 12, and return the answer.
However, you’ll be using two different methods to do this. The first method should utilize either a for loop or a while loop, to simply add the numbers together. The second method will meanwhile use the following formula.
Given some number, and told to calculate the sum from 1 to the given number, you can calculate the answer by simply calculating (X*(X + 1)) / 2
FOR YOUR ASSIGNMENT, you will use both of these methods to return the answer to the user, specifying which answer came from which method. In addition to simply returning the numbers however, your program MUST also mention the number of operations that occurred in calculating that answer. For this particular assignment we will only count arithmetic operations, adding, subtracting, multiplying, and dividing. So that if you perform the following: 1 + 2 + 3 + 4 + 5, the result requires FOUR addition operations.
For the two different methods you use, you’ll need your program to return the number of operations it had to perform to reach its answer. WHAT YOU NEED TO TURN IN, are two very specific scenarios, as you’ll be supplying the input yourself. First, you need to show a result in which using the mathematical formula uses fewer operations than using the looping addition. Second, you need to show a result in which using the looping addition uses fewer operations than the formula.
C++ Program:
#include <iostream>
using namespace std;
//Function prototypes
int loopingAddition(int, int&);
int formulaAddition(int, int&);
//Main function
int main()
{
int n, result, operations;
//Reading n value
cout << "\n\n Enter n value: ";
cin >> n;
operations = 0;
//Storing result of looping addition
result = loopingAddition(n, operations);
//Printing number of operations and
result
cout << "\n\n Looping Addition: \t Number
of operations: " << operations << " \t Result: "
<< result << " \n";
//Storing result of formula addition
result = formulaAddition(n, operations);
//Printing number of operations and
result
cout << "\n\n Formula Addition: \t Number
of operations: " << operations << " \t Result: "
<< result << " \n";
cout << endl << endl;
return 0;
}
//Function that performs looping addition
int loopingAddition(int n, int &operations)
{
int sum = 0, i;
//Initializing operations to 0
operations = 0;
//Iterating from 1 to n
for(i=1; i<=n; i++)
{
//Accumulating sum
sum = sum + i;
//Incrementing
operations
operations += 1;
}
//Subtracting 1 in total operations
operations -=1;
//Returning sum
return sum;
}
//Function that performs formula ((X*(X + 1)) / 2)
addition
int formulaAddition(int n, int &operations)
{
int sum;
//Operations in the formula are 3 (+ , *,
/)
operations = 3;
sum = (n *(n + 1)) / 2;
//Returning sum
return sum;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Get Answers For Free
Most questions answered within 1 hours.