Using the STL stack
In a given company an employee receives successive quantities of wheat and puts them in a containers of 20kg .Let us suppose that once a container is full , it is closed and then a new container is used. We want to write a C++ program that stimulate the work of employee . The program will ask the user to enter consecutive quantities of wheat . These quantities will be stored in a stack . Once 20kg of wheat is gathered the existing elements in the stack are removed . The stack will keep only the remaining quantity that could not be put in the container. The program will continue asking to enter new quantities until 5 containers are filled .
1
Expert's answer
2015-06-04T03:35:09-0400
#include <iostream> #include <stack> using namespace std; int main() { stack<int> rest; int containerNumber = 1; int containerSum = 0; int quantity; while (containerNumber <= 5) { cout << "Enter quantity: "; cin >> quantity; rest.push(quantity); quantity = rest.top(); rest.pop(); if (quantity + containerSum <= 20) { containerSum += quantity; } else { rest.push(quantity); } if (containerSum == 20) { cout << "Container " << containerNumber << " is filled." << endl; containerNumber++; containerSum = 0; while (!rest.empty()) { rest.pop(); } } } return 0; }
Good job, but there is an error, if the user entered a number grater
than 20 like 25 the program will never stop asking for a number. it
should be like: if insert 25 container 1: full container 2: 5
Leave a comment
Thank you! Your comments have been successfully added. However, they need to be checked by the moderator before being published.
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!
Comments
Good job, but there is an error, if the user entered a number grater than 20 like 25 the program will never stop asking for a number. it should be like: if insert 25 container 1: full container 2: 5
Leave a comment