🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Week 2 (Ch. 3) - Quizzes and Extra Credit

Started by
18 comments, last by Dbproguy 16 years, 2 months ago
I'll just take 4 bits for my question:

what you are saying is:

0111 = 7
1111 = -1 (and not -7, what i thought)
1000 = -8 (and not -0)

then how come the signed int range is -2,147,483,647 to 2,147,483,647 and not -2,147,483,648 to 2,147,483,647 ? or is it?
Advertisement
Quote: Original post by twoaterisn
I'll just take 4 bits for my question:

what you are saying is:

0111 = 7
1111 = -1 (and not -7, what i thought)
1000 = -8 (and not -0)

then how come the signed int range is -2,147,483,647 to 2,147,483,647 and not -2,147,483,648 to 2,147,483,647 ? or is it?


The range IS -2,147,483,648 to 2,147,483,647.

It was like 1am in the morning when I posted the simplification, and clearly it was an incorrect simplification.

The important thing to note is that the most signficant bit is a reflection of the sign, that is - if it's 1...the value will be nagative, if it's 0, the value will be positive. What the actual value is, isn't a straight 2^31-1 computation, but rather determined by 2's complement, which enables easy addition and subtraction of negative numbers.

As Emmanuel already pointed out, you can find the best description of Two's Complement on Wikipedia Here.

So to summarize:
If the sign bit is 0, the value will be positive
If the sign bit is 1, the value will be negative

If the sign bit is 0, the value of the integer is just 2^31.
If the sign bit is 1, the value of the integer is determined using Two's Complement. (All the 0's becomes 1's, all the 1's become 0's...then add 1)

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
hey, sorry to keep barging on about this one, but I'd like to indicate that this 2's complement thing explains wrap-around. (Bet you all noticed that allready)

with 4 bits for simplicity:

