Tuesday 11 September 2012

C++ ( Tutorials XI )

The following topics are covered in this section: 
Introduction , Integer, Floating Type, Double, Character, Boolean determining the range, More on Binary Numbers.
1.)Integer
An integer can contain only digits (numbers) from 0 to 9. Examples of integers are:
0, 10, 345, 6789, 33, -23, -600

It includes positive and negative numbers but the numbers have to be whole numbers. It does accept the decimal point. Hence the following numbers are not integer data types:
 3.5
 4.8
 0.23
These numbers come under the second category (floating point type) and not under integers. If the program has to accept such values from the user do not declare the variable as an integer. If a variable is declared as an integer and the user enters a value of 2.3, the program will assign 2 as the value for that integer variable. Similarly, if the user enters 3.2, the program will assign 3 to the integer variable.

Once a variable is declared as an integer, it will only store whole numbers (if the user types a value with the decimal point, the program will ignore everything that is typed after the decimal point).

How to declare a variable as belonging to the type integer? The syntax is:

int variable-name;

2.)Floating Types

Floating type data include integers as well as numbers with a decimal point. It can also have an exponent. Exponent means 10 to the power of some integer value (whole number). 20000 = 2 x 10^4 = 2e4 = 2E4. 
If you specify decimal numbers, floating point data type will store up to a precision of 6 digits after the decimal point. Suppose 0.1234567 is assigned to a floating-point variable, the actual value stored would be 0.123457 (it will round up to the sixth digit after the decimal place). Valid floating-point numbers are:
a.) 0.1276
b.) 1.23
c.) 1.0
d.) 10.2
e.) 35
f.) 2e5 (this will be typed in your code as 2e5)

Do not use an exponent with a decimal point. For example: 2e2.2 is an invalid floating point because the exponent has to be an integer.Floating point numbers use 4 bytes of memory and has a much greater range than integers because of the use of exponents. They can have values up to 10^38 (in positive and negative direction). The same qualifiers used for an integer can be applied to floating point numbers as well. To declare a floating variable, the syntax is:
float variable-name;

3.)Double

This is similar to the floating-point data type but it has an even greater range extending up to 10308. The syntax to declare a variable of type double is:

double variable-name;

Beware: Visual C++ (VC++) usually uses its default as ‘double’ instead of ‘float’. Suppose we type:
float x=31.54;
you will get a warning message saying that a ‘double’ (i.e. 31.54) is being converted into a floating point. It is just to warn you that you are using a ‘float’ and not a ‘double’. (Even if there are warnings, there won’t be any problem in running your program).

4.)Character

A character uses just one byte of memory. It can store any character present on the keyboard (includes alphabets and numbers). It can take numbers from 0 to 9 only. The following are valid characters:

a.)A
b.) B
c.) 3
d.) a
e.) :
f.)
g.) /

If the number 13 is entered as the value for a character, the program will only store 1 (i.e it will store the first character that it encounters and will discard the rest). A character is stored in one byte (as a binary number). Thus whatever the user enters is converted into a binary number using some character set to perform this conversion. Mostly all computers make use of the ASCII (American Standard Code for Information Interchange). For example, according to the ASCII coding, the letter ‘A’ has a decimal value of 65 and the letter ‘a’ has a value of 97.

There is another form of coding called the EBCDIC (Extended Binary Coded Decimal Information Code) which was developed by IBM and used in IBM computers. However, ASCII remains the most widely used code in all computers. The table at the end of the book gives a listing of the ASCII values and their equivalent characters. The syntax to declare a variable which can hold a character is: 

char variable-name;

5.)Determining the range:

You might be wondering why the ranges are from -128 to 127 or from -32768 to 32767? Why not from –128 to 128? We’ll take the case of signed characters to understand the range. A character occupies one byte (8 bits). In a signed character the MSB (i.e. the 7th bit is used to denote the sign; if the MSB is 1 then the number is negative else it is positive). The binary number: 0111 1111 represents +127 in decimal value (we’ve seen about conversion from binary to decimal numbers in the first chapter). The number 0000 0000 represents 0 in decimal. But what about 1000 0000? Since the MSB denotes a negative number, does this stand for –0? Since there is no point in having a –0 and a +0, computers will take 1000 0000 as –128. 0000 0000 is taken to be zero. Thus in the negative side we have a least value of –128 possible while in the positive side we can have a maximum of only +127.

Another point to note is that negative number in computers are usually stored in 2’s complement format. For example: 1000 0001actually stands for –1 but if this number is stored in 2’s complement format then it stands for –127. Similarly 1000 0010 would appear to be –2 but is actually –126. To understand 2’s complement you should know about 1’s complement. Let us say we have a signed binary number: 1000 0001. The 1’s complement of this number is 1111 1110 (i.e. to find the 1’s complement of a binary number just change the 1s to 0s and 0s to 1s but do not change the sign bit). To find the 2’s complement just add 1 to the 1’s complement. Hence the 2’s complement of 1000 0001 is 1111 1111 (or –127). In the same way the 2’s complement of 1111 1111 (-127) is 1000 0001 (or – 1). Thus the computer instead of storing 1111 1111 (for –127) will store the 2’s complement of the number (i.e. it will store 1000 0001).

6.)More on Binary Numbers:

Everything in the computer is stored in binary format. Even if we ask the computer to store an octal number or a hexadecimal number in a variable, it will still be stored in binary format (we’ll see this later) because computers only understand 0s and 1s. How does a computer perform calculations? Yes, it has to do it in binary format.

0 comments:

Post a Comment