Hi I am trying to find the number of previous towers less than or equal to the current one , this solution is working great for inputs(NumOfTowers) <=10, but for the NumOfTowers >10 the code is going in segfault, i could not see the issue here,
#include <iostream>
#include <stack>
using namespace std;
int main()
{
std::stack<int> Towers; //for storing the Height of the towers
std::stack<int> TempTowers; // Temrory buffer stack
std::stack<int> CountTowers; //for storing the count of all previous
towers less than the current one
unsigned int NumTestCases,NumOfTowers,i,j,count=1,temp,temp_height;
cin>>NumTestCases;
cin>>NumOfTowers;
while(NumTestCases){
while(!Towers.empty()){
Towers.pop();
}
for(i=0;i<NumOfTowers;i++){
cin>>temp;
Towers.push(temp);
}
for(i=0;i<NumOfTowers-1;i++){
count=1;
temp_height=Towers.top();
Towers.pop();
temp=Towers.top();
while(temp<temp_height){
count++;
TempTowers.push(temp);
Towers.pop();
temp=Towers.top();
}
CountTowers.push(count);
while(!TempTowers.empty()){
temp=TempTowers.top();
TempTowers.pop();
Towers.push(temp);
}
}
NumTestCases--;
cout<<"1"<<" ";
while(!CountTowers.empty()){
cout<<CountTowers.top()<<" ";
CountTowers.pop();
}
cout<<"\n";
}
}
any help would be great.