473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

stack using C++

16
I have a problem in C++.My problem is how to push random number to a stack.
It is to generate 20 random numbers and push it to the stack and pop it from the stack when it's needed.
Jan 10 '07 #1
2 3550
Rryk
1
What do u mean by 'stack'? Is it a command stack or simply array (and several stack functions) or do u want to use any class from standart libraries (like MFC)?

If u simply need a stack in your program u may use this code:

Expand|Select|Wrap|Line Numbers
  1. include <stdio>;
  2. include <stdlib.h>;
  3.  
  4. // redefine this to set up a maximum numbers could be hold in stack
  5. #define MAX_SIZE 100
  6. // define from which interval we will get random numbers
  7. #define MIN_RAND 0
  8. #define MAX_RAND 1000
  9.  
  10. int * stackArray = new int[MAX_SIZE];
  11. int stackSize = 0;
  12.  
  13. // to get lasy number from the top and delete it from stack
  14. int pop()
  15. {
  16.    return stackArray[--stackSize];
  17. }
  18.  
  19. // to get last number from the top without delete it from stack
  20. int pick()
  21. {
  22.    return stackArray[stackSize-1];
  23. }
  24.  
  25. // put a number into a stack
  26. void put(int i)
  27. {
  28.    stackArray[stackSize++] = i;
  29. }
  30.  
  31. void main()
  32. {
  33.    int i;
  34.    // to initiate randomizer
  35.    srand();
  36.  
  37.    // generate 20 random number and put them into a stack
  38.    for (i = 0; i < 20; i++)
  39.    {
  40.       int number = (int)((rand()/RAND_MAX)*(MAX_RAND-MIN_RAND)+MIN_RAND);
  41.       put(number);
  42.    }
  43.  
  44.    // get numbers from stack and print them out
  45.    while (stackSize > 0)
  46.    {
  47.       cout << pop();
  48.    }
  49. }
Sorry - hadn't time to test code. It should work, but there may be some syntax errors. And while using functions pop() and put() check maximum size of stack and if stack is empty, as i didn't wrote any error checking - wrong use will cause runtime error;
Jan 10 '07 #2
Banfa
9,065 Expert Mod 8TB
When you say the stack, I think you mean a stack.

You will need to create a stack (of integers) to use. In such a simple case it could just be an array with a couple of index variables.

Alternitively since it is C++ it would be rather easy to implement using a list or deque from the STL.
Jan 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is...
7
by: unified | last post by:
Ok, I'm working on a program that is supposed to compare each letter of a string that is put into a stack and a queue. It is supposed to tell whether or not a word is a palindrome or not. (a...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
1
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting...
87
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.