0% found this document useful (0 votes)
5 views

Chapter3 - Introduction To Computer Programming Language

Uploaded by

2023837758
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter3 - Introduction To Computer Programming Language

Uploaded by

2023837758
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

IMD238

Programming for Information


Professional
CHAPTER 3:
Introduction to Computer Programming
Language

Madam Khadijah Abdul Rahman


Faculty of Information Management
Unviersiti Teknologi MARA
Cawangan Kelantan
ELEMENTARY PROGRAMMING
[Learning Outcomes]
At the end this class, students should able:
• To solve practical problems programmatically.
• Describe primitive data types and related subjects, such as variables,
constants, data types, operators, expressions, and input and output.
ELEMENTARY PROGRAMMING
[INTRODUCING PROGRAMMING WITH AN EXAMPLE]

• This program computes the area of the circle.

#include <iostream>
using namespace std;

int main()
{
double radius;
double area;

// Step 1: Read in radius


radius = 20;

// Step 2: Compute area


area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is " << area << endl;

return 0;
ELEMENTARY PROGRAMMING
[TRACE A PROGRAM EXECUTION]
#include <iostream> allocate memory
using namespace std; for radius

int main() { radius no value


double radius;
double area;

// Step 1: Read in radius


radius = 20;

// Step 2: Compute area


area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << endl;
}
5
ELEMENTARY PROGRAMMING
[TRACE A PROGRAM EXECUTION]
#include <iostream>
using namespace std;
memory
int main() {
double radius; radius no value
double area; area no value

// Step 1: Read in radius


radius = 20;
allocate memory
// Step 2: Compute area for area
area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << std::endl;
}
6
ELEMENTARY PROGRAMMING
[TRACE A PROGRAM EXECUTION]
#include <iostream>
using namespace std;
assign 20 to radius
int main() {
double radius;
double area; radius 20

area no value
// Step 1: Read in radius
radius = 20;

// Step 2: Compute area


area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << std::endl;
}
7
ELEMENTARY PROGRAMMING
[TRACE A PROGRAM EXECUTION]
#include <iostream>
memory
using namespace std;
radius 20
int main() {
double radius;
area 1256.636
double area;

// Step 1: Read in radius compute area and assign it


radius = 20; to variable area

// Step 2: Compute area


area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << std::endl;
}
8
ELEMENTARY PROGRAMMING
[TRACE A PROGRAM EXECUTION]
#include <iostream>
using namespace std; memory

radius 20
int main() {
double radius; area 1256.636
double area;
print a message to the
// Step 1: Read in radius console
radius = 20;

// Step 2: Compute area


area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << std::endl;
}
9
ELEMENTARY PROGRAMMING
[Reading Input from the Keyboard]
#include <iostream> You can use the cin object to
using namespace std; read input from the keyboard.
int main()
{
// Step 1: Read in radius
double radius;

cout << "Enter a radius: ";


cin >> radius;
// Step 2: Compute area
double area = radius * radius * 3.14159;
// Step 3: Display the area
cout << "The area is " << area << endl;
return 0;

}
10 ELEMENTARY PROGRAMMING
[Reading Multiple Input in One Statement]
#include <iostream>

using namespace std;

int main()

// Prompt the user to enter three numbers

double number1, number2, number3;

cout << "Enter three numbers: ";

cin >> number1 >> number2 >> number3;


// Compute average

double average = (number1 + number2 + number3) /


3;

// Display result

cout << "The average of " << number1 << " " <<
11
ELEMENTARY PROGRAMMING
[Identifiers]
• An identifier is a sequence of characters that consists of letters,
digits, and underscores (_).
• An identifier must start with a letter or an underscore. It cannot
start with a digit.
• An identifier cannot be a reserved word.
• An identifier can be of any length, but your C++ compiler may
impose some restriction. Use identifiers of 31 characters or
fewer to ensure portability.
12 ELEMENTARY PROGRAMMING
[Variables]
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
cout << area;

// Compute the second area


radius = 2.0;
area = radius * radius * 3.14159;
cout << area;
13 ELEMENTARY PROGRAMMING
[Declaring Variables]
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
14 ELEMENTARY PROGRAMMING
[Assignment Statements]
x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;

a = 'A'; // Assign 'A' to a;


ELEMENTARY PROGRAMMING
[Declaring and Initializing in One
15

Step]

• int x = 1;
• double d = 1.4;
16
ELEMENTARY PROGRAMMING
[Named Constants]

const datatype CONSTANTNAME = VALUE;

const double PI = 3.14159;


const int SIZE = 3;
17
ELEMENTARY PROGRAMMING
[Numerical Data Types]
Name Synonymy Range Storage Size

short short int –215 to 215–1 (-32,768 to 32,767) 16-bit signed

unsigned short unsigned short int 0 to 216–1 (65535) 16-bit unsigned

int signed –231 to 231–1 (-2147483648 to 2147483647) 32-bit

unsigned unsigned int 0 to 232–1 (4294967295) 32-bit unsigned


signed
long long int –231 (-2147483648) to 231–1 (2147483647) 32-bit signed
unsigned long unsigned long int 0 to 232–1 (4294967295) 32-bit unsigned
long long –263 (-9223372036854775808) to
263–1 (9223372036854775807) 64-bit signed

float Negative range: 32-bit IEEE 754


-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to -4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
long double Negative range: 80-bit
-1.18E+4932 to -3.37E-4932
Positive range:
3.37E-4932 to 1.18E+4932
Significant decimal digits: 19
18
ELEMENTARY PROGRAMMING
[Numerical Data Types]
Name Synonymy Range Storage Size

short short int –215 to 215–1 (-32,768 to 32,767) 16-bit signed

unsigned short unsigned short int 0 to 216–1 (65535) 16-bit unsigned

int signed –231 to 231–1 (-2147483648 to 2147483647) 32-bit

unsigned unsigned int 0 to 232–1 (4294967295) 32-bit unsigned


signed
long long int –231 (-2147483648) to 231–1 (2147483647) 32-bit signed
unsigned long unsigned long int 0 to 232–1 (4294967295) 32-bit unsigned
long long –263 (-9223372036854775808) to
263–1 (9223372036854775807) 64-bit signed
C++11: long long is
float defined in C++11 Negative range: 32-bit IEEE 754
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to -4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
long double Negative range: 80-bit
-1.18E+4932 to -3.37E-4932
Positive range:
3.37E-4932 to 1.18E+4932
Significant decimal digits: 19
19
ELEMENTARY PROGRAMMING
[sizeof Function]
You can use the sizeof function to find the size of a type. For example, the
following statement displays the size of int, long, and double on your
machine.

cout << sizeof(int) << " " << sizeof(long) << " " << sizeof(double);
20
ELEMENTARY PROGRAMMING
[Synonymous Types]
short int is synonymous to short. unsigned short int is
synonymous to unsigned short. unsigned int is synonymous to
unsigned. long int is synonymous to long. unsigned long int is
synonymous to unsigned long. For example,
short int i = 2;
is same as
short i = 2;
21
ELEMENTARY PROGRAMMING
[Numeric Literals]
A literal is a constant value that appears directly in a program.
For example, 34, 1000000, and 5.0 are literals in the following
statements:

int i = 34;
long k = 1000000;
double d = 5.0;
22
ELEMENTARY PROGRAMMING
[octal and hex literals]
By default, an integer literal is a decimal number. To denote an octal integer
literal, use a leading 0 (zero), and to denote a hexadecimal integer literal, use a
leading 0x or 0X (zero x). For example, the following code displays the decimal
value 65535 for hexadecimal number FFFF and decimal value 8 for octal number
10.

cout << 0xFFFF << " " << 010;


23
ELEMENTARY PROGRAMMING
[double vs. float]
The double type values are more accurate than the float type values. For example,

cout << "1.0 / 3.0 is " << 1.0 / 3.0 << endl;
displays 1.0 / 3.0 is 0.33333333333333331

16 digits

cout << "1.0F / 3.0F is " << 1.0F / 3.0F << endl;

displays 1.0F / 3.0F is 0.3333333432674408


7 digits
24
ELEMENTARY PROGRAMMING
[Why called floating-point?]

The float and double types are used to represent numbers with a decimal point.
Why are they called floating-point numbers? These numbers are stored into
scientific notation. When a number such as 50.534e+1 is converted into
scientific notation such as 5.0534, its decimal point is moved (i.e., floated) to a
new position.

You might also like