First Lecture

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Western Mindanao State University

Zamboanga City, Philippines


College of Computing Studies
Department of Computer Science

COMPUTER PROGRAMMING I
CC 101

Odon A. Maravillas, Jr., MSCS


COMPUTER
-is a machine or device that performs processes, calculations and
operations based on instructions provided by a software or hardware
program. It has the ability to accept data (input), process it, and then
produce outputs.

-is an electronic device that manipulates information, or data. It has the


ability to store, retrieve, process data and display accurate results.
What is Programming?
• Programming is also called Coding
• It is an act of instructing computer to do different tasks
• A computer program is a sequence of instructions that the computer executes.
• a computer program is a self-contained set of instructions used to operate a computer to
produce a specific result. Another term for a program or set of programs is software.
Programming Language
• These instructions are usually written in any programming language which
is understandable by programmers.
• A programming language is a computer language programmers use to
develop software programs, scripts, or other sets of instructions for
computers to execute.
• A translator is used to convert programming language instructions into
binary language.
Binary Code
• Computer itself understand only binary code
• Binary codes can have only two states, 0(off) and 1(on)
• Computers only understand electrical pulses
• It is very difficult for a human to communicate with computer in
binary code.
Computer System
Organization

A computer system is made up of various components. The


components can be hardware or software. Because these systems are
so massively complex, the components are organized in layers.
The Computer System Layer
The computer systems we build today feature programmable processing units which interact
with a number of devices, each controlled by an I/O controller, and using memory.

Each CPU can have one or more cores. There may be additional kinds of processors, including
GPUs(Graphic processing units) and TPUs(Tensor processing units). Memory itself
is layered (cache memory, main memory, secondary memory). Machines themselves are
networked, giving the appearance of one large machine made up of smaller ones.
Devices are roughly classified into input devices, output
devices and storage devices. Examples include:
A program is a solution developed to solve a particular problem, written in a
form that can be executed on a computer.
Therefore, writing a program is almost the last step in a process that first
determines the problem to be solved and the method to be used in the solution.
Each field of study has its own name for the systematic method of designing
solutions to solve problems.

In science and engineering, the approach is referred to as the scientific method,


and in quantitative analysis, the approach is called the systems approach.

Professional software developers use the software development procedure for


understanding the problem to be solved and for creating an effective,
appropriate software solution.
This procedure, illustrated in Figure 1.1, consists of three overlapping phases:
1. Development and design
2. Documentation
3. Maintenance

As a discipline, software engineering is concerned with creating readable, efficient,


reliable, and maintainable programs and systems, and it uses the software development
procedure to achieve this goal.
Phase I: Development and Design
• Phase I begins with a statement of a problem or a specific request for a program,
which is referred to as a program requirement. After a problem has been stated
or a specific request for a program solution has been made, the development and
design phase begins. This phase consists of four well-defined steps, as illustrated
in Figure 1.2.
Step 1 Analyze the Problem
The analysis of a problem can consist of up to two parts. The first
part is a basic analysis that must be performed on all problems; it
consists of extracting the complete input and output information
supplied by the problems. For this analysis, you must

1. Determine and understand the output items the program must


produce.
2. Determine the input items.
Step 2 Develop a Solution
In this step, you select the exact set of steps, called an “algorithm,” to be used to
solve the problem.
ALGORITHM:

The word “algorithm” relates to the name of the Arab mathematician


Al-khowarizmi, which means a procedure or a technique. Software
Engineer commonly uses an algorithm for planning and solving the
problems. An algorithm is a sequence of steps to solve a particular
problem or it is an ordered set of unambiguous steps that produces a
result and terminates in a finite time.
Step 3 Code the Solution (Write the Program)
This step consists of actually writing a C++ program that corresponds
to the solution developed in Step 2.

Step 4 Test and Correct the Program


The purpose of testing is to verify that a program works correctly and actually
fulfills its requirements. In theory, testing would reveal all existing program
errors. (In computer terminology, a program error is called a bug )
Phase II: Documentation
Documenting your work is one of the most important steps in
problem solving.
Problem solving is the process of transforming
the description of a problem into solution where The Implementation phase is where you
you define the problem and identify the sets of convert your algorithm to a series of
instructions on how you are going to solve the source codes by writing a computer
problem. In this phase, you need to produce an program using a programming language.
ordered sequence of steps that describe a The output of your program must be the
solution to a problem. solution of the intended problem.

Concept of Program Design


Algorithm
Algorithm has the following characteristics
• Input: An algorithm may or may not require input
• Output: Each algorithm is expected to produce at least one result
• Definiteness: Each instruction must be clear and unambiguous.
• Finiteness: If the instructions of an algorithm are executed, the
algorithm should terminate after finite number of steps.
The algorithm include following three types of control structures.
1. Sequence: In the sequence structure, statements are placed one after the
other and the execution takes place starting from up to down.
2. Branching (Selection): In branch control, there is a condition and
according to a condition, a decision of either TRUE or FALSE is
achieved. In the case of TRUE, one of the two branches is explored;
but in the case of FALSE condition, the other alternative is taken.
Generally, the ‘IF-THEN’ is used to represent branch control.
3. Loop (Repetition): The Loop or Repetition allows a statement(s) to be
executed repeatedly based on certain loop condition e.g. WHILE, FOR loops.
Advantages of algorithm
• It is a step-wise representation of a solution to a given problem, which makes it
easy to understand.
• An algorithm uses a definite procedure.
• It is not dependent on any programming language, so it is easy to understand for
anyone even without programming knowledge.
• Every step in an algorithm has its own logical sequence so it is easy to debug.
Algorithm can be represented in a form of a pseudocode or a flowchart.
Pseudocode is an informal language that helps you develop algorithms. Pseudocodes do not
follow any particular computer language and are written in simple English.
How to write Pseudocode
Step 1 Define your algorithms input (s)
Step 2 Define the variables:
Step 3 Outline the algorithm's operations:
Step 4 Output the results of your algorithm's operations:
Example Determine and display the area and perimeter of a rectangle
Steps
1 Start
2 Input the length and width say Length and Width
3 Calculate the area and perimeter
Area=Length x Width
Perimeter=2*Length + 2* Width
4 Display Area and Perimeter
5 Stop

