Professional Documents
Culture Documents
To Computer Programming (CSC425) : Chapter 2: Component of Programming Language
To Computer Programming (CSC425) : Chapter 2: Component of Programming Language
TO COMPUTER
PROGRAMMING
(CSC425)
Chapter 2: Component Of
Programming Language
Basic Operations
Program Elements
Contents Storage
Arithmetic Operations
11/7/20 2
Basic Operations
6 types of basic operations :
1. Receive input
2. Produce output
3. Assign value into storage
4. Perform arithmetic and logic operation
5. Make selection
6. Repeating a set of actions
11/7/20 3
Program’s elements
Generally a computer program has the following elements:
1. Input section
2. Storage allocation
• Constants
• Variables
• Data Structures
• Files
3. Processing using arithmetic or logic operation and control with either
sequential, selection or and repetition structure.
4. Output Section
11/7/20 4
Program’s elements
constant
Storage variable
11/7/20 5
Storage
• One of the essential resource of a program
• A location in computer memory that is allocated to the program
• Used to hold value of specific data type
• Identifier is the name given to specific storage
• Categories:
• Constants
• Variables
• Data Structure
• Files
• Data Types
• Integer number
• Floating point number
• Character
11/7/20 6
Storage (cont.)
• Example:
int number;
11/7/20 7
Storage (cont.)
• Identifier naming rules:
• Must begin with a letter
• Can be followed by a letter, number or underscore (‘_’)
• C++ is case sensitive
• Examples of valid identifiers
number1
averageScore
latest_CGPA
• Examples of invalid identifiers
20score
Student’sAge
average Salary
11/7/20 8
Storage (cont.)
• Constant
• To hold fixed value that cannot be altered during program execution.
• Examples:
•PI value 3.1416
•Number of days per week 7
• Constant declaration:
const keyword is used to declare a constant and it must be
initialized.
<data type> const <identifier> = value;
• Example:
float const PI = 3.1416;
11/7/20 9
Storage (cont.)
• Variables
• To hold value that might be altered during program execution.
• Hold value on temporary basis
• Examples:
• score
• Temperature
• Speed
• Length
• Variable declaration:
<data type> <identifier> [ = initial value];
• Example:
int number;
float score1, score2;
int score, float number;
int count = 0;
11/7/20 10
Storage (cont.)
• Data Structure
• An array or a pointer
• An array is a contagious memory locations that hold more than one
values of the same type.
• Hold value on temporary basis
• Examples:
• Name
char name[20];
• A set of students’ score
int score[20];
11/7/20 11
Storage (cont.)
• Basic Data Types C++ Data Types
• Integer (90, 0, -78) int or long
• Floating point (4.5,1.0,-0.67) float or double
• Character (‘A’,’a’,’*’,’1’) char
• Examples
int number;
int score;
long population;
float temperature;
double accountBalance;
char gender;
11/7/20 12
Example :
#include <iostream.h>
//program to calculate area of a circle
PI 3.1416
main()
{ radius ?
//variable and constant allocation
float const PI = 3.1416; area ?
float radius, area;
//input section
cout << “Enter a radius : ”;
cin >> radius;
//output section
cout << “Area of the circle is ” << area << endl;
return 0;
}//end main() 13
Examples
Enter a radius : __
11/7/20 14
Examples
Enter a radius : 8
11/7/20 15
Examples
#include <iostream.h>
//program to calculate area of a circle PI 3.1416
main() radius 8
{
//variable and constant allocation
area 201.0624
float const PI = 3.1416;
float radius, area;
//input section
cout << “Enter a radius : ”;
cin >> radius;
//output section
cout << “Area of the circle is ” << area << endl;
return 0;
}//end main() 16
Examples
Enter a radius : 8
Area of the circle is 201.0624
11/7/20 17
Input and Output Statement (cont.)
• Input Statement
• To received input from keyboard or read input from a file.
• Using cin keyword and input stream operator (>>).
• Syntax:
cin >> <variable>;
• Example
11/7/20 18
Input and Output Statement (cont.)
• Example of a good input statement:
Prompt user to enter a number
Read a number
11/7/20 19
Input and Output Statement (cont.)
• Output Statement
• To display output on screen or to write output into file.
• Using cout keyword and output stream operator (<<).
• Syntax:
cout << <string|constant|variable|expression>;
11/7/20 20
Input and Output Statement (cont.)
• Output Statement
• Examples:
• Statements Output
cout << “Hello World”; Hello World
cout << 80; 80
cout << area; content of area
cout << 8 + 4; 12
11/7/20 21
Input and Output Statement (cont.)
• Output Statement
• What is the output of the following statements?
• Statements Output
cout << “Total is ” << 8 + 2; ?
cout << “8 + 2 = ” << 8 + 2; ?
11/7/20 22
Input and Output Statement (cont.)
• End of line (newline)
• endl keyword will force the cursor to begin at new line.
• Examples:
• Code fragment Output
cout << 14; 1416
cout << 16;
11/7/20 23
Arithmetic Operations
• FIVE basic operations:
11/7/20 24
Arithmetic Operations (cont.)
• Examples
int number1, number2;
int sum, different, product, quotient, reminder;
number1 = 8;
number2 = 4;
11/7/20 25
Arithmetic Operations (cont.)
• Integer Division
IF two integers are divided, the result will be an integer number. Any
fraction will be truncated.
int / int int
Example:
int a = 5, b = 2;
float c;
c = a / b; //the new value of c is 2
11/7/20 26
Arithmetic Operations (cont.)
• Data type conversion
To avoid the effect of integer division the data type must be converted to float.
Example:
int a = 5, b = 2,
float c;
c = float(a) / b; //the new value of c is 2.5
c = a / float(b); //the new value of c is 2.5
11/7/20 27
Arithmetic Operations (cont.)
• Invalid Statement!
11/7/20 28
Arithmetic Operations (cont.)
• Type Conversion (Casting)
- store a value into a variable of a different type
- example : if you want to store a double value into an int variable
- expressed using static_cast keyword as follows :
static_cast<data_type>(expression)
- example :
int n = static_cast<int>(x + 0.5)
11/7/20 29
Arithmetic Operations (cont.)
11/7/20 30
Arithmetic Operations (cont.)
Exercise :
Write a program that does the following :
1. Prompts the user to input two decimal numbers
2. Find the sum, subtract and average of the two integers
11/7/20 31
Review
• Declare the suitable storage to hold the following data:
1. Number of days worked in a week
2. Student’s CGPA
3. Name of a person
4. Speed of a light
11/7/20 32
Review
• Translate the following flow chart into source code:
Begin
Read totalScore
Read count
Display average
End
11/7/20 33
Review
• Given the following declaration:
11/7/20 34