Question

In: Computer Science

how do you get the sum of all elements in a stack in c++ ?

how do you get the sum of all elements in a stack in c++ ?

Solutions

Expert Solution

There can be 2 approaches

1. We dont want the original stack again after calculating the sum

2. We want the original stack again after calculating the sum

In the first approach, we simply pop the elements till the stack becomes empty and add the element popped out to the sum.

In the second approach, we need to store the element popped out using another data structure, for example another stack and push the elements back to it after we have our answer.

Here is the code:

#include <iostream>

#include<stack>

using namespace std;

//Approach 1

int calc1(stack<int> s1)

{

    int ans=0;

    //run the loop till stack becomes empty

    while(!s1.empty())

    {

        //add the topmost element to the answer

        ans+= s1.top();

        //remove the element

        s1.pop();

    }

    return ans;

}

//Approach 2

int calc2(stack<int> s2)

{

    //use another stack for storing removed elements

    stack<int> temp;

    int ans=0;

    while(!s2.empty())

    {

        //add the topmost element to the answer and push it to another stack

        ans+=s2.top();

        temp.push(s2.top());

        //remove element from original stack

        s2.pop();

    }

    while(!temp.empty())

    {

        //push the elements back to obtain the original stack

        s2.push(temp.top());

        temp.pop();

    }

    return ans;

}

int main() {

    stack<int> s1;

    stack<int> s2;

    //pushing elements into stack 1

    s1.push(1);

    s1.push(3);

    s1.push(4);

    s1.push(5);

    s1.push(6);

    //pushing elements into stack 2

    s2.push(1);

    s2.push(3);

    s2.push(4);

    s2.push(5);

    s2.push(6);

    int sum1, sum2;

    //calculating sum using approach 1

    sum1 = calc1(s1);

    //calculating sum using approach 2

    sum2 = calc2(s2);

    cout<<"Sum of elements of first stack: "<<sum1;

    cout<<"\nSum of elements of second stack: "<<sum2;

}


Related Solutions

In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
how do you get the PV factor?
how do you get the PV factor?
how do you get p value
how do you get p value
1) Class Reviews If you sum up light transmission, absorption and scattering do you get unity...
1) Class Reviews If you sum up light transmission, absorption and scattering do you get unity (1)? If yes why, if not what is missing? In your owns words describe the difference between phase and group velocity From Maxwell’s equations derive the wave equation. State explicitly any assumption you made. How is the beating length defined of a directional coupler. Explain in your own words why a symmetric coupler is needed to fully switch a signal from a cross-to-bar output...
Who get a performance appraisal? How do you get a performance appraisal? When when do you get a performance appraisal?
Who get a performance appraisal?How do you get a performance appraisal?When when do you get a performance appraisal?
Write a C program to show sum of 10 elements of array and show the average....
Write a C program to show sum of 10 elements of array and show the average. [10]
Do you find all the elements as you have learnt, or do you find any difference?...
Do you find all the elements as you have learnt, or do you find any difference? if yes bring out the differences. Auditors' Report to the sharehoders Scope of aduit: We have aduited the accompanying consolidated balance sheet of x industries corporation and its subsudaries (the "Group") as at 31 December 2016 and the related consoildated statments of income, cash flows and shareholders' equity forr the Group's management and have been prepared by them in accordance with the requirememtns of...
if I need to get the sum of 6 numbers, here is a C programming solution...
if I need to get the sum of 6 numbers, here is a C programming solution int getSum(int number1, int number2, int number3, int number4, int number5, int number6); and the function is: int SUM; sum = number1+number2+number3+number4+number5+number6; return SUM; this function is called in main, and the main looks like: int totalSum; totalSum=getSum(1,2,3,4,5,6); while(1); so how can I write this C programming code in ARM assembly code, can you please write the comment of each line so I can...
How do you calculate the radioactive isotope inventory of all present elements when a sample 1g...
How do you calculate the radioactive isotope inventory of all present elements when a sample 1g of 5% U-235 and 95% U-238 is left to decay for a year?
a. What is Bitbucket and how do I get there? b. What is Sourcetree? c. How...
a. What is Bitbucket and how do I get there? b. What is Sourcetree? c. How do I get Sourcetree and install it? d. With a lot of details and screen shots show the following with Sourcetree i.Clone a repository from Bitbucket ii.Fetching iii.Pulling iv.Staging v.Committing vii.Pushing
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT