Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 20th, 2004, 09:04 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default Stacks

I dont have much programming experience. Is this the way to print the elements in the stack?

#define STACK_SIZE 5

int stack[STACK_SIZE]; // initialise the array

void print(int stack[])
{
 int count;

    for (count = 0; count < sizeof(stack); count++)
    {
       printf("%d ", stack[count]);
    } // end for
} // print




George N. Bilios
__________________
gbilios
Reply With Quote
  #2 (permalink)  
Old March 20th, 2004, 03:24 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In a word: no.

(If this was a true/false question, and all that anyone wants to know is
the answer, that person can stop reading now.)

In a few more words:

Usually I would recommend that people with questions like this should
write a program that calls this function and see happens. Since you say
that you are just getting started, I'll write a program that does
something like yours, and suggest that you compile and run it and see
what happens.

This is a pretty useless routine for printing stuff, but is a good think-piece
to illustrate two points:

1. What is sizeof ?

2. How do functions process arrays that are passed as arguments.

sizeof is an operator, and C evaluates expressions with sizeof at
compile time.

The following illustrates the points, I think. I used a slightly
different function than you wrote, because it illustrates the point a
little better, I think.

In main(), C knows that stack was declared as an array of five doubles,
so sizeof(stack) is equal to the number of bytes allocated to that
array.

In print(), C only knows that stack is a pointer to a double (arguments
of type double x[] are treated as double *x, when being compiled by
C). Inside the function print(), sizeof(stack) is the number of bytes
required by a pointer to a double. There is no way, (I repeat no
way
) that print() can know the number of elements in the array unless
you tell it!

Try the program and see if it makes any sense.

Code:
#include <stdio.h>

#define STACK_SIZE 5
double stack[STACK_SIZE]; // initialise the array

int main()
{
  int i;
  void print(double stack[]);

  printf("\n\n");
  printf("Debug: In main():    sizeof(stack) = %d\n", 
          sizeof(stack));
  printf("Debug: Number of elements in stack =  %d\n\n", 
          sizeof(stack)/sizeof(stack[0]));

  for (i = 0; i < STACK_SIZE; i++) {
    stack[i] = 2 * i + 1;
  }
  print(stack);

  printf("\n\n");

  return 0;
}

void print(double stack[])
{
 int count;
 
    printf("Debug: In print(): sizeof(stack)   =  %d\n\n", sizeof(stack));
    for (count = 0; count < sizeof(stack); count++)
    {
       printf("%lf ", stack[count]);
    }
}
Regards,

Dave
Reply With Quote
  #3 (permalink)  
Old March 21st, 2004, 12:21 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default

i compiled the code you supplied. The code below:

printf("Debug: Number of elements in stack = %d\n\n",
          sizeof(stack)/sizeof(stack[0]));

The code here does not display the number of elements in the array, but the actual limit of the array.

i integrated the code in my stack application and pushed some values. if i wanted to display the size of the stack i'll get 5, 'cos the stack size is set to 5. if i were to pop a value off the stack and then print, i can still see that value, why?

gbilios

George N. Bilios
Reply With Quote
  #4 (permalink)  
Old March 21st, 2004, 11:05 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:
printf("Debug: Number of elements in stack = %d\n\n",
          sizeof(stack)/sizeof(stack[0]));

The code here does not display the number of elements in the array, but the actual limit of the array.
The code that I showed has not implemented a stack! It has reserved memory for STACK_SIZE doubles. The sizeof operator is evaluated at compile time (and therefore will not change no matter what the program does).

The program message "Number of elements in stack = 5", tells you the number of doubles that have been defined in the C array "stack".

I used your program variable names, and changed the type to "double", just to be different

Go back and change the name of the array to "double_array", instead of "stack", and change the print message to
Code:
printf("Debug: Number of elements in double_array =  %d\n\n",
          sizeof(double_array)/sizeof(double_array[0]));
Now that we have nomenclature out of the way, are you ready to implement a stack, using this array?

If you have an assignment (or a simple desire) to implement a stack, you must have some concept of what a stack is.

One way to implement a stack is to maintain a pointer (pointing to an element of your array) that points to the current "top of the stack". That is, it points to the last value that you have pushed onto the stack.

To push something on to the stack, you will increment the pointer and write the new value.

To pop something from the stack, you will read the value from the array (the element pointed to by the pointer), then decrement the pointer. There are a few more details to consider, but that's the basic idea.

Of course, your program will have to keep track of things so that you won't write to a full stack (that is, don't allow pushing anything onto the stack if it already contains STACK_SIZE elements). You also must protect against popping from an empty stack.





What operations do you need?

Here are some suggestions:

1. initialize_stack() (or, maybe flush_stack())
   This makes the stack empty.

2. push_stack(value)
   Pushes something onto the stack

3. pop_stack()
   Returns the value of the entry at the top of the stack,
   and removes that entry from the stack.

4. stack_full()
   returns 0 if the stack is not full, 1 if it is full

5. stack_empty()
   returns 0 if the stack is empty, 1 if it is not empty

Stacks are sometimes implemented as a struct whose elements
are an array, and a pointer, which is modified by pushing and popping, and points to current "top of stack".

Dave
Reply With Quote
  #5 (permalink)  
Old March 21st, 2004, 06:44 PM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default

I changed the code: instead of using count < STACK_SIZE, i used count < tos.

int tos = -1; // top of the stack
int count;

for(count = 0; count <= tos; count++)
{
  printf("%d", stack[count]);
} // end for



George N. Bilios
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Queues & Stacks kgs51 VB.NET 2002/2003 Basics 1 November 21st, 2004 05:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.