Chapter 2 - COMPONENT OF PROGRAMMING I (CSC425)
Chapter 2 - COMPONENT OF PROGRAMMING I (CSC425)
Programming
(CSC425)
by
DR AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM MALAYSIA
Component Of
Programming Language
Chapter 2
2
Contents
⚫ Basic Operations
⚫ Program Elements
⚫ Storage
⚫ Input and Output
⚫ Arithmetic Operations
constant
Storage variable
⚫ Example:
int number;
⚫ 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
• A set of students’ score
char name[20];
int score[20];
⚫ Examples
int number;
int score;
long population;
float temperature;
double accountBalance;
char gender;
//input section
cout << “Enter a radius : ”;
cin >> radius;
//output section
cout << “Area of the circle is ” << area <<
endl;
return 0;
}//end main() 14
Examples
Enter a radius : __
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() 17
Examples
Enter a radius : 8
Area of the circle is 201.0624
• Example
Read a number
⚫ 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>;
⚫ Output Statement
• What is the output of the following statements?
• Statements Output
cout << “Total is ” << 8 + 2; ?
cout << “8 + 2 = ” << 8 + 2; ?
⚫ Examples
int number1, number2;
int sum, different, product, quotient,
reminder;
number1 = 8;
number2 = 4;
⚫ Integer Division
int a = 5, b = 2;
float c;
c = a / b; //the new value of c is 2
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
⚫ Invalid Statement!
static_cast<data_type>(expression)
- example :
int n = static_cast<int>(x + 0.5)
(see Exercise 12)
⚫ Try to run :
- Exercise 3
- Exercise 2
- Exercise 7
- Exercise 8
Exercise 9 :
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
Assignment #1
⚫ Try to run :
- Exercise 4
- Exercise 5
- Exercise 8
Read totalScore
Read count
Display average
End