STQS1313 Computer Programming PDF
Document Details
Uploaded by GodlikeClavichord9430
D. S. Malik
Tags
Summary
These are notes on data types in C++. The document covers simple data types, integral data types and floating-point data types, in addition to providing examples of their usage.
Full Transcript
STQS1313 COMPUTER PROGRAMMING Adapted from C++ Programming: From Problem Analysis to Program Design, 8th edition by D. S. Malik TOPIC 2: Basic elements of C++ (Part B) Data Types 1) The objective of a C++ program is to manipulate data. Different programs manipulate different data. 2) Data type: A...
STQS1313 COMPUTER PROGRAMMING Adapted from C++ Programming: From Problem Analysis to Program Design, 8th edition by D. S. Malik TOPIC 2: Basic elements of C++ (Part B) Data Types 1) The objective of a C++ program is to manipulate data. Different programs manipulate different data. 2) Data type: A set of values together with a set of allowed operations. 3) C++ data types fall into the following three categories: simple data type structured data type pointers. 4) In this topic, you will learn about simple data type. Simple data type 1) Is the fundamental data type in C++. 2) Three categories of simple data are: Integral: deals with integers, or numbers without a decimal part. Floating point: deals with decimal numbers. Enumeration: user-defined data type. 3) The reason that there are so many categories for the same data types: In the early days of programming, computers and main memory were very expensive. As a result, programmers had to optimize the use of memory, hence only used what he/she needed. Integral data types 1) Can be classified into: int, bool, char, short, long, long long and a few others. 2) int Used for integers Two rules: do not need a + sign for positive integers, and no commas are used within an integer. The most common storage allocation given by computers for this data type is 4 bytes. 3) bool bolian has only two values: true and false. You will learn this in depth in Chapter 4. 4) char mainly used to represent single characters – that is, letters, digits and special symbols. each character has to be enclosed by a single quotation marks. Example: ‘A’, ‘a’, ‘0’, ‘$’, ‘ ’ Thus, the value ‘abc’ is not of the type char. Each of the 128 values of the American Standard Code for Information Interchange (ASCII) character set represents a different character. For example, ‘A’ is represented by the value 65, and the value 43 represents ‘+’. Thus, each character has a predefined ordering represented by the numeric value associated with the character (called as a collating sequence) → used when comparing characters. For example, ‘A’ is smaller than ‘a’, since the value representing ‘A’ is 65 while the value representing ‘a’ is 97 (note the case-sensitive property). Floating-point data types 1) used to deal with decimal numbers. 2) can be classified into: float, double and long double → differ in the set of values they can represent. 3) float The memory allocated for a value is usually four bytes. 4) double 1 the memory allocated for a value is usually eight bytes. we will mostly use this double type rather than float. 5) long double the memory allocated for a value is usually 16 bytes. 6) What is meant here is that there is a difference in the maximum number of significant digits (precision) can be represented by each type. 7) Sometimes, float values are called single precision, and values of type double are called double precision. 8) Example: Let’s try calculating the value of PI, obtained by finding cos-1(-1). Here we need a library called cmath: #include #include using namespace std; int main() { int PI1 = acos(-1); float PI2 = acos(-1.0F); double PI3 = acos(-1.0); long double PI4= acos(-1.0L); cout.precision(100); cout