C++ Workshop - Functions, Parameters, & Scope (Ch. 5)

Started by
45 comments, last by me_minus 14 years, 7 months ago
Quote:Original post by J0Be
Hi guys,

Just wanted to ask about a part of the text in the book that goes like this:

Page. 130 Last paragraph states the following and I quote

Quote:
Last-in, first out means that whatever is added to the stack last is the first thing taken off. This differs from most queues in which the first in is the first out (like a line at the theater).


Shouldn't the queues be first in, last out?

Meaning:
Stack: First in, First out 'FiFo'
Queu: First in, Last out 'FiLo'?

Nope, the book has it right. Think of a stack of papers. Someone puts a new paper on the top of the stack and you grab the top most paper to work on it. The last page put on the stack is the first one you grab. Likewise, if someone puts one page on top of another the stack gets higher and higher. The first page put down can't be removed until all the pages above it are removed. Hence the 'first in last out'.

As for a queue, if you were standing in line for a drink of water and got there first, would you be the last person to get a drink? No! You'd be the first person to get a drink. If five people got in line, say first Alice, then Bob, Carrie, Dave, and finally Eliza, then Alice would leave first, then Bob, Carrie, Dave, and finally Eliza. Or ordering from a restaurant that can only cook one thing at a time. Or being put on hold for a help-line where it tells you 'there are 20 people ahead of you".
Quote:
Exercise:5_6

Using the call to a function as an expression? Is that often done or good coding habit?


The exact type of line
if ((result = Division(0, 1)) == -1) // <--- this part???

That is, the call to a function itself is pretty common, but assigning the result while at the same time comparing the result to something else is something I personally avoid. I would have written that code like this:

int result = Division(0,1);if(result == -1)


However, as I said, it IS pretty common to use a function as an expression. It depends on the function and it depends on the expression though. The problem with that particular example you gave is that the function returned either an error code (-1) or a result. This wouldn't be so bad if there wasn't valid input that would give -1 as a result (say Division(-2, 2); ). There are a number of different ways to write a function to take care of this kind of situation. If you use exceptions you can just throw an error, you can write the code under the assumption that no one will ever give 0 in the denominator(add an assert(denominator != 0); ). Maybe you can write something like this:

bool Division(double numerator, double denominator, double &result){  if (denominator == 0)  {    return false;  }  result = numerator / denominator;  return true;}int main(){  int answer;  if(Division(-2, 2, answer) )  {    cout << "Answer: " << answer << endl;  }  else  {    cout << "Divide by zero error" << endl;  }}


Hope that helps.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement
Thanks for the explanation nobodynews, that made perfect sense.

I get it why you wouldn't use the function call in the expression the way I did it. Just didn't think further in that there could be a value returned with -1 as a result!!!
Hey, thanks for this chapter, I'm looking forward to finishing the book. Your quizzes are really helpful too. After I answer the quiz I check my answers and almost always learn something.

It's a shame I'm a bit late on this but I can still learn can't I?

Have Fun!
Dbproguy
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life
Hello im new here and the c++.

I need to learn how progress that recursion @ listing 5.10

when i enter 6 for n variable its natural return( fib(n-2) + fib(n-1)); is making progress but its don't stop after one process.

i have seen codes and figure 5.4-5.5. i know what it does but i want to learn
why its doing it.

i was searching good book for c++ and i found here with c++ book advice+wonderful workshop.

But my english doesn't well enough to understand everything in that book(i am looking dictionary all time) and local
sources from my main language not enough for my needs.

So if you have time i need a little- simple english - information about this.

waiting for your reply with impatience.

Thanks.





i solved this myself.

i was miss the "return (deh(n-2)+deh(n-1));" line i did not interest with this line when i searching solution.

easy one.




[Edited by - Psiacron on July 29, 2008 8:17:24 AM]
Not sure if you guys still browse this posting, but I have a problem. I am working on Day 5 Exercise 7:

Write a program that asks for a number and a power. Write a recursive function
that takes the number to the power. Thus, if the number is 2 and the power is 4, the function will return 16.

I have the main part written but cannot think of how to write the Power function to perform the calc, I could prob do a Do While of For Next, but I have not gotten to that part of the book yet. Can someone give me some pointers as to where to start, not answers just hints :).

Thanks
The key is a RECURSIVE function.

This topic is closed to new replies.

Advertisement