Question

C++ PROGRAMMING Assignment: For this assignment, you will construct a program which is capable of taking...

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.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In C programming language please. -Construct a program that will make use of user defined functions,...
In C programming language please. -Construct a program that will make use of user defined functions, the do/while loop, the for loop and selection. -Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. -If the user enters in a 0 or negative number the program should exit with a message to the user indicating they...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
In C programming, Thank you Write a program to compute the area of a circle. You...
In C programming, Thank you Write a program to compute the area of a circle. You have to write a complete “C” program that compiles and runs in Codeblocks. Program requirements: 1. Declare the variables needed: radius (r) and area (yourLastName). 2. Calculate and print the area of each circle. 3. The program MUST prompt the user for a new radius (r) over and over until the user types -1. 5. Use the type double for all decimal numbers. 6....
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
A) write a program the computes nx and store the result into y You can use...
A) write a program the computes nx and store the result into y You can use y = Math.pow( Mantissa, exponent) Requirements: Besides main() your program must have one method with two parameters, one double and one int n and x are positive numbers read from the keyboard if the user makes an entry that does not meet this criteria, the user must be given to opportunity retry the entry the user must be able to quit prior to entering...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
Create a flowgorithm program to calculate the Area of Circle first then do the Circumference of...
Create a flowgorithm program to calculate the Area of Circle first then do the Circumference of Circle. The user will have the ability to convert Area OR the Circumference. That means we need to add a decision structure to the program. Furthermore, they need to be able to do it as many times as they want. That means we need to add a main loop structure to the program. The user will also have the choice of whether to run...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT