
Patrick B. answered 03/02/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stack>
#include <stdlib.h>
#include <time.h>
Go()
{
int N;
cout << "How many ??? :>";
cin >> N;
int iNum;
stack <int> intStack;
srand (time(NULL));
for (int iLoop=0; iLoop<N; iLoop++)
{
iNum = rand();
intStack.push(iNum);
}
long grandTotal = 0;
long evenSum=0;
int iCount=0;
int pop4=0;
while (!intStack.empty())
{
iNum = (int) intStack.top();
cout << " int # " << (iCount+1) << " is " << iNum << endl;
intStack.pop();
grandTotal += iNum;
if ((iNum%2)==0) { evenSum += iNum; }
if ((++iCount)==4) { pop4 = iNum; }
}
cout << "Grand total is " << grandTotal << endl;
cout << "Even total is " << evenSum<< endl;
cout << " 4th item is " << pop4 << endl;
}
int main()
{
Go();
}