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 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...
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 approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
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...
Write a Python program which calculates the average of the numbers entered by the user through...
Write a Python program which calculates the average of the numbers entered by the user through the keyboard. Use an interactive loop and ask the user at each iteration if he/she wants to enter more numbers. At the end dispay the average on the screen. Using built-in library functions for finding average is not allowed. Sample output of the program: Enter a number > 23 More numbers (yes or no)? y Enter a number > 4 Do you have more...
Assignment Content You recently graduated from college and are applying for a programming job that requires...
Assignment Content You recently graduated from college and are applying for a programming job that requires the understanding of loops in Python. The manager you are interviewing with has asked you to take an assessment to prove your programming knowledge. Below are the requirements for the programming skills test. In Python, create a program that meets the following requirements: Take two integers from the user. Save the lower number as x. Save the largest integer as y. Write a loop...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...