7+1= 0111 + 0001     //binary= 1000            //binary= -8 + 0 + 0 + 0= -8
[advanced]
If you play with the maximum negative value (ie the smallest value) of an integer, you'll notive a strange property. Let's take again the 4 bit integer example:
  -8  =  1 0 0 0-(-8) =  0 1 1 1 + 1 (two's complement) = 1 0 0 0 = -8

If you don't find this very logic, remember that you can encode only integer numbers from -8 to +7 (in this 4 bit integer case), thus -(-8) can't be represented - and cause a wrap around.

I only say this because this little tricky case can happen (for example, in this test: if (-(x) < 0) { }) and can be a real mess to debug. However, you're not likely to deal with this kind of problem right now, so you can safely put this issue in a small box in your head and move forward [smile]
[/advanced]

Regards,
Chapter 3 Quiz Questions & Answers

1. What is a variable?
1a. A place to store information.

2. What happens to a variable when the program terminates?
2a. It is “lost” and the memory for it is reclaimed by the operating system.

3. What purpose does the "label" or identifier serve with respect to variables?
3a. It identifies the memory address where the data is stored.

4. What purpose does the type serve with respect to variables?
4a. It tells the compiler the size of data to allocate for the variable, and in some cases how to interpret the data.

5. Is C++ a "Strongly typed" or "Weakly typed" language...what does that mean?
5a. It is a Strongly Typed language. It means the context in which a variable can be used depends upon its type. Attempting to use a variable of a specific type in the wrong context is a syntax error.

6. Does an integer require the same amount of memory each time a program is run on the SAME computer?
6a. Yes.

7. Does an integer require the same amount of memory on EVERY computer?
7a. No. Depending on platform and operating system an integer can require different amounts of memory.

8. What can be stored in a 'char' variable type? Integers, characters, both?
8a. Both. ‘char’ variables are actually an integer type that can store 256 values. These values are, by default, interpreted as indices into the ASCII character table, representing a character. However you can instead use the integer value itself, if you simply need a place to store a small number.

9. How large is a 'char' variable usually
9a. It is usually 1 byte.

10. What are the usual sizes for 'short int', 'int', and 'long int' data types?
10a. 2 bytes, 4 bytes, and 4 bytes.

11. What does the C++ Standard say about the size of integers?
11a. Short must be less than or equal to ‘int’, which must be less than or equal to ‘long’.

12. How big are integers on a 32bit x86 processor using a modern compiler?
12a. 32 bits. (4 bytes)

13. What operator can you use to determine the size of a data type?
13a. the sizeof() operator.

14. Are integers signed or unsigned by default?
14a. They are signed by default.

15. [Extra Credit] What are the minimum and maximum values for signed and unsigned long integers (4 byte integer)?
15a. Unsigned Long: 0 to 4,294,967,295. Signed Long: -2,147,483,648 to 2,147,483,647

16. [Extra-Extra Credit] Why are those the minimum/maximum values? That is, how does the C++ language treat signed vs. unsigned integers differently on a "binary" level?
16a. The most significant bit is used to indicate the “sign” of the value. If the sign bit is 0, the value is positive and the remaining 31 bits are interpreted as usual. If the sign bit is 1, then the number is negative and a process known as two’s compliment is used to determine the value of the negative number. The process of removing the most significant bit from the range of values and then performing two’s compliment for negative numbers gives the range of possible values listed above.

17. Is C++ a case-sensitive language? What does this mean? Show some examples.
17a. C++ is a case sensitive language. This means that identifiers, keywords, etc…are sensitive to uppercase/lowercase distinction. For example; int myVariable is not the same variable as int MyVariable. (due to the M being capitalized). As well, ‘int’ is a keyword in C++, however, ‘Int’ is not.

18. What are the Do's and Dont's of naming variables according to your textbook?
18a.Do define a variable by writing the type, then the variable name.
Do use meaningful variable names.
Do remember C++ is case sensitive
Do understand the number of bytes each variable type consumes in memory and what values can be stored in variables of that type.
DON’T use C++ keywords as variable names.
DON’T make assumptions about how many bytes are used to store a variable.
DON’T use unsigned variables for negative numbers.

19. What operator do you use to assign a value to a variable?
19a. The Assignment Operator. Ie. =

20. Can you initialize a variable at the same time it is declared? What does this look like?
20a. Yes. int myValue = 10;

21. What does the "typedef" keyword do? Why is it useful?
21a. It allows you to create an easier to type alias for an existing data type. It is useful to prevent typing ‘unsigned long’ every time you want an unsigned long, for example. As well, it can be used to make your application more portable. By defining aliases for all the data types you use, you can easily re-define those types on a new platform or OS to ensure the size of variables remains the same.

22. When do you use short, int, or long?
22a. If any chance exists that the value you’ll want to put into your variable will be too large for its type, use the larger type.

23. What happens when you attempt to store a value bigger than a variable can hold?
23a. You get overflow, where the value wraps back around to the smallest possible value for the type.

24. What happens when you attempt to store a value smaller than a variable can hold?
24a. You get underflow, where the value wraps around to the largest possible value for the type.

25. What are the TWO things 'char' variables can be interpreted as? What is the relationship between these two things?
25a. Integers and characters. As described above, an integer is actually stored at the address in memory, however when that integer is used as an index in the ASCII lookup table it identifies a corresponding character value.

26. When must you initialize constants?
26a. When they are declared.

27. What is the difference between a literal constant, and a symbolic constant?
27a. A literal constant is a value typed directly into your source code wherever it is needed. The number 10 for example or the string “hello world” are both literal constants. A symbolic constant, however, is a symbol which represents a literal constant. See question 28 for the two ways to declare a symbolic constant.

28. What are the two ways covered in this chapter for declaring a constant? Which is better?
28a. By using the #define preprocessor directive, and by using the ‘const’ keyword. The const keyword is the better method as it is actually checked for proper use and type safety during the compile stage.

29. How do you declare an enumeration?
29a. By using the keyword enum, followed by the new enumerated type, followed by an opening brace, each of the legal values separated by a comma, and finally a closing brace with a semi colon. Example:
enum MY_ENUM {    MY_ENUM_ZERO,    MY_ENUM_ONE,    MY_ENUM_TWO};

30. [Extra Credit] Site some possible examples where an enumeration might be a useful data type.
30a. Any time you have a relatively small, well defined set where knowing the names might be useful for reading the code. An example might be the suits in a deck of cards, the days of the week, the months of a year, names of colors in a color set, Gender in a database, etc…

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Thank you. Whilst we can (and do) go through the book to find the answers, it's nice to have the "official" ones available for comparison!
Quote: Original post by jwalsh
Chapter 3 Quiz Questions & Answers
9. How large is a 'char' variable usually
9a. It is usually 1 byte.

<advanced type=information priority=verylow>
The byte jwalsh is refering to is a machine byte - ie made of 8 bits. It is different from the byte definition of the C++ standard where the number of bits is not specified. According to the C++ standard, a char is always one (C++ standard) byte, but the exact machine representation of the (C++ standard) byte is left to the compiler vendor. Most of the time (hence the emphasized 'usual' in jwalsh's question, the (C++ standard) byte meet the (machine) byte, and is 8 bit long (although It's possible to have a compiler implementation for specialized processors where a (C++ standard) byte is 32 bits in order to avoid unaligned memry access).
So, to be more precise, the usual let's say that size of 'char' variable is 8 bits.
</advanced>
1. What is a variable? A place to store information.

2. What happens to a variable when the program terminates? Clears from the memory.

3. What purpose does the "label" or identifier serve with respect to variables? So that you can find it easily without knowing its actual memory address.

4. What purpose does the type serve with respect to variables? How much memory is being taken up, and what kind of number/char is being stored.

5. Is C++ a "Strongly typed" or "Weakly typed" language...what does that mean? Strongly Typed, when all the variables are pre-defined.

6. Does an integer require the same amount of memory each time a program is run on the SAME computer? Yes

7. Does an integer require the same amount of memory on EVERY computer? No

8. What can be stored in a 'char' variable type? Integers, characters, both? All characters can be stored as a char. If you store a number, it wont be considered a normal integer, it will be looked at like a number in relation to the keyboard.

9. How large is a 'char' variable usually? 1 byte

9. What are the usual sizes for 'short int', 'int', and 'long int' data types? 2, 2, 4

10. What does the C++ Standard say about the size of integers? Short must be less than or equal to ‘int’, which must be less than or equal to ‘long’.

11. How big are integers on a 32bit x86 processor using a modern compiler? 4 bytes

12. What operator can you use to determine the size of a data type? sizof()

13. Are integers signed or unsigned by default? signed

14. What are the minimum and maximum values for signed and unsigned long integers (4 byte integer)?
unsigned long int - 0 to 4,294,967,295
signed long int = -2,147,483,648 to 2,147,483,647

15. [Extra Credit] Why are those the minimum/maximum values? That is, how does the C++ language treat signed vs. unsigned integers differently on a "binary" level? There is a bit dedicated to sign, 0 is positive, 1 is negetive.

16. Is C++ a case-sensitive language? What does this mean? Show some examples. Yes in identifiers and keywords. myVar = 3 is not the same as myvar = 3.

17. What are the Do's and Dont's of naming variables according to your textbook? Don't name them 4j32j23lk4t or randomness, try to name them in relation to what your using it for. Ex. iAreaOfCircle, fDepth (Im using the hungarian style of name by putting i (integer) or f (float) in front of it.)

18. What operator do you use to assign a value to a variable? =

19. Can you initialize a variable at the same time it is declared? What does this look like?
int myAge = 39, yourAge, hisAge = 40;

20. What does the "typedef" keyword do? Why is it useful? instead of writing 'unsigned short int' everytime you can put that into a variable, 'typedef unsigned short int USHORT' now just type USHORT to achieve the same objective as 'unsigned short int'.

21. When do you use short, int, or long? If there is any chance that the value you'll want to put into your variable will be too big for its type, use a larger type.

22. What happens when you attempt to store a value bigger than a variable can hold? It will wrap to the smallest value.

23. What happens when you attempt to store a value smaller than a variable can hold? Wraps to largest value.

24. What are the TWO things 'char' variables can be interpreted as? What is the relationship between these two things? Integers and characters. An integer is actually stored at the address in memory, when that integer is used as an index in the ASCII lookup table it identifies a corresponding character value.

25. When must you initialize constants? When they are declared.

26. What is the difference between a literal constant, and a symbolic constant? Show some examples. myAge = 19; 19 being the literal constant, symbolic would be assigning a name to a literal constant, myAge = ageOfCouryDitch

27. What are the two ways covered in this chapter for declaring a constant? Which is better?
#define or const

28. How do you declare an enumeration?
enum myEnum
{
enum1, enum2, enum3
};

29. [Extra Credit] Site some possible examples where an enumeration might be a useful data type. Days of week, colors, months, gender, etc.


Chapter 3.

.What is a variable?
A) In c++, a variable is a place to store information. A variable is a location in your computer's memory in which
you can store a value and from which you can later retrieve that value.

