- Name the twelve basic data types in C++.
short, int, long,
unsigned short, unsigned int, unsigned long,
float, double, long double,
bool, char, wchar_t
Indicate which 5 we use most regularly. (Bolded.)
- To declare a variable, we place the variable's [data] type first, then its name/identifier. Multiple variables of the same type
can be declared at once by separating their names by commas (,). All declaration statements end in a
semi-colon (;) (as do all C++
statements).
- Whole numbers are represented by the data types short and long. We usually don't use the data type
int because it can change size (i.e.
range) when a program is moved between platforms (a combination of machine
and operating system). It is okay for the main program to return such a
value to the operating system, however, since it is the operating system which helps determine the
size of this data type.
- Fill in the following table — as appropriate.
Data Type |
Minimum |
Maximum |
Precision |
bool |
true |
false |
N/A |
char |
'\0' |
ASCII |
N/A |
short |
-32768 |
32767 |
N/A |
long |
-2147483648 |
2147483647 |
N/A |
float |
1e-38 |
1e+38 |
6 digits |
double |
1e-308 |
1e+308 |
15 digits |
- What does the keyword unsigned do to an
integer data type? (Hint: there are two effects.)
unsigned disallows negative values from being stored and uses
the 'bits' once used to store negatives to double the positive range of
values you can store. (i.e. an unsigned short will allow you to
store from 0 to 65535 instead of from -32768 to 32767.)
- What does the keyword const do to the
declaration of a memory location? (Hint: there are 'two'
effects.)
A constant must be immediately initialized and will forever after
be considered unchangeable.
-
TRUE✗ | | FALSE✓ | |
Division works identically on both integer and floating point data
types. |
TRUE✓ | | FALSE✗ | |
The remainder operation (modulo) works only on integer data types.
It is represented by the %
operator. |
TRUE✗ | | FALSE✓ | |
We often use arithmetic operations on non-numeric data types (such
as bool or char). |
- Show how to use modulo to turn a whole number of cents into an equivalent number of dimes, nickles, and
pennies. You can use the following
declarations as a starting point -- assume that the cents has already been initialized
somehow.
long cents, dimes;
short nickles, pennies;
dimes = cents / 10;
nickles = cents % 10 / 5;
pennies = cents % 10 % 5;
- Tell what data type you would use and why
for each of the following situations.
- population of an ant
farm (for a hint, click the link, do a little math with the
dimensions of an ant farm, and think about its contents -- there isn't
a picture at the site, they seem to have removed all of the figures,
but you can try to Google "ant farm"
dimensions and you should find several
images)
unsigned short or short
These small devices hold dirt and air in addition to the ants.
There cannot be too awfully many ants. Also we'll not be counting
partial ants.
- population of Chicago
unsigned long or long
There are a hideous number of people in Chicago. But we don't
typically count parts of people we find laying around.
- weekly salary of a McDonalds employee
double
There'll be decimal places!
- first initial of student
char
A letter is a sub-category of character.
- yearly salary of the President
double
There'll be decimal places!
- if a student is currently enrolled at Harper
bool
This query will be either true or false.
- Briefly explain the concept of the 'magic' number. Be sure to include in
your discussion:
- Why don't we like them?
- How do we get rid of them in a program?
- Are all numeric literal values magic?
- Why don't we like them?
They operate as if by magic! They've no sense. No rhyme. No reason.
- How do we get rid of them in a program?
Make a clearly-named constant and store the value in it.
- Are all numeric literal values magic?
No. When we do something mathematically obvious (not 3.1415926!!!)
like (a+b)/2 — the 2 is taking an average of the two values
we added.
- Explain the term 'literal'. Give an example for each of the five data
types we use most regularly. (See above for which types those are.
*grin*)
A literal is a value that is literally placed into the source code by
the programmer. A literal value represents itself rather than having
a name or memory location.
short 5
long 5L
double 5.0
char '5'
bool true