You are on page 1of 38

FUNDAMENTALS OF COMPUTER

PROBLEM SOLVING
(CSC415)

by
AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM SHAH ALAM

1
Component Of
Programming Language

Chapter 2

2
Contents
 Basic Operations
 Program Elements
 Storage
 Input and Output
 Arithmetic Operations

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 3


PROGRAMMING
Basic Operations
 6 types of 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

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 4


PROGRAMMING
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

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 5


PROGRAMMING
Program’s elements

int number1, number2, total;

cin >> number1 >> number2;


Arithmetic
Logic
total = number1 + number2;

cout << total << endl


cin >> number1 >> number2; cout << total << endl
total = number1 + number2;
Input Processing Output

constant

Storage variable

int number1, number2, total;

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 6


PROGRAMMING
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
 Categories:
• Constants
• Variables
• Data Structure
• Files
 Data Types
• Integer number
• Floating point number
• Character
 Identifier is the name given to specific storage
December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 7
PROGRAMMING
Storage (cont.)

 Example:

int number;

Data type Identifier

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 8


PROGRAMMING
Storage (cont.)
 Identifier naming rules:
• Must begin with a letter
• Can be followed by a letter, number or underscore (‘_’)
 Examples of valid identifiers
number1
averageScore
latest_CGPA
 Examples of invalid identifiers
20score
Student’sAge
average Salary

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 9


PROGRAMMING
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;
December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 10
PROGRAMMING
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;
December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 11
PROGRAMMING
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
• A set of students’ score
char name[20];
int score[20];

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 12


PROGRAMMING
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;
December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 13
PROGRAMMING
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;

area = PI * radius * radius; //processing

//output section
cout << “Area of the circle is ” << area <<
endl;

return 0;
}//end main() 14
Examples
Enter a radius : __

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 15


PROGRAMMING
Examples
Enter a radius : 8

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 16


PROGRAMMING
Examples
#include <iostream.h>
//program to calculate area of a circle PI 3.1416

main()
{
radius 8
//variable and constant allocation
float const PI = 3.1416; area 201.0624
float radius, area;

//input section
cout << “Enter a radius : ”;
cin >> radius;

area = PI * radius * radius; //processing

//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

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 18


PROGRAMMING
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

cin >> number;


cin >> number1 >> number2;

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 19


PROGRAMMING
Input and Output Statement
(cont.)
 Example of a good input statement:

Prompt user to enter a number

cout << “Enter a score : ”;


cin >> score;

Read a number

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 20


PROGRAMMING
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>;

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 21


PROGRAMMING
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

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 22


PROGRAMMING
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; ?

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 23


PROGRAMMING
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;

cout << 14 << endl; 14


cout << 16 << endl; 16

cout << 14 << ‘\n’; 14


cout << 16 << ‘\n’; 16

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 24


PROGRAMMING
Arithmetic Operations
 FIVE basic operations:
• Addition (+)  Sum of two numbers
• Subtraction (-)  Different of two numbers
• Multiplication (*)  Product of two numbers
• Division (/)  Quotient of two numbers
• Modulo (%)  Remainder of division
operation

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 25


PROGRAMMING
Arithmetic Operations (cont.)
 Examples
int number1, number2;
int sum, different, product, quotient, reminder;
number1 = 8;
number2 = 4;

sum = number1 + number2;


different = number1 - number2;
product = number1 * number2;
quotient = number1 / number2;
remainder = number1 % number2;

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 26


PROGRAMMING
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

cout << a / b; //2 will be displayed

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 27


PROGRAMMING
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

//2.5 will be displayed


cout << float(a) / b;
cout << a / float(b);

c = float(a / b); //2.0 will be stored in c


cout << float (a / b); //2.0 will be displayed

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 28


PROGRAMMING
Arithmetic Operations (cont.)

 Invalid Statement!

number1 + number2 = total

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 29


PROGRAMMING
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)
(see Exercise 12)

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 30


PROGRAMMING
Arithmetic Operations (cont.)

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 31


PROGRAMMING
Arithmetic Operations (cont.)

 Try to run :
- Exercise 3
- Exercise 2
- Exercise 7
- Exercise 8

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 32


PROGRAMMING
Arithmetic Operations (cont.)

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

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 33


PROGRAMMING
Arithmetic Operations (cont.)

Assignment #1

 Try to write a program : Convert Length


Write a program that takes as input given lengths
expressed in feet and inches. The program should
then convert and out the length in centimeters.
Assume that the given lengths in feet and inches
are integers.

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 34


PROGRAMMING
Assignment Statement (cont.)

 Try to run :
- Exercise 4
- Exercise 5
- Exercise 8

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 35


PROGRAMMING
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

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 36


PROGRAMMING
Review
 Translate the following flow chart into source code:
Begin

Read totalScore

Read count

average = total / count

Display average

End

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 37


PROGRAMMING
Review
 Given the following declaration:

int a, b, c, v1, v2;


a = 8;
c = b = 3;
c = c – 1;
v1 = a / b;
v2 = a / c;

What is the final content of a, b, c, v1 and v2?

December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 38


PROGRAMMING

You might also like