🎉 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, 1 month ago

Quizzes and Extra Credit for Week 2.

Post your answers to the Quizzes from Week 2 here rather than in the chapter thread, so as not to spoil things for the others. Chapter 3 Quiz 1. What is a variable? 2. What happens to a variable when the program terminates? 3. What purpose does the "label" or identifier serve with respect to variables? 4. What purpose does the type serve with respect to variables? 5. Is C++ a "Strongly typed" or "Weakly typed" language...what does that mean? 6. Does an integer require the same amount of memory each time a program is run on the SAME computer? 7. Does an integer require the same amount of memory on EVERY computer? 8. What can be stored in a 'char' variable type? Integers, characters, both? 9. How large is a 'char' variable usually 9. What are the usual sizes for 'short int', 'int', and 'long int' data types? 10. What does the C++ Standard say about the size of integers? 11. How big are integers on a 32bit x86 processor using a modern compiler? 12. What operator can you use to determine the size of a data type? 13. Are integers signed or unsigned by default? 14. What are the minimum and maximum values for signed and unsigned long integers (4 byte integer)? 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? 16. Is C++ a case-sensitive language? What does this mean? Show some examples. 17. What are the Do's and Dont's of naming variables according to your textbook? 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? 20. What does the "typedef" keyword do? Why is it useful? 21. When do you use short, int, or long? 22. What happens when you attempt to store a value bigger than a variable can hold? 23. What happens when you attempt to store a value smaller than a variable can hold? 24. What are the TWO things 'char' variables can be interpreted as? What is the relationship between these two things? 25. When must you initialize constants? 26. What is the difference between a literal constant, and a symbolic constant? Show some examples. 27. What are the two ways covered in this chapter for declaring a constant? Which is better? 28. How do you declare an enumeration? 29. [Extra Credit] Site some possible examples where an enumeration might be a useful data type.
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
Advertisement
1) What is wrong with the following example?

enum Colour;int main(){  Colour MyColours;}enum Colour{  Red,  Blue,  Green};


2) What, if anything, is wrong with the following code?

extern int MySmellyGlobal;int main(){  MySmellyGlobal = 0;}int MySmellyGlobal;


3) What, if anything, is wrong with the following code?

extern int MySmellyGlobal = 0;int main(){  MySmellyGlobal = 0;}int MySmellyGlobal;



4) What, if anything, is wrong with the following code?

int main(){  typedef int Colour;  int foo = 0;  Colour MyColour = 10;  foo = MyColour;}



5) What, if anything, is wrong with the following code?

#define ColourPointer int*typedef int* ColourPointer_t;int main(){   const ColourPointer pMyColour = 0;  const ColourPointer_t pMyColour_t = 0;  pMyColour_t = pMyColour;}


6) What type does the following string literal have?

int main(){  "String Literal";}


7) What, if anything, is wrong with the following code?

int main(){   const char String[15] = "String Literal";   char* pString = String;}


8) What, if anything, is wrong with the following code?

int main(){   char* pString = "String Literal";}


9) Why does example 8 compile when example 7 does not?

10) How many bits are in a C++ byte?




1. What is a variable? A place to store information
2. What happens to a variable when the program terminates? It is gone
3. What purpose does the "label" or identifier serve with respect to variables? Identifies a memory address
4. What purpose does the type serve with respect to variables? Lets the compiler know how much memory to set aside
5. Is C++ a "Strongly typed" or "Weakly typed" language...what does that mean? Strongly, gives errors when memory usage for a variable is incorrect.
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? 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, 2
10. What does the C++ Standard say about the size of integers? Not a lot, short <= int <= 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? 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)? Signed -2,147,483,648 to 2,147,483,647, unsigned 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? An extra 0 or 1 to identify + or – for signed will be used as an extra factor of 2 in the unsigned.
16. Is C++ a case-sensitive language? What does this mean? Show some examples. Yes, it differentiates between upper and lower case, myAge or MyAge.
17. What are the Do's and Dont's of naming variables according to your textbook? Do’s: Name by type, meaningful names, case sensitive, know the number of bytes. Don’t’s: Don’t use keywords as variables, don’t assume how many bytes, don’t use unsigned variables for negative numbers.
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 Width = 5;
20. What does the "typedef" keyword do? Why is it useful? Creates an alias for a type definition.
21. When do you use short, int, or long? Depends on the range of data you can expect to use.
22. What happens when you attempt to store a value bigger than a variable can hold? Cycles over to the smallest value possible for that type
23. What happens when you attempt to store a value smaller than a variable can hold? Cycles to largest available value
24. What are the TWO things 'char' variables can be interpreted as? What is the relationship between these two things? ASCII value or character symbol, the ASCII value represents the symbol.
25. When must you initialize constants? When you create it
26. What is the difference between a literal constant, and a symbolic constant? Show some examples. Literal means the value is what it is, like 37, it will always be equal to 37. symbolic would be assigning a value to variable, const int myAge = 30
27. What are the two ways covered in this chapter for declaring a constant? Which is better? #define and const, const is better #define is obsolete.
28. How do you declare an enumeration? Enum Thing { Thing1, Thing2, …};
29. [Extra Credit] Site some possible examples where an enumeration might be a useful data type. Days of week, Months of year, standard lists
1. What is a variable?
A place to store information.

