0% found this document useful (0 votes)
1 views23 pages

Problem Solving Lecture

The document outlines the fundamentals of computer programming, focusing on problem-solving techniques, algorithms, flowcharts, and the C programming language. It emphasizes the importance of understanding the input, process, and output (IPO) model before coding, and provides a structured approach to algorithm design and flowchart creation. Additionally, it introduces the C language, its significance, and the components of a C program, including variables, data types, and the compilation process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views23 pages

Problem Solving Lecture

The document outlines the fundamentals of computer programming, focusing on problem-solving techniques, algorithms, flowcharts, and the C programming language. It emphasizes the importance of understanding the input, process, and output (IPO) model before coding, and provides a structured approach to algorithm design and flowchart creation. Additionally, it introduces the C language, its significance, and the components of a C program, including variables, data types, and the compilation process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Fundamentals

Problem Solving, Algorithms,


Start Think

Flowcharts & C Programming


Clear?

Output Code

Presented By
Zannatul Fardaush Tripty
Lecturer, Department of CSE
World University of Bangladesh

1
Class roadmap
One connected path from problem to program

1 2 3 4

Problem Algorithm Flowchart C Program


Understand what is Write clear ordered Translate the logic into
Draw the logic visually
given and required steps syntax

Today’s goal
Students will be able to solve a simple numerical problem using input, process, output, algorithm,
flowchart, and basic C structure.

Computer Fundamentals 2
Problem solving in programming
A computer needs exact input, operations, and output

Before coding, identify the IPO model

INPUT PROCESS OUTPUT


Data we already have Operations to perform Expected result

Examples: two numbers, Examples: add, compare, Examples: sum, largest


marks, daily wage repeat, calculate value, salary, message

Guiding question: What do I have? What should I do with it? What should I produce?

Computer Fundamentals 3
Steps for solving a problem
A beginner-friendly method for first-year students

1 Read carefully 2 List IPO 3 Try examples


Understand the exact Separate input, process, and Use small values to verify the
question. output. idea.

4 Design logic 5 Test and debug


Write algorithm or draw Check the result and fix
flowchart. mistakes.

Do not start coding until the logic is clear in your own words.

Computer Fundamentals 4
Algorithm: definition and quality
A finite set of clear steps for solving a particular problem

Algorithm = blueprint for a Example: Add two numbers


program
1. Start
2. Read A and B
● Language independent: can be translated 3. SUM = A + B
into C, Python, Java, etc. 4. Print SUM
● Well-structured: steps have a clear order. 5. Stop
● Detailed enough: a programmer can directly
convert it into code.

A good algorithm must be finite, definite, effective, general, and must clearly define input
and output.

Computer Fundamentals 5
Three building blocks of algorithms
Sequence, decision, and repetition appear in most programs

SEQUENCE DECISION REPETITION


Steps run in the written order. A condition chooses one path. A step runs again and again.
Example: read A → read B → Example: if marks ≥ 40, pass; Example: add numbers from 1
add. otherwise fail. to 10.

Yes/No
Step 1 Step 2 ?
Path Body Again?

Computer Fundamentals 6
Top-down algorithm design
Break a large task into smaller parts

Calculate student result

Input marks Calculate total Decide grade

Math English Pass? Grade?

Why it helps
Top-down thinking prevents confusion because each small sub-task is easier to algorithmize, draw, and
code.

Computer Fundamentals 7
Flowcharts
A graphical representation of an algorithm

A flowchart uses standard symbols Start

and arrows to show how information


is processed.
Input data

Shows logic in a pictorial form. Read the chart


Process
Helps students understand decisions and from top to
loops. bottom or left to
Gives a permanent record of the solution. right.
Makes complicated programs easier to Output result
discuss.

Stop

Computer Fundamentals 8
Basic flowchart symbols
Shape meaning must be consistent

Start Terminator Input Input/Output SUM=A+B Process


Calculation /
Start / Stop Read / Print
assignment

A>B?
Decision Flow line A
Connector
Condition with Direction of Continue
Yes/No control elsewhere

Computer Fundamentals 9
Algorithm vs. flowchart
Two ways to represent the same logic

Algorithm Flowchart

• Step-by-step English-like procedure • Diagrammatic representation of an


• Quick to write for small problems algorithm
• Good for explaining exact operations • Uses boxes, symbols, and arrows
• Can become hard to follow for complex • Good for visualizing decisions and loops
logic • Takes more space and time to draw

Best classroom practice: write the algorithm first, then draw the flowchart, then
write code.

Computer Fundamentals 10
Example 1: Sum of two numbers
Input → process → output

Algorithm Start

1. Start
2. Read A and B Read A, B
3. SUM = A + B
4. Print SUM
5. Stop SUM = A + B

Print SUM

Test Stop
If A = 5 and B = 3, then SUM = 8.

Computer Fundamentals 11
Example 2: Check if two numbers are equal
Decision logic uses a condition

Algorithm Start

