You are on page 1of 62

Computer Programming-4

ASSIST. PROF. DR MUSTAFA KARA


Content
Problem solving
Algorithm
Pseudocode
Flow chart
Problem solving
Problem solving order
1. Understanding the problem (Understanding, Analyzing),
2. Developing a solution (Designing),
3. Algorithm and program writing (Writing),
4. Testing again and again (Reviewing)
Problem Solving Phase
1. Defining the problem
2. Revealing the outlines of the solution
3. Developing an algorithm based on outlines
4. Ranking the accuracy of the algorithm
Algorithm
Algorithm refers to the path to be followed for solving any problem.
The operations required for the solution are verbally expressed
without allowing any alternative interpretation.
In other words, an algorithm can be defined as the verbal expression
of how data will be entered into the computer from which
peripheral, how the problem will be solved, through which steps the
solution will be processed, and how and where the result will be
written.
Algorithm
I. The word "algorithm" originates from Abu Abdullah
Muhammad ibn Musa al-Khwarizmi, who was born in the
city of Khwarezm, which is present-day Khiva in
Uzbekistan, Turkmenistan.
II. In the 9th century, he wrote the book 'Hisab al-Jabr wal-
Muqabala' regarding his algorithmic works in the field of
algebra, which was translated into Latin and taught in
Europe for many years.
III. European scholars who couldn't pronounce al-
Khwarizmi's name used the term 'algorizm' to refer to
'rules for solving arithmetic problems using Arabic
numerals.' This term later evolved into 'algorithm' and is
used in a broader context.
Algorithm
The effective creation of an algorithm is far more important than the step of
programming.
Translating the prepared algorithm into a programming language is the simpler
part of the task.
If the algorithm you design isn't good, the choice of programming language
becomes irrelevant (C, C++, C#, Java, Visual Basic, etc.).
Multiple algorithms can be prepared for solving a single problem. This indicates
that hundreds of different computer programs can be written for the solution of
any given problem.
Algorithm - Example
1. If you're not in the kitchen, go to the kitchen.

2. Check the tea, if there is no tea?

3. Go to the market, buy tea.

4. Look at the teapot, if it is not full, fill it with water.

5. Light the stove and place the kettle on the fire.

6. Wait for the water to boil.

7. After the water boils, leave the tea and pour the water over it.

8. Again, add some more water to the teapot and wait.

9. When the water boils, let it rest for a while and turn off the fire.

10. Take your tea glass and fill it with tea.

11. Add (or leave out) as much sugar as you want into your tea and stir.

12. Go back to the room you came from.

13. And drink the tea.


Algorithm – Example 2
1. I will calculate the diameter, circumference and area of the circle.
2. I will get the radius information from the screen.
3. First I need to create a radius variable.
4. I need to read a value from the screen and assign it to the radius variable.
5. I have to read the value from the screen with scanf.
6. I should use the ampersand (&) operator when reading a value to a variable.
7. I must first print the diameter value on the screen.
8. I print the variable value to the screen with printf.
9. When using printf, I print the text I want within double quotes.
10. I calculate the diameter in the area separated by a comma at the end of the double quotes. To print this
result I have to use % with type specifier. For Integer I should use %d.
Algorithm-Example 3
Write the algorithm of the prime numbers.
Algorithm Display Types
1. Display in plain text,
2. Representation with pseudo code,
3. Illustration with flowchart (diagram).
Display in Plain Text
The problem to be solved is written step by step as text.
Each line is numbered.
It starts with "START" and ends with "END".
Pseudo code
I. It is a language consisting of simple commands that can be easily understood
by everyone and can be translated into a programming language effortlessly.
II. The primary function of pseudocode is to develop algorithms and discuss
them before moving on to programming.
III. Pseudocodes are written in plain language and programming logic, along
with conditional words like 'if', 'else', and expressions like '>', '=', '<'.
IV. Appropriate names or variables are chosen to represent the elements used
in the program.
V. Arithmetic operations are performed using algebraic notation and decisions.
Write pseudocode for a program that adds two
numbers and outputs the result to the screen.
Select T for the total, X for the first number, and Y for the second number.

1. START
2. Read the value of X
3. Read the value of Y
4. T = X + Y
5. Print the value of T
6. END
Another example
Write pseudocode for a program that calculates the area of a triangle and outputs
the result to the screen.
Select A for the area, B for the base length, and H for the height.
1. START
2. Read the value of B
3. Read the value of H
4. A = (B * H) / 2
5. Print the value of A
6. END
Flow Chart
Flowcharting is the visualization of problem-solving steps.
Each operation is represented by a specific geometric
shape.
Arrows between shapes indicate the flow between
operations.
When looking at a flowchart, the overall structure can be
understood through commonly accepted shapes.
Flow Chart Example
Flow chart components
What are the Basic Symbols of a
Flowchart?
Let's go over each flowchart symbol individually.

Start/End: Use this shape for the initial and final steps of your
process
Process
The Process: This shape represents a general step in your
process. It is the most commonly used shape in almost
all operations.
Output
Document: This shape is used to indicate sending a document to peripheral devices such as a disk or printer.
Decision
Decision: This shape indicates the point where
a decision determines the next step in the
process. There may be multiple outcomes, but
typically there are two outcomes, yes and no.
Unlike other shapes, there is an exit for each
outcome.
Input/Output

Data: This shape indicates that information is


either coming into the process from an external
source or going out from the shape. This shape
can also be used to represent materials and is
sometimes referred to as the Input/Output
shape.
Connector
Loop Symbol
Loop: This symbol is used when there are multiple iterations to be done, in other words, when there is a loop
in the workflow.
Example
PSEUDOCODE
Select t for the base, y for the height, and A for the area.
1. START
2. Read the value of t
3. Read the value of y
4. A = (t * y)/2
5. Print the value of A
6. END
Write the C code for the algorithm given
below:
Input Number And
Check If They Are Odd
Or Even:
Contents 2
sequential expression
Selection statements (if – else)
puts() command
Nested if else (Nested ifs)
Short circuit analysis in conditions
If – else usage errors
comparison operator
Control Structures
1. Sequential expression
"Structured programming consists of three steps."
2. Selection expressions
I. if (single select statement)
II. if .. else (double select statement)
III. switch .. case (multiple select statement)

3. Repetition phrases
I. while
II. do .. while
III. for

All C programs consist of a combination of these 7 control statements.


if in C
The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or
block of statements will be executed or not i.e if a certain condition is true then a block of statements is executed
otherwise not.

Here, the condition after evaluation will be either true


or false. C if statement accepts boolean values – if the
value is true then it will execute the block of statements
below it otherwise not. If we do not provide the curly
braces ‘{‘ and ‘}’ after if(condition) then by default if
statement will consider the first immediately below
statement to be inside its block.
Flowchart of if Statement
if-else in C
Flowchart of if-else Statement
Nested if-else in C
Flowchart of Nested if-else
if-else-if Ladder in C
The if else if statements are used when the user has to decide among multiple options. The
C if statements are executed from the top down. As soon as one of the conditions controlling
the if is true, the statement associated with that if is executed, and the rest of the C else-if
ladder is bypassed. If none of the conditions is true, then the final else statement will be
executed. if-else-if ladder is similar to the switch statement.
Flowchart of if-else-if Ladder
puts() command
It is a stdio.h library function.
Like printf, it prints text expressions within parentheses and double quotes to the screen.
At the end of printing, the cursor moves to the next line.
It acts as if the text was printed with the printf command and written \n at the end.
The only difference from the printf command: With the puts command, the variable value is not
written with the type specifier with the “, at the end of the text.
Example
Example 2:
Nested if
Multiple conditions can be tested simultaneously by placing other if-else structures within if-else
structures.
NOTE:
In nested if-else, we have to be careful with the indentation
because multiple if-else constructs are involved in this
process, so it becomes difficult to figure out individual
constructs. Proper indentation makes it easy to read the
program.
Nested Else-if statements
Nested else-if is used when multipath decisions are required.
The general syntax of how else-if ladders are constructed in ‘C’ programming is as follows:
Nested Else-if statements
This type of structure is known as the else-if ladder.
This chain generally looks like a ladder hence it is also called as an else-if ladder.
The test-expressions are evaluated from top to bottom. Whenever a true test-expression if
found, statement associated with it is executed.
When all the n test-expressions becomes false, then the default else statement is executed.
Let us see the actual working with the help of a program.
The above program prints the grade as per the marks scored in a test.

We have used the else-if ladder construct in the above program.


Explain step by step:
x = 5, y = 15 x = 15, y = 5
if (x < 10)
if (y > 10)
printf(“Air \n");
else
printf(“Force \n");
printf(“Academy \n");
if (x < 10) {
if (y > 10)
Printf("Air \n");
}
else {
printf("Force \n");
printf("Academy \n");
}
if (x < 10) {
if (y > 10)
printf("Air \n");
}
else
printf("Force \n");
printf("Academy \n");

BLG-130 Bilgisayar Programlama 57 /


Let’s write this code:
Question:
Calculate the average exam score of the university.
The user inputs her/his midterm and final exam scores.
The program will calculate the average score of the user. (midterm is
%40 and final is %60.)
If the score range is between 100 and 85, it means A
84-70 means B
55-69 means C or F.
Give the user a message: you passed the course, or F: you failed.
Summary
•Decision making or branching statements are used to select one path based on
the result of the evaluated expression.
•It is also called as control statements because it controls the flow of execution
of a program.
•‘C’ provides if, if-else constructs for decision-making statements.
•We can also nest if-else within one another when multiple paths have to be
tested.
•The else-if ladder is used when we have to check various ways based upon the
result of the expression.

You might also like