Answer:
Explanation:
The code is written in C++:
#include <iostream>
#include <stack>
#include <math.h>
#include <iomanip>
using namespace std;
/*
* Supporting method to print contents of a stack.
*/
void print(stack<double> &s)
{
if(s.empty())
{
cout << endl;
return;
}
double x= s.top();
s.pop();
print(s);
s.push(x);
cout << x << " ";
}
int main(){
// Declaration of the stack variable
stack<double> stack;
//rray with input values
double inputs[] = {25,64,-3,6.25,36,-4.5,86,14,-12,9};
/*
* For each element in the input, if it is positive push the square root into stack
* otherwise push the square into the stack
*/
for(int i=0;i<10;i++){
if(inputs[i]>=0){
stack.push(sqrt(inputs[i]));
}else{
stack.push(pow(inputs[i],2));
}
}
//Print thye content of the stack
print(stack);
}
OUTPUT:
5 8 9 2.5 6 20.25 9.27362 3.74166 144 3
-------------------------------------------------------------------------
Process exited after 0.01643 seconds with return value 0
Press any key to continue . . . -