Thread: Confused newbie

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    9

    Confused newbie

    Hey I am new to programming, and I am reading a book and it has questions and answers after every section. One of them asks:
    How would the number 2321 be spanned through memory? and the answer is:
    1st byte 9 and 2nd byte 17

    What do they mean by this? I've looked through the section again and do not understand the question or the answer.


    Also it asks:
    What would be the result of the following operation:
    int Result, a, b, c;
    a = 9;
    b = 1;
    c = 2;
    Result = c-- + (b++ - --b) * a + c
    They have the answer as 11, but I have the answer as 21. Can someone explain to me how they have that answer and what I did wrong. Here is what I did:
    first I did my post and prefix operation which make the problem like this:
    result = 2 + (1 - (-1)) * 9 + 1
    result = 2 + (2) * 9 + 1
    result = 2 +18 + 1
    result = 21

    The b gets added 1 to the taken directly away again right??? since it is a negative 1 then it is added to to make a 0 and then taken from to make a negative 1 again and then 1 - negative 1 is prositive 2.

    The c's are 2 and 1 which make 3 right? I'm confused.

    Thanks for any help.
    Nice board!

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I doubt the answer is even possible in the second exercise...

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Both questions are relying on platform and compiler specific items. This is bad, because such things are not truly portable. When you study C++, you should focus on more general things that work on all or almost all systems. Learning something specific and thinking it's always true will bite you later on when you least expect it.

    Let's take their first question. How is 2321 represented in memory? The answer might surprise you: We don't know. Each system is free to keep memory organized in any way they want. Motorola-based systems usually keep integers in big-endian format, while Intel-based chips generally keep integers in little-endian format. This can be quite confusing, and you should write programs that do not assume anything about the underlying implementation, with an obvious exception being that you are writing software specific to that system.

    Now here's two ways 2321 might be represented in memory, assuming an integer is 32-bits:

    Big Endian:

    Code:
    00000000 00000000 00001001 00010001
    Little Endian:

    Code:
    00010001 00001001 00000000 00000000
    Little Endian "reverses" the bytes around from what you might naturally expect the number to be.

    What they mean by saying 9 and 17 is that one byte in the answer taken by itself makes up the number 9 and the non-zero byte by itself makes up the number 17. Your book might be assuming natural arrangement, such as how the number would be represented in registers and such (and I suppose that might not really truly be endian-independent), but to ask specifically about memory deserves such a long answer.

    Now taking your second question.... Unless they clarified the question to a purely mathematical sense, the question is again an answer of "we simply don't know". The problem lies in the ++ and -- operators. In C and C++, we can't guarentee the order in which they will be executed inside of a function. One might be operated before the other.

    Take this example:

    Code:
    int x = 5;
    int z = ++x + --x
    It might resolve to any of these:

    Code:
    z = 6 + 5 = 11
    Code:
    z = 5 + 4 = 9
    Why? Because compilers are free to set the order of those operations. Your book is again appearing to assume way too much with regard to how a system will implement such expressions.

    I took from your example and wrote this bit of code:

    Code:
    #include <iostream>
    
    int main()
    {
    	int a = 9;
    	int b = 1;
    	int c = 2;
    	int Result = c-- + (b++ - --b) * a + c;
    	
    	std::cout << "Result = " << Result << std::endl;
    	
    	return 0;
    }
    My output:

    Result = 4
    So, as you can see, you can't rely on how such an expression will be resolved since we all disagree on what the answer is.

    I suggest you consider getting a different book to learn from.
    Last edited by MacGyver; 06-24-2007 at 10:55 PM.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    The first exercise assumes a lot. Let's assume that when we say "first byte", we're talking about the fist sequence of eight bits that aren't all zero, counting bytes from the big end, but counting bits within them from the small end. Then
    Code:
    2321 = base2(0000100100010001)     //if this isn't duh to you, just look up binary representation. No big deal.
    2321 -> 00001001  00010001
    00001001 = base10(9)
    00010001 = base10(17)
    I think that's right.

    *beat me to it*
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Quote Originally Posted by MWalden View Post
    Hey I am new to programming, and I am reading a book and it has questions and answers after every section. One of them asks:
    How would the number 2321 be spanned through memory? and the answer is:
    1st byte 9 and 2nd byte 17

    What do they mean by this? I've looked through the section again and do not understand the question or the answer.


    Also it asks:
    What would be the result of the following operation:
    int Result, a, b, c;
    a = 9;
    b = 1;
    c = 2;
    Result = c-- + (b++ - --b) * a + c
    They have the answer as 11, but I have the answer as 21. Can someone explain to me how they have that answer and what I did wrong. Here is what I did:
    first I did my post and prefix operation which make the problem like this:
    result = 2 + (1 - (-1)) * 9 + 1
    result = 2 + (2) * 9 + 1
    result = 2 +18 + 1
    result = 21

    The b gets added 1 to the taken directly away again right??? since it is a negative 1 then it is added to to make a 0 and then taken from to make a negative 1 again and then 1 - negative 1 is prositive 2.

    The c's are 2 and 1 which make 3 right? I'm confused.

    Thanks for any help.
    Nice board!
    hi sorry to say both are wrong as far as i know the answer i going to be 4
    as
    in the expression
    c=2and b=0 and a=9
    so (b-b)*a=0
    and c+c=4;
    after the expression is evaluated c=1 and b=1 and a=9;

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > (b++ - --b)
    As soon as this appeared, the whole expression (and by extension the whole program) became undefined.
    http://c-faq.com/expr/index.html

    All the explained "answers" given are plausible, wrong, and useless all at the same time.

    If you want a consistent answer, don't invoke undefined behaviour in your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And tell us what book you're reading, so that we can warn away other newbies in the future. It seems to be one dreadful thing. (Herbert Schildt, by chance?)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    Game Programming All In One by Bruno Miguel Teixeira de Sousa is the book and it does assume I program with microsoft Visual C++, but I use dev. Could someone suggest me a better book maybe?

    I like this book so far, I am probably going to finish this one first as that was the only problem I ran into so far. It gives alot of examples dealing with basic games which are fun to program. I just programed a text based craps game, pretty cool for first program. It will eventually get into windows programming, direct x, physics engines, AI, etc. I'm sure it will not explain every detail but just give the general idea of how to do it. Anyway this is the type of thing I want to learn if there are better books to accomplish this goal?

    I also want to learn how to communicate over the internet with software, for games, programs etc..


    Thanks for the help guys, I understand now.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't think it's a good idea to pack everything from basic C++ up to DirectX-based graphics and AI into one book. It sounds like a receipt for disaster.

    We have a sticky thread for book recommendations. Most of these books are about the language basics.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by MWalden View Post
    Could someone suggest me a better book maybe?
    http://www.cprogramming.com/books.html

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> http://www.cprogramming.com/books.html

    Note that those books are the ones recommended by the webmaster, not by the forum members. I would actually put C++ Without Fear on a not recommended list.

    The sticky thread here: http://cboard.cprogramming.com/showthread.php?t=74078 is what CornedBee was referring to.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MWalden View Post
    Hey I am new to programming, and I am reading a book and it has questions and answers after every section. One of them asks:
    How would the number 2321 be spanned through memory? and the answer is:
    1st byte 9 and 2nd byte 17
    Argh. I'm sick of people writing books who have no business writing books. The proper answer to that question is "I have no idea."

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by brewbuck View Post
    Argh. I'm sick of people writing books who have no business writing books. The proper answer to that question is "I have no idea."
    No problem to have an idea here...

    Take a calculator (Scientific view)
    Enter 2321

    Switch to Hex

    911

    9 goes to the High byte

    0x11 goes to the Low Byte

    Enter 11 (In Hex) switch to decimal - 17
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Ah, but what is the high and what is the low byte?

    For that matter, how do you know you have 8-bit bytes?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Assuming these things just causes trouble later on. You should seriously not make these assumptions. It only makes sense when doing math and examining binary, that we as humans, keep all of of the numbers in big-endian format. Computers are not humans, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie : a little confused ?
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 08:33 AM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  3. newbie question about linked list
    By gemini_shooter in forum C Programming
    Replies: 10
    Last Post: 04-13-2005, 10:50 PM
  4. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM