Solved

Stacks

Posted on 2003-10-31
1
296 Views
Last Modified: 2010-04-01
please explain to me how this for statement works:

assuming that stack is a class with stack type set to int and stack capacity to 5.  Give the content of myTop and myArray data memebers of s after code segment has been executed.

Stack s;
for (int i = 0; i<5; i++)  //please help me in this step, what is the first value of i
s.push(2*i);    //what are the values of all the i's please help me.
   s.pop();
   s.pop();

the answer according to my professor is myTop = 2 and myArray is 0, 2, 4, 6 but please help me understand why it is so. i had this question on an exam and i need to understand it.  
0
Comment
Question by:needle176
1 Comment
 
LVL 5

Accepted Solution

by:
lwinkenb earned 20 total points
ID: 9662062
If you were going to put that for loop into english, it would sound like:
set an integer 'i' equal to 0.  while 'i' is less than 5 run the corresponding body, and add 1 to 'i' after each iteration through the loop.

So basically the for loop in your example runs 5 times.  The first time i is equal to 0, then 1, then 2, until it reaches 4.  When i is equal to 5 it wont run because the condition in the for loop says to run while i<5.

since you are pushing 2*i onto the stack, you will basically push the values 0, 2, 4, 6, 8.  Then the last two commands will pop 8, and 6 from the stack.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now