Csc2311 - Lecture - 3
Csc2311 - Lecture - 3
CSC2311
Computer Programming I
Page 1 of 7
Lecture 3
o Integral types
Exercise
References
Page 2 of 7
Lecture 3
From the point of view of computer programmers, there are two main types of numbers. The classification
of numbers in to these two categories is based on whether they can have only integer values or also
fractional part in addition to the integer part. For example, the number 123 has only integer portion.
However, the number 12.35 has an integer portion (i.e 12) and also fractional portion (i.e 35) after decimal
point.
Numbers that cannot have any fractional part are called an integer. Number that can have fractional part
are called floating – point numbers.
Integers are used for counting discrete values. For example, we say that, there are 5 balls in a bag or 10
biscuits in a pack. But we never say that, there are 2.8 balls or 5.9 biscuits.
On the other hand, floating point numbers are used approximately measure something. For example, we
say that, the height of a person is 5.7 feet or a weight of some metal is 5.20 kilograms.
This distinct between integers and floating point numbers is significant from the perspective of
representing such numbers inside computers. When we know that integer cannot have fractional part.
Why set aside extra (unused) space for the fractional part. Instead, we can utilize only the number of the
bits that suffice the storage of integers. Also, the mathematics of floating point numbers is more
complicated than that of integers. So, it is advisable to treat these numbers as distinct and have them
stored and processed as such.
Two kinds of numeric types are common to all programming languages: integral types and floating – point
types.
The term “floating – point” refers to the scientific notation that is used for rational numbers. For example,
123.4567 can also be represented as 1.234567 x 10^2, and 0.000123 as 1.23 x 10^-4. The alternative are
obtained by letting the decimal point to “float” among the digits and using the exponent on 10 to count
how many places it has floated to the left or right.
Standard C++ has 14 different fundamental types: 11 integral types and 3 floating point types.
The integral type includes:
Boolean type: bool
Enumeration type: enum
The three character types
1. Char
2. Unsigned
3. Wchar_t
The six explicit integer types
1. Short
2. Int
3. Long
4. Unsigned short
5. Unsigned int
Page 3 of 7
Lecture 3
6. Unsigned long
The floating – point types includes
Float
Double
Long double
The most frequently used fundamental types are bool, char, int, and double.
Boolean Type
The Boolean type is an integral type whose variables can have only one of the two values: false or true.
The values are stored as the integer 0 and 1 respectively. The Boolean type in standard C++ is named bool.
Example:
#include<iostream>
int main(){
Bool flag = false;
cout<< “flag =” <<flag<<endl;
Flag = true;
cout<< “flag=” <<flag<<endl;
}
Enumeration Types
In addition to the predefined types such int and char, C++ allows you to define your own special data
types. This can be done in several ways. The most powerful of which use classes, we consider here much
simpler kind of user – define type an enumeration type is an integral type that is defined by the user with
the syntax:
Enum typename {enumeration – list}
Here enum, is a C++ keyword, typename stands for an identifier that names the type of being defined,
and enumerator – list stands for a list of names for integer constants. For example, the following defines
the enumeration type semester, specifying the three possible values that a variable of that type can have.
Enum semester{winter, monsoon, summer};
We then declare variables of these types:
Semester s1, s2;
And we can use those variables and those type values as we would with predefined types:
S1 = monsoon;
S2 = winter;
If (s1 == s2) cout<<”same semester.”<<endl;
The actual values defined in the enumerator – lists are called enumerators. In fact, they are ordinary
integer constants. For example, the enumerator winter, monsoon, and summer that are defined for the
same semester type above would have been defined like this:
Const int = 0;
Const int = 1;
Const int = 2;
Page 4 of 7
Lecture 3
The values 0,1,… are assigned automatically when the type is defined. These default values can be
overridden in the enumerator – list:
Enum coin{paisa, five_paisa, ten_paisa};
If integer values are assigned to only some of the enumerators, then the ones that follow are given
consecutive values. For example,
enum month {jan = 1, feb, mar, april, may, jun, jul, aug, sept, oct, nov, dec};
Will assigned the values 1 through 12 to the twelve months.
Since enumerators are simply integer constants, it is legal to have several different enumerators with
the same values. For example:
enum answer{No = 0, False = 0, Yes = 1, True = 1, O.k =1};
This would allow the code,
Answer answer;
cin>>answer;
…
If (answer ==yes) cout<<”you said it was Ok”<<endl;
To work as expected. If the values of the variable answer is Yes or Ok (both of which equal 1), then the
condition will be true and the output will occur. Note that this selection statement could also be written.
If (answer) cout<<”you have said it was Ok.”<<endl;
Character Type
The character type is an integral type whose variables represent characters like the letter ‘A’ or the digit
‘8’. Character literals are delimited by the apostrophe (‘).Example:
#include<iostream>
int main() {
char c = ‘A’;
cout<<”c =”<<c<< “, int(c) =”<<int(c)<<endl;
c = ‘t’;
cout<< “c =”<<c<< “, int(c) =”<<int(c)<<endl;
c = ‘!’;
cout<< “c=”<< “, int(c) =”<<int(c)<<endl;
}
Since character values are used for input and output, they appear in their character form instead of their
integral form: the character ‘A’ is printed as the letter “A”, not as the 65 which is its internal
representation. The type cast operator int(c) is used here to reveal the corresponding integral value.
These are the character’s ASCII codes (American Standard Code for Information Interchange).
Integer Types
There are six integer types in standard C++; these types actually have several names. For example, short
is also named short int, and int is also unsigned int.
#include<iostream>
#include<conio.h>
Page 5 of 7
Lecture 3
int main(){
int num1;
int num2;
int num3;
int sum;
num1=4;
num2=7;
num3= 23;
sum=num1+num2+num3;
cout<<“first number is: “<<num1<<“\n”;
cout<<“second number is: “<<num2<<“\n”;
cout<<“third number is: “<<num3<<“\n”;
cout<<" the total sum is: "<<sum;
getch();
}
EXERCISE
Q1. Write a program to compute and output the perimeter of and area of a circle having a radius of 3m
Q2. Write a program that will allow inputting two integer numbers, subtract the first integer from the
second integer and display the result on the screen.
Assume your variables are x and y;
Q3. Write the C++ code to store three variables: your weight, height in feet, and shoe size. Declare the
variables, assign their values in the body of your program and then output the values.
Q4. Write a program that store the detail of your bank account. Declare Name of the bank, Account Name,
Account Type, Account Number and Current Balance as your variables, assign values and display them on
the screen.
REFERENCES
Hubbard, J.R. (2000) Programming with C++, Tata McGraw-Hill, New Delhi, India (Second Edition)
Page 7 of 7