Quick Review
Integers are used for holding whole numbers. When using integers, keep an eye out for overflow and integer division problems.
Floating point numbers are used for holding real numbers (which can have fractional components). When using floating point numbers, keep an eye out for precision issues, rounding errors, and comparison issues.
Boolean values hold only true and false. They do not have any major issues.
Char values are integers that can be interpreted as an ASCII value. When using chars, be careful not to mix up ASCII code values and numbers, and watch for overflow and integer division problems.
Use the const keyword to declare symbolic constants instead of #define. It’s safer.
Comprehensive quiz
1) Why are symbolic constants usually a better choice than literal constants? Why are const symbolic constants usually a better choice than #defined symbolic constants?
2) Pick the appropriate data type for a variable in each of the following situations. Be as specific as possible. If the answer is an integer, pick a specific integer type (eg. short) based on range. If the variable should be unsigned or const, say so.
a) The age of the user (in years)
b) Whether the user wants color or not
c) pi (3.14159265)
d) The number of pages in a textbook
e) The price of a stock in dollars (to 2 decimal places)
f) How many times you’ve blinked since you were born (note: answer is in the millions)
g) A user selecting an option from a menu by letter
h) The year someone was born
3) Declare each of the above using Caste Hungarian Notation. Pick a good variable name. Don’t forget to assign values to any const variables.
4) Write the following program: The user is asked to enter 2 floating point numbers (use doubles). The user is then asked to enter one of the following mathematical symbols: +, -, *, or /. The program computes the answer on the two numbers the user entered and prints the results. If the user enters an invalid symbol, the program should print nothing.
Example of program:
Enter a value: 7 Enter a second value: 5 Enter one of the following: +, -, *, or /: * 7 * 5 is 35
Hint: You can check if the user has entered a plus symbol using an if statement (briefly covered in section 2.6) and the equality operator (used to compare two values for equality): if (chUserInput == '+') // fill in rest of code here
Solutions
3.1 — Precedence and associativity
|
Index
|
2.9 — Hungarian Notation
|
3.1 — Precedence and associativity
Index
2.9 — Hungarian Notation
Alex, your tutorial is very well presented. It would be even more helpful if you could provide more problems to work, or, if you could suggest a link where we might find some appropriate problems and exercises to work. Thanks.
Thank you for your thoughts. I do plan on returning to these older sections and adding more examples. However, this will probably happen after I finish writing the majority of the content sections. In the meantime, I suggest checking other C++ tutorial sites. Alternatively, most books provide sample questions for you to tackle (though few provide answers).
Allen01, I’ve found that the learncpp.com site has provided a strong background to work with. I am now reviewing a book by Duffy that is topic specific. I bring this up because Duffy also maintains a forum where readers can post questions (and possibly get answers to problems that Alex mentions don’t generally come with answers). In my area of study, another prominent author is Joshi, and he does the same. I bring this up because after reviewing and learning the material on this site, it may be advantageous to branch out into areas of interest and participate in the forums that those authors maintain. Just my $0.02.
You never explained if statements :( was confusing me through many lessons until i asked a friend.
I talked briefly about if statements in the section on boolean variables. I’ll change the hints on quiz question 4 to be slightly more explicit through, since I can see how this would be confusing.
Thanks alex :) BTW best guides ever :) Been helping me, i like the way you do it, instead of trying to explain everything the most confusing way possible you kind of leave A bit for the imagination with IMO is much better then reading all this confusing stuff witch can just make you a bad programmer.
i think i learn more from these quizzes than any other part of the tutorial
yea it’s good, but so simple at this point. If I wanted to make that above program for real, it would need to be a bit more complicated. Who wants their program to just end? I would repeatedly remind the user to enter an appropriate choice until they did so.
I love giving wrong inputs to programs to see what happens. Sometimes wrong inputs will lead to errors later on that could have been prevented. Sometimes wrong inputs lead to invalid results. if you are writing a game, the user could use wrong inputs to cheat. I say this to all you newbies to try to keep that in mind, and fix it when you learn how. I am new to C++ and object oriented programming, but I learned on a Ti-83 mostly. Good times.
[ Cameron, I've moved your post to the forum and addressed it there. -Alex ]
[...] 2.10 — Comprehensive quiz [...]
[...] 2007 Prev/Next Posts « 2.8 — Constants | Home | 2.10 — Comprehensive quiz » Monday, June 11th, 2007 at 4:04 [...]