Note: We can use keyword INPUT or READ or GET to accept input(s) /value(s) and keywords PRINT or
DISPLAY to output the result(s).
The language used to write pseudocode is simple and similar to day-to-day life language. The variable
names are used to store the values. The value store in variable can change in the solution steps.

In addition some special symbols are used as below


Assignment Symbol ( → or =) is used to assign value to the variable.
e.g. to assign value 5 to the variable HEIGHT, statement is
HEIGHT → 5
or
HEIGHT = 5
The symbol ‘=’ is used in most of the programming language as an assignment symbol, the same has
been used in all the algorithms and flowcharts in the manual.

The statement C = A + B means that add the value stored in variable A and variable B then assign/store
the value in variable C.

The statement R = R + 1 means that add 1 to the value stored in variable R and then assign/store the
new value in variable R, in other words increase the value of variable R by 1
Example Write a pseudocode to convert temperature from Fahrenheit(F) to Celsius (C)

C = 5.0/9.0 (F - 32 )
1. Start
2. Input temperature in Fahrenheit, F
3. Convert temperature in Fahrenheit to temperature in Celsius
C = 5.0/9.0 (F - 32 )
4. Display temperature in Celsius,C
5. Stop

Example Write a pseudocode to find the sum, difference, and product of the two numbers.

1. Start
2. Input the two numbers say Num1,Num2
3. Compute the sum,S , difference,D , and the product,P of the two numbers
S = Num1 + Num2
D = Num1 - Num2
P = Num1 * Num2
4. Print sum S, difference D, and product D
5. Stop
Selection Process
Problems
1. Write a pseudocode to display a word “greater” if the number entered is greater than 10.
2. Write a pseudocode to input two numbers and compare its value and display a message in describing their
comparison.
3. Write a pseudocode to input angle in degree,Deg. Print the message “ The angle is a right angle” if an angle
inputted is equal to 90 degrees, else print the message “The angle is not a right angle.”
4. Write a pseudocode to input a number and display a message “greater than 20” if the number is greater than 20
and also display a word “ODD” or “EVEN” if it is an odd or even number, otherwise “not greater than 20”.
5. Write a pseudocode that assign grade based on the percentage obtained by a student as shown in the table
below
percentage,P grade
P>=90 “A”
75<= P < 90 “B”
65<= P<75 “C”
P below 65 “F”
6. Write a pseudocode to input three numbers and display the lowest number.
1. Write a pseudocode to display a word “greater” if the number entered is greater than 10.
Pseudocode
1. Start
2. Input a number, Num
3. If the number, Num is greater than 10 then If Num > 10
4. Display “ Greater”
5. Endif
6. Stop

2. Write a pseudocode to input two numbers and compare its value and display a message in describing their
comparison.
Pseudocode
1. Start
2. Input two numbers, first number, N1, and second number, N2
3. If N1=N2 then
4. display “Both numbers are equal”
5. Endif
6. If N1>N2 then
7. Display “First number is greater than the second number”
8. Endif
9. If N1<N2 then
10. Display “First number is less than the second number”
11. Endif
12. Stop
3. Write a pseudocode to input angle in degree,Deg. Print the message “ The angle is a right angle” if an angle
inputted is equal to 90 degrees, else print the message “The angle is not a right angle.”

Pseudocode
1. Start
2. Input the value of angle in degree, Deg
3. IF Deg = 90 then
4. Display “The angle is a right angle”
5. Else
6. Display “ The angle is not a right angle”
7. Endif
8. Stop
4. Write a pseudocode to input a number and display a message “greater than 20” if the number is greater than 20
and also display a word “ODD” or “EVEN” if it is an odd or even number, otherwise “not greater than 20”.

Pseudocode
1. Start
2. Input a number, Num
3. IF Num > 20 then
4. Display “ greater than 20”
5. IF NUM%2 =0 the
6. Display “even”
7. else
8. Display “odd”
9. endif
10. Else
11. Display “ not greater than 20”
12. Endif
13. Stop
5. Write a pseudocode that assign grade based on the percentage obtained by a student as shown in the table
below
percentage,P grade
P>=90 “A”
75<= P < 90 “B”
65<= P<75 “C”
P below 65 “F”
Pseudocode
1. Start
2. Input percentage,P
3. If P>=90 then
4. Display “A”
5. Else
6. If P>=75 AND P<90 then
7. Display “B”
8. Else
9. If P>=65 AND P<75 then
10. Display “C”
11. else
12. Display “F”
13. Endif
14. Endif
15. Endif
16. Stop
6. Write a pseudocode to input three numbers and display the lowest number.
Pseudocode
1. Start
2. Input three numbers, N1,N2,N3
3. If N1<N2 then If N1<N2 And N1<N3 then
4. If N1<N3 then Display the lowest, N1
5. Display the lowest, N1 endif
6. Else If N2<N1 And N2<N3 then
7. Display the lowest is, N3 Display the lowest, N2
8. endif endif
9. Else If N3 <N1 And N3<N2 then
10. If N2 < N3 then Display the lowest, N3
11. Display the lowest is , N2 endif
12. else stop
13. Display the lowest is, N3
14. endif
15. Endif
16. stop

You might also like