.What happens to a variable when the program terminates?
A) Then the information stores in that variable is lost.

.What purpose does the "label" or identifier serve with respect to variables?
A)

.What purpose does the type serve with respect to variables?
A) It determines what the amount of memory the compiler must reserve for that particular variable.

.Is C++ a "Strongly typed" or "Weakly typed" language... what does that mean?
A) C++ is a "Strongly typed" language, it means that your compiler will warn you or give an error message when you
try to store a wrong type of variable in the reserved memory.

.Does an integer require the same amount of memory each time a program is run on the SAME computer?
A) Yes, it does.

.Does an integer require the same amount of memory on EVERY computer?
A) No, it depends on the operating system, whether it's a 32-bit or 64-bit computer and which compiler you use.

.What can be stored in a 'char' variable type? Integers, characters, both?
A) Characters solely, but they can output integers as characters.

.How large is a 'char' varialbe usually?
A) Usually, it is 1 byte.

.What are the usual sizes for 'short int', 'int', and 'long int' data types?
A) short int = 2 bytes (32-bit), int = 4 bytes (32-bit), and long int = 4 bytes (32-bit).

.What does the C++ Standard say about the size of integers?
All it says is that a short must be less than or equal to the size of an int, which, in turn, must be less or equal
to the size of a long.