1. Start
2. Read A and B Read A, B
3. If A = B, print “Equal”
4. Otherwise, print “Not equal”
5. Stop Yes No
Print Equal A = B? Print Not Equal

Stop

Important: In C code, equality comparison is written with ==, not =.

Computer Fundamentals 12
Example 3: Sum of first 10 natural numbers
Repetition / loop example

Start
Idea
Start with SUM = 0 and I = 1. Add I
to SUM. Increase I by 1. Repeat
SUM=0, I=1
while I ≤ 10.

Algorithm Yes No
I ≤ 10?

1. Start
2. SUM = 0, I = 1
3. If I ≤ 10, SUM = SUM + I SUM=SUM+I
I=I+1
4. I = I + 1 and repeat step 3 Print SUM
5. Print SUM
6. Stop
Stop

Expected result: 1 + 2 + ... + 10 = 55

Computer Fundamentals 13
Example 4: Salary of a daily wager
A practical numerical problem

Problem Start
Read the number of days worked and
the wage per day. Calculate and display
total salary. Read DAYS, RATE

Algorithm Test
SALARY = DAYS × RATE DAYS = 20, RATE =
1. Start 500
2. Read DAYS and RATE SALARY = 10,000
3. SALARY = DAYS × RATE
Print SALARY
4. Print SALARY
5. Stop
Stop

Computer Fundamentals 14
From logic to C code
The solution stays the same; the representation changes

Algorithm / Flowchart C Program

#include <stdio.h>
int main() {
Read A and B Translate the same logic int A, B, SUM;
into exact C syntax. scanf("%d%d", &A, &B);
SUM = A + B SUM = A + B;
Print SUM printf("%d", SUM);
return 0;
}

Computer Fundamentals 15
Introduction to C programming language
A powerful language for structured programming

System programming
Mid-level language Structured language
Used in operating systems,
Combines low-level control with Uses functions, statements,
embedded systems, and fast
high-level syntax. conditions, and loops.
software.

Why learn C first?


C helps students understand variables, operators, memory, control flow, functions, and
precise programming logic.

Computer Fundamentals 16
Brief history and significance of C
Context for why C is still taught

1972 UNIX 1978 1989 1999


Dennis Ritchie
C is used to build Kernighan & Ritchie ANSI standardizes C C99 adds newer
develops C at Bell
UNIX publish their C book as C89 features
Labs

C remains important for systems, embedded devices, compilers, and


performance-critical applications.

Computer Fundamentals 17
Components of a C program
Functions and statements are the main building blocks

Statements
Functions main()
Instructions that perform
Building blocks of a C program The starting point of execution
actions
Example: main(), printf() Program begins here
Most end with semicolon ;

General form of a C function


return_type Remember
function_name(parameter_list) #include lines are preprocessor
{ directives, so they do not end with a
statement_sequence; semicolon.
}

Computer Fundamentals 18
First C program: Hello World
Understand each line instead of memorizing blindly

#include <stdio.h> #include <stdio.h>


Brings input/output functions such as printf().
int main()
{ int main()
printf("Hello World"); The program starts executing from main().
return 0;
} printf()
Displays text on the screen. The semicolon ends the
statement.

return 0;
Shows successful program termination.

Computer Fundamentals 19
Variables and data types in C
A variable is a named memory location that stores a value

Type of value Keyword Example Declaration syntax

Character data char 'A' type variable_name;

Whole number int 25 Examples:


int age;
Decimal number float 3.14 float salary;
char grade;
High-precision decimal double 3.14159265

No value void function return type

Choose a data type according to the kind of value you need to store.

Computer Fundamentals 20
Structure of a C program
How the main parts fit together

Header files main() function Statements return 0


#include <stdio.h> starting point program logic finish status

Header files contain function declarations and


A compiled C program typically begins from the first
macros. Common examples: stdio.h, stdlib.h,
executable statement inside main().
string.h, math.h.

Semicolon reminder: C uses semicolons to mark the end of most statements.


Preprocessor directives are different.

Computer Fundamentals 21
Compilation process in C
How source code becomes an executable program

1 Preprocessor 2 Compiler 3 Assembler 4 Linker


Handles #include, Connects libraries
Checks syntax and
#define, and Creates object code and builds
translates C
comments executable

If syntax is wrong,
compilation stops
hello.c hello.o [Link] / [Link]
source code object code executable
before an
executable is
created.

Computer Fundamentals 22
Class practice and recap
Solve first, then code

Practice 1
Practice 2 Practice 3
Read radius r and calculate area of
Read marks and print Pass if Print the sum of first N natural
a circle.
marks ≥ 40; otherwise print Fail. numbers.
area = 3.1416 × r × r

For each problem, students should write:


Recap
Input, process, and output Problem → IPO → Algorithm →
Algorithm Flowchart → C Code → Output
Flowchart
C program structure or partial code

Thank you! Any questions?


Computer Fundamentals 23

You might also like