Hi, i'm trying to write this program that uses a stack. Im just confused on how to create a function to print the bottom element in a stack. What i tried on my own was:
1 2 3 4 5
void stack::print_ends()
{
cout << data [top] << " " << data [stack_size];
}
but the data[stack_size] prints a club and diamond characters. I dont really understand why this is happening.
And my other problem i am having is how to print the smallest element in the stack. Again, i tried on my own, but it didn't work. I took that function out of my program because i couldnt get it to even compile.
I know i need a for loop for this, but i just can't seem to get it. I really could use all the help i can get, and any pointers are appreciated. Here is my full code:
char smallest (); // Should be char stack::smallest() (without semicolon)
{
char i; // Should be int i;
small = 0; // Should be char small = 0;
for (i = 0, i < stack_size, i++) // Semicolons, not commas
if (i > small) // Should be if (data[i] < small)
small = i // Missing semicolon, and should be small = data[i];
return small;
}
Also, about the for (i = 0; i < stack_size; i++)
I'm not sure the i < stack_size part is correct.
You're looping over the entire "data" array...even if the stack doesn't have that many elements yet.
Thanks, that was a lot of help. Ok, so my smallest function now looks like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
char stack::smallest ()
{
int i;
char small;
small = 0;
for (i = 0; i < data [stack_size]; i++)
if (data[i] < small)
{
small = data[i];
}
return small;
}
But i still cannot get it to print the smallest value in the stack. Im guessing im still getting the data [stack_size] wrong....