Sep 11, 2013 at 9:48am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
using namespace std;
class stack{
private :
int *st;
int s;
int top;
public :
stack(int );
void set_values();
void get_values();
void push(int );
};
stack::stack(int x)
{
s=x;
top=0;
st=new int [s];
}
void stack::set_values()
{
for (int i=0; i<s; ++i)
st[i]=rand()%10;
}
void stack::get_values()
{
for (int i=0; i<s; ++i)
cout<<st[i]<<endl;
}
void stack::push(int x)
{
st[++top]=x;
}
int main()
{
stack sc(10);
sc.set_values();
sc.get_values();
sc.push(?);
system ("Pause" );
return 0;
}
What should I put here as argument?
Last edited on Sep 11, 2013 at 11:21am UTC
Sep 11, 2013 at 10:02am UTC
fist of all srand was not declared, and what do you expect the main to do there, cause clearly it does nothing
Sep 11, 2013 at 10:04am UTC
include the cstdlib header and call the functions in your main.then everything would be fine
Sep 11, 2013 at 10:22am UTC
Are you sure.Even though everything is done in the main.I used valgrind and it shows no memory leak.although he should implement a destructor , but why is the memory deallocated Coder777
Sep 11, 2013 at 10:24am UTC
even used gdb....still no leaks
Sep 11, 2013 at 10:29am UTC
The problem is, its doesnt display the desired outputs. In fact doesnt display anything.
I need the random values..
Sep 11, 2013 at 10:32am UTC
Try this
#include <iostream>
#include <cstdlib>
using namespace std;
class stack{
private:
int *st;
int s;
int top;
public:
stack(int);
~stack();
void set_values();
void get_values();
};
stack::stack(int x)
{
s=x;
top=0;
st=new int[s];
}
stack :: ~stack()
{
delete [] st;
}
void stack::set_values()
{
for(int i=0; i<s; ++i)
st[i]=rand()%10;
}
void stack::get_values()
{
for(int i=0; i<s; ++i)
cout<<st[i]<<endl;
}
int main()
{
stack sc(10);
sc.set_values();
sc.get_values();
return 0;
}
Sep 11, 2013 at 10:34am UTC
But the randomm numbers will always be the same.If thats the case then fine.If not consider srand and time functions
Sep 11, 2013 at 10:44am UTC
its a destructor.to deallocate memory from the heap that you allocated.
Sep 11, 2013 at 10:46am UTC
Ya I can use srand; bt thats not a big problem, dude.
The problem is, the output is with garbage values..