Type in the following source code:
#include <iostream> int main() { int m = 13; int n = 3; int p = 023; int q = 0x52; char r = 'B'; char s = 'b'; cout << "a. " << m - n << " " << m / n << " " << m % n << endl; cout << "b. " << p << " " << q << endl; cout << "c. " << r << " " << r + 1 << " " << char (r + 1) << " " <<'r' + 1 << endl; cout << "d. " << char(s - 32) << endl; return 0; }
Save your source as dtype1.cpp and your project as dtype1.proj.
Make and run your program. You can view the output as you read through the comments to understand what is happening here.
Explanation:
#include <iostream> int main() { int m = 13; int n = 3;
We've seen all of this so far before. But now...
int p = 023;
The zero at the beginning of this number indicates that it is an octal, or base-8, number ( ... 64, 8, 1 ). The number reads from right to left. In the rightmost position, the 3 in the above definition indicates how many ones are present. In the next position there is a 2, indicating the 8's position. Mathematically, this is expressed as (8 x 2) + 3 = 19. If there was another number in there (say, 0123), then the one would be the 64's position, and it would be expressed as such:
(64 x 1) + (8 x 2) + 3 = 83.
This pattern continues with 128, 256, 512, ...
int q = 0x52;
The zero-x indicates that this number is hexidecimal, or base-16.
Hexidecimal numbers are often used with computers and programming, especially in assembly language, which is of no real use on the BeOS due to it's presence on both x86 and PPC. But you still may want to get used to some math using hexidecimal.
Quickie: Hexidecimal (base 16) is like decimal (base-10) in the way you do math. The major difference is the base 16 part. In decimal, when you add 5 + 9, you carry the ten over to the next column to the left (the tens column), giving you (10 x 1) + 4. A larger number, 47, would read: (10 x 4) + 7 = 47. An even larger number 1256 would be: (1000 x 1) + (100 x 2) + (10 x 5) + 6.
Now, with hexidecimal, you have 16 to work with. But there are only 10 single digit numbers, 0-9, so you have to use some letters in there as well, a-f. Counting in hexidecimal would be:
0, 1, 2, 3 , 4, 5, 6, 7, 8, 9, a, b, c, d, e, f
The number representing 12 decimal would be c hexidecimal (or 0xc). 16 decimal would be 10 (or 0x10), mathematically shown as (16 x 1) + 0 = 16. 17 decimal would be 11 (or 0x11) - in the "sixteens" position, you have a one, so that is (16 x 1) + 1 = 17. You can figure out hexidecimal for the decimal number 47 by (47 / 16), which is 2 remainder 15, which equals: (16 x 2) + 15, or 0x2f.
Confused? Don't worry too much about it now. Got it? Great, you must be a hexidecimal natural, because it don't come easy!
char r = 'B'; char s = 'b';
To define an actual character, you enclose it within single quotes ('). You may also define a character by its ASCII integer value. For these values, just remember:
'a' = 97 'A' = 65
You can count up from there.
You may want to get your hands on a chart (cheat sheet) that shows you conversions between binary, octal, decimal, hexidecimal and ASCII character sets. Most C++ books should contain one, to different extents.
Now the display (the lines of code are broken up to explain each item separately as needed):
cout << "a. " << m - n << " "
The first and last are quite self explanatory, the last space being necessary to separate the different numbers we will display per line. In the center, we have m - n, which we know is integer math (13 - 3). The display on the running program should show 10.
<< m / n << " "
Integer division. The remainder will be lost (or truncated). 13 / 3 = 4. In C++, dividing integers will always lose the remainder, unless measures are taken to cast it into a float or higher (double, long double). But we knew all of this from before, right?
<< m % n << endl;
Okay, I promised you would learn what this was when we discussed the orders of operation. The percent sign, known in C++ as modulus, will take two numbers, divide them, and then use the remainder. 13 / 3 = 4 remainder 1, so the modulus (%) is 1. This is useful for a number of purposes. One of which is finding out if a number is odd or even. Take any number, say 23. You want to know if it is odd or even. Well, you know, just because you know. But how does the computer know? You have to tell it to take that number and the number 2, and perform a modulus (%) on it. 23 % 2 is 11 r 1. If there is a remainder, which there was, then the number is odd. The modulus operator has many other uses, but we will not go into them here.
cout << "b. " << p << " " << q << endl;
This is pretty self-explanatory. It just displays the values contained in the variables, p and q (which should have been 19 and 82). The reason it is included in the program is to show how you can enter an octal or a hexidecimal number into an integer, and it will display the decimal equivalent.
cout << "c. " << r << " " << r + 1 << " "
Characters. Here's where things will get a bit tricky. The display of << r will be the actual character stored, in this case 'B'. The display of << r + 1 will be an integer value. This is because int is a larger type than char, so the char will be automagically "upgraded", or cast, to type int. The ASCII value will be taken, 66, and 1 will be added to that, which equals 67.
<< char (r + 1) << " " << 'r' + 1 << endl;
Here, you have the same thing, with r + 1 = 67, but then you cast it back down to a char, so the display will be 'C'. Next, 'r' + 1, will take the ASCII value of the character 'r', NOT the variable r, and add 1 to it, casting it to an int: 114 + 1 will display 115.
cout << "d. " << char(s - 32) << endl;
This one takes the ASCII value of s (98) and subtracts 32, casts it to a char and displays it ('B'). Note that the original character of variable s was a lower case 'b', and after subtracting 32 from it, it becomes an upper case 'B'. This is because there is a difference of 32 in the ASCII upper and lower case characters. Adding 32 to an upper case character will convert it to a lower case character. This is almost exactly what the toupper and tolower functions of the ctype library do, with the exception of not changing a character that is already there (ie, using toupper, 'C' would return 'C', whereas just subtracting 32 would give a quote ' " ' character due to the number 66 - 32 = 34). To use toupper or tolower you must include the library ctype (#include <ctype>).
return 0; }
Properly exit the program.
Now to the last page of this section, where you get a chance to prove to yourself what you have learned with datatypes.