2. What happens to a variable when the program terminates?
It disapears.

3. What purpose does the "label" or identifier serve with respect to variables?
The label is the link the the actual data stored in memory.

4. What purpose does the type serve with respect to variables?
This tells the compiler how much memory to set aside for the variable.

5. Is C++ a "Strongly typed" or "Weakly typed" language...what does that mean?
Strongly typed, which means that you will receive an error when you attempt to store data of a wrong type in a variable.

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.

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 bytes, 4 bytes and 4 bytes, respectively.

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?
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 - 2,147,483,648 and 0 - 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?
Unsigned integers need additional space to hold negative numbers, thus the short 'span' for the signed ints.

16. Is C++ a case-sensitive language? What does this mean? Show some examples.
Yes. 'Age' is different from 'age'.

17. What are the Do's and Dont's of naming variables according to your textbook?
Dos: Define via writing the type, then name, use meaningful names, realize C++ is case-senstive, and understand the amount of space needed to store each different variable type:
Don'ts: Use C++ keywords as variables, make assumptions about memory, Using unsigned variables to hold negative numbers.

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?
Yes, Int myage=39;

20. What does the "typedef" keyword do? Why is it useful?
It creates aliases for different variable types, thus not forcing you to have to type 'unsigned short int' all the time.

21. When do you use short, int, or long?
If you ever believe your value will exceed a certain type's limits, you use the larger type.

22. What happens when you attempt to store a value bigger than a variable can hold?
It wraps around to the lowest possible value stored for that variable.

23. What happens when you attempt to store a value smaller than a variable can hold?
It removes the negative sign from the variable.

24. What are the TWO things 'char' variables can be interpreted as? What is the relationship between these two things?
Either a small number up to 255 or a member of the ASCII set. Each numeral is the ASCII sets relates to a different type of 'character'.

25. When must you initialize constants?
When you create them.

26. What is the difference between a literal constant, and a symbolic constant? Show some examples.
A literal constant is an actual number and a symbolic constant is a constant represented by a name.
Literal: int myage = 27;
Symbolic: students = classes * 15;

27. What are the two ways covered in this chapter for declaring a constant? Which is better?
#define and const. Const is better because it allows you to declare a type for your constant, and C++ will enforce that type.

28. How do you declare an enumeration?
enum name {value1, value2};