.How big are integers on a 32bit x86 processor using a modern compiler?
A) They're 4 bytes.

.What operator can you use to determine the size of a data type?
A) You can use the 'sizeof()' operator.

.Are integers signed or unsigned by default?
A) They are signed by default.

.What are the minimum and maximum values for signed and unsigned long integers( 4 byte integer)?
A) signed = -2.147.483.648 to 2.147.483.647 and for unsigned = 0 to 4.294.967.295.

.Why are those the minimum/maximum values? That is, how does the C++ language treat signed vs. unsigned integers
differently on a "binary" level?
A)Because 4 bytes for an unsigned long have a maximum of 32-ones(I), incrementing every bit from left to right with
the power of 2, you get a maximum value of the one mentioned above, seeing as you get negative numbers with a signed
long, you half the maximum value by two.

.Is C++ a case-sensitive language? What does this mean? Show some examples.
A) Yes it is case-sensitive, it means that when using a variable in different places in your code, it MUST be
written EXACTLY the same way as it was declared. Some examples are:
- myAge is NOT the same as MYAGE or myage
- highAltitude is NOT the same as HIGHALTITUDE or highaltitude.

.What are the Do's and Dont's of naming variables according to your textbook?
A) DO DON'T
DO define a variable by writing the type, DON'T use C++ keywords as variable names
then the variable name.
DO use meaningfull variable names. DON'T make assumptions about how many bytes are used to
store a variable.
DO remember that C++ is case sensitive. DON'T use unsigned variables for negative numbers.
DO understand the number of bytes each variable
type consumes in memory and what values can
be stored in variables of that type.

.What operator do you use to assign a value to a variable?
A) The equal to (=) operator is used for that.

.Can you initialize a variable at the same time it is declared? What does this look like?
A) Yes you can, it looks like this then: int myInt = 5;

.What does the "typedef" keyword do? Why is it useful?
A) It creates an alias for the used variable, it's useful because you can make shorter names for long types and it becomes less error
prone.

.When do you use short, int, or long?
A) Depending on what the maximum value is you want to store in the variable.

.What happens when you attempt to store a value bigger than a variable can hold?
A) It wraps around and starts over from the lowest possible value.

.What happens when you attempt to store a value smaller than a variable can hold?
A) It wraps around and starts over from the highest possible value.

.What are the TWO things 'char' variables can be interpreted as? What is the relationship between these two things?
A) It can be interpreted as a small number(0-255) or as a member of the ACSII set. That the value/letter is arbitratry.

.When must you initialize constants?
A) You must initialize a constant when you create it.

.What is the difference between a literal constant, and a symbolic constant? Show some examples.
A) A literal constant is a certain value typed directly into your program wherever it is needed. A symbolic constant is a constant
that is represented by a name, just as a variable is represented. Unlike a variable however, after a constant is initialized, its
value can't be changed. Examples are: int myAge = 39; where 39 is the literal constant and const MAXARRAY = 15; where MAXARRAY is the
symbolic constant.