29. [Extra Credit] Site some possible examples where an enumeration might be a useful data type.
Lists of colors, sizes, days, and character classes.
Only 2 answers for the quizz? C'mon guys! :) A new Workshop week is about to begin, thus you won't have much time to answer these quizz questions (and the tutors won't have much time to answer your own questions regarding this quizz). Hurry up, hurry up! [smile]
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?

I didn't look it up but I think I remember one of the bits is used as +/- ( being 0/1) sign. This way you have one 1 or one 0 less to represent the value, meaning that you can only store a number half as big. So a signed int's maximum absolute value is half that of unsigned int, but you can represent just as much numbers with it (well, one less because 0 = -0).

Did that make sense?
Quote: Original post by twoaterisn
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?

I didn't look it up but I think I remember one of the bits is used as +/- ( being 0/1) sign. This way you have one 1 or one 0 less to represent the value, meaning that you can only store a number half as big. So a signed int's maximum absolute value is half that of unsigned int, but you can represent just as much numbers with it (well, one less because 0 = -0).

Did that make sense?


It make sense for floating point values (the high order bit of a IEEE float or IEEE double is the sign bit), but integer values are handled in a different way.

Any more answer?
I can understand the logic for signed short and long int being symmetrical around zero and being based upon 2 raised to a specific power.

According to the table inside the back cover, float, double and long double have ranges between 1.2e-38 to 3.4e38, 2.2e-308 to 1.8e308 and 3.4e-4932 to 1.1e4932. How exactly are these ranges derived? Are they also related to a simple calculation involving 2 raised to a certain power?
Most of this post is <OPTIONAL> and maybe <CONFUSING>. Thou are warned [smile].

Quote: Original post by CondorMan
I can understand the logic for signed short and long int being symmetrical around zero and being based upon 2 raised to a specific power.

According to the table inside the back cover, float, double and long double have ranges between 1.2e-38 to 3.4e38, 2.2e-308 to 1.8e308 and 3.4e-4932 to 1.1e4932. How exactly are these ranges derived? Are they also related to a simple calculation involving 2 raised to a certain power?


<OPTIONAL>
No, it is much more complicated. An IEEE floating point value is made of 3 parts: the sign bit, the mantissa and the exponent. The exponent is used to raise the mantissa to a power of 10 (the number itself is a binary representation of the exponent). Reading the mantissa is even more complex. If you want more information about the IEEE floating point format, check this wikipedia entry.
</OPTIONAL>

Quote: Original post by jwalsh
Actually, that is how it's done for integers. The most significant bit is the sign bit. For example, a long has 4 bytes:

// Full range of values (32 bits)
(2^32) - 1 = 4,294,967,295

// Reserving the most significant for sign leaves 31 bits
(2^31) - 1 = +/- 2,147,483,647

You must subtract 1 to account for 0, which is one of the possible values.


I now understand what you want to say (I wasn't when I sent you my PM). I tend to disagree with your explaination.

<OPTIONAL>
The signed integer representation is not symetric. 0 to 2,147,483,647 is represented on the range 0x00000000 to 0x7fffffff (hexadecimal), while -1 to 2,147,483,648 is represented on the range 0x80000000 to 0xffffffff. If the most significant bit was a real sign bit, it would mean that since 1 is 0x00000001, -1 would be 0x80000001. This is not the case. -1 is in fact 0xffffffff. The following code shows it:
#include <iostream>int main(){  std::cout << "the hexadecimal representation of -1 is " << std::hex << -1 << std::endl;}

So, how does it work? It works by using a technique which is called two's complement. Again, the corresponding wikipedia article might give you more information that I would.
To create the two's complement of a unsigned integer, reverse all the bits of the value and add one. Here is a 16 bit example (using the 'short' type):
// 345 == bin 0000 0001 0101 1001 == hex 0159//     => bin 1111 1110 1010 0110 == hex fea6//  +1 => bin 1111 1110 1010 0111 == hex fea7 == -345#include <iostream>int main(){  short s = -345;  std::cout << "the hexadecimal representation of " << s << " is " << std::hex << s << std::endl;}

The Wikipedia link give you a good way to count the value of either a signed or an unsigned integer: if the integer is unsigned, the value of the most significant bit is 2n-1 (16 bits: 32,768). If the integer is signed, the value is -2n-1 (16 bits: -32,768). Let's check using our example:
1       -32,7681       +16,3841       + 8,1921       + 4,0961       + 2,0481       + 1,0241       +   5120       +     01       +   1280       +     01       +    320       +     00       +     01       +     41       +     21       +     1        -------        -   345
And voilà [smile]
In a sense, the most significant bit is a sign bit since its associated absolute value is always bigger than the other one - meaning that if it is set, the signed integer is always negative. However, there is a huge difference with the floating point sign bit: the floating point sign bit has no associated value while we've just seen that the integer sign bit has.
</OPTIONAL>

I hope I made it clearer :)
Quote: Original post by jwalsh
Actually, that is how it's done for integers. The most significant bit is the sign bit. For example, a long has 4 bytes:

// Full range of values (32 bits)
(2^32) - 1 = 4,294,967,295

// Reserving the most significant for sign leaves 31 bits
(2^31) - 1 = +/- 2,147,483,647

You must subtract 1 to account for 0, which is one of the possible values.

Cheers!


I would like to apologize for my post. I was attempting to make an over simplification so that people would understand that the most significant bit played a role in the sign of integers. I was hoping to avoid a discussion about Two's Compliment at this point as many people are new to programming.

But my over-simplification proved faulty in the analysis. So for that I apologize. You've already received a thorough description of Two's Compliment, so I wont bother doing it again.
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

This topic is closed to new replies.

Advertisement