.What are the two ways covered in this chapter for declaring a constant? Which is better?
A) The two ways are: using #define and using const, using const is better because you can maintain your code easier and prevent bugs.
The biggest difference however is that the const has a type and the compiler can enforce that it is used according to its type.

.How do you declare an enumeration?
A) Like this: enum DAYSOFTHEWEEK { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };

.Site some possible examples where an enumeration might be a useful data type.
A) Days of the week, months of the year, days in a month, colors, etc...

1. What is a variable?
A temporary place for memory storage

2. What happens to a variable when the program terminates?
All data within the variable is deleted.

3. What purpose does the "label" or identifier serve with respect to variables?
The variables name

4. What purpose does the type serve with respect to variables?
What data can be stored in the variable
5. Is C++ a "Strongly typed" or "Weakly typed" language...what does that mean?
strongly typed, means you can't put anything but what is allowed for the type

6. Does an integer require the same amount of memory each time a program is run on the SAME computer?
Yes

7. Does an integer require the same amount of memory on EVERY computer?
No

8. What can be stored in a 'char' variable type? Integers, characters, both?
Both but the integers will represent characters

9. How large is a 'char' variable usually
1 byte

9. What are the usual sizes for 'short int', 'int', and 'long int' data types?
2, 4 and 4

10. What does the C++ Standard say about the size of integers?
They are an unchanged amount, different size on each computer

11. How big are integers on a 32bit x86 processor using a modern compiler?
4 bytes

12. What operator can you use to determine the size of a data type?
sizeof()

13. Are integers signed or unsigned by default?
signed

14. What are the minimum and maximum values for signed and unsigned long integers (4 byte integer)?
-2,147,483,648 to 2,147,483,647 or 0 to 4,294,967,295

15. [Extra Credit] Why are those the minimum/maximum values? That is, how does the C++ language treat signed vs. unsigned integers differently on a "binary" level?
It's the same in binary, it changes the number differently

16. Is C++ a case-sensitive language? What does this mean? Show some examples.
Yes, the variables myAge and Myage are two completely different variables.

17. What are the Do's and Dont's of naming variables according to your textbook?
write the type then the name, use meanungful names, remember C++ is case-sensitive, the number of bytes consumes in memory and what values can be stored in the variables of that type. Don't use C++ keywords as names, don't make assumptions on how many bytes are used and don't use unsigned variables for negative numbers.

18. What operator do you use to assign a value to a variable?
the equal sign (=)

19. Can you initialize a variable at the same time it is declared? What does this look like?
int myAge = 14;

20. What does the "typedef" keyword do? Why is it useful?
It can create a shortcut to a longer word type, so you can make ushort mean unsigned short int. It saves some typing

21. When do you use short, int, or long?
always use a short to consume memory unless you know the number will be large then use a long.

22. What happens when you attempt to store a value bigger than a variable can hold?
It will reset at 0, as in 65536 in an unsigned short int will display as 0, then 65547 will be 1 and so forth.

23. What happens when you attempt to store a value smaller than a variable can hold?
It will be the opposite of if it's bigger, so that in an unsigned short, -1 will rather be 65535.

24. What are the TWO things 'char' variables can be interpreted as? What is the relationship between these two things?
integers or characters, integers are assigned without quotation marks. Like 5 without quotation marks is just 5, if you add the quotation marks then it's a character, the integer is 53 though.

25. When must you initialize constants?
Upon declaration for they can't be assigned any other value afterwards.

26. What is the difference between a literal constant, and a symbolic constant? Show some examples.
A literal constant is just a number. A symbolic constant is usually a variable or constant. const int myAge = 14; is a literal constant. int age = 14; const int myAge = age; would be a symbolic constant.

27. What are the two ways covered in this chapter for declaring a constant? Which is better?
#define and the const keyword. #define is obsolete making const better.

28. How do you declare an enumeration?
enum Seasons { SPRING, SUMMER, AUTUMN, WINTER };

29. [Extra Credit] Site some possible examples where an enumeration might be a useful data type.
In listing seasons, months, days, etc.
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life

This topic is closed to new replies.

Advertisement