0% found this document useful (0 votes)
18 views16 pages

Notions of Problem Solving and Coding

NOTIONS OF PROBLEM SOLVING AND CODING

Uploaded by

bichenyimaxime
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)
18 views16 pages

Notions of Problem Solving and Coding

NOTIONS OF PROBLEM SOLVING AND CODING

Uploaded by

bichenyimaxime
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
You are on page 1/ 16

MODULE 4: NOTIONS OF PROBLEM SOLVING AND

CODING

CHAPTER ONE: NOTIONS ON PROBLEM SOLVING

Lesson 1:

Problem Solving Steps


 Step 1: Identify the problem
• Define the problem clearly
• Understand the constraints and limitations
 Step 2: Understand the problem
• Gather information and data
• Analyze the problem and identify key issues
 Step 3: Plan a solution
• Brainstorm possible solutions
• Evaluate and select the best solution
 Step 4: Implement the solution
• Put the plan into action
• -Write the code or create the program
 Step 5: Test and evaluate the solution
• Test the solution with different inputs and scenarios
• Evaluate the results and make improvements
LESSON 2: Solving problems 1
Example problem: Calculate the area of a rectangle with a length of 6 cm and a width of
4 cm.

- Step 1: Identify the problem

- Define the problem: Calculate the area of a rectangle

- Understand the constraints: Length = 6 cm, Width = 4 cm


- Step 2: Understand the problem

- Gather information: Formula for area = length x width

- Analyze the problem: Identify the values for length and width
- Step 3: Plan a solution
- Brainstorm possible solutions: Use the formula for area
- Evaluate and select the best solution: Use the formula with the given values
- Step 4: Implement the solution
- Plug in the values: Area = 6 cm x 4 cm
- Calculate the area: Area = 24 cm^2
- Step 5: Test and evaluate the solution
- Test with different values: Try length = 3 cm and width = 5 cm
- Evaluate the results: Area = 15 cm^2 (correct!)

Computational Thinking
Definition: The process of breaking down complex problems into smaller, manageable
parts to solve them efficiently.
Concepts:
 Decomposition: Breaking down problems into smaller sub problems
 Pattern recognition: Identifying patterns and relationships in data
LESSON 3: Solving problems 2
Example problem: Find the sum of all multiples of 3 from 1 to 20.
Step 1: Decompose the problem
- Break down the problem: Identify multiples of 3, add them up
- Identify the pattern: Multiples of 3 increase by 3 (3, 6, 9, ...)
Step 2: Recognize the pattern
- Identify the next multiple of 3: 18 (last multiple of 3 in the range)
- Use the pattern to find the sum: 3 + 6 + 9 + 12 + 15 + 18 = 63
 Abstraction: Focusing on essential features while ignoring nonessential details
Example problem: Write a program to greet a user. Step 1: Focus on
essential features
- Identify the essential feature: Greeting the user
- Ignore non-essential details: User's name, location, etc.
Step 2: Write the program
- Use a block programming environment (e.g. Scratch)
- Create a sprite that says "Hello, World!"
Writing Programs in Block Programming Environments
- Example: Write a program to move a sprite across the screen.
- Step 1: Drag and drop blocks to create the program
- Step 2: Test and evaluate the program
- Step 3: Refine the program as needed

 Algorithmic thinking: Developing step-by-step solutions to problem.

Differences between 2D representations and3D


2D Representations are Flat, two-dimensional images or shapes Examples: Drawings,
diagrams, graphs while 3D Representations are Three-dimensional objects or shapes with
depth and volume. Examples: Models, sculptures, real-world objects.
CHAPTER TWO: EXPLORING BASIC ALGORITHMIC
INSTRUCTIONS FOR COMPUTERS

Lesson 4: Program development life cycle

Program Development
Definition: Program development is the process of designing, creating, testing,
and maintaining software programs. Program development helps solve real-world
problems and create innovative solutions.

Program Development Life Cycle (PDLC):


1. Problem Definition: Identify the problem or need.
2. Analysis: Break down the problem into smaller parts.
3. Design: Create a detailed design or algorithm.
4. Implementation: Write the code.
5. Testing: Verify the program meets requirements.
6. Maintenance: Update and modify the program as needed.

PRACTICALS. Apply the program development life cycle to a problem.

Example: Develop a program to manage student grades.

Follow the PDLC stages to create a solution:

Lesson 5: Notions on algorithms


Definition: An algorithm is a step-by-step procedure for solving a problem or
achieving a particular goal. It’s a well-defined procedure that takes some input
and produces a corresponding output.

Characteristics of a Good Algorithm


A good algorithm should have the following characteristics:
1. Clarity Easy to understand and interpret.
2. Conciseness: Fewer steps and less complex.
3. Efficiency: Minimizes resources and time.
4. Effectiveness: Produces the correct output.
5. Generality: Can be applied to similar problems.
6. Reliability: Produces consistent results.
7. Finite: it must have an end

Lesson 6: Algorithmic instructions


Simple Algorithmic Instructions
Algorithmic instructions are step-by-step procedures.

Examples of simple algorithmic instructions:

1. Sequence: Perform steps one after the other.


2. Selection: Choose between two or more alternatives.
3. Iteration: Repeat steps until a condition is met.
4. Repetition: Repeat steps for a specified number of times.

Lesson 7: Flowcharts
A flowchart is A graphical representation of an algorithm using symbols and
arrows.

Flowchart symbols:
1. Rectangle (Process): Represents a step or action in the algorithm,
such as a calculation or data manipulation.

2. Diamond (Decision): Represents a choice or condition in the


algorithm, such as a yes/no question or a true/false statement.

3. Oval (Start/End): Represents the beginning or end of the algorithm,


indicating where the process starts and stops.
4. Arrow (Flow): Represents the direction of flow between steps,
showing the sequence of actions.

Flowchart rules:

 Start with a clear problem statement.


 Use one entrance and one exit.
 Use arrows to show flow direction. Avoid crossing arrows.

Lesson 8: Integration activities


Example 1: Sequence Algorithm_
Write an algorithm to calculate the sum of the first n positive integers.

Solution:

1. Input n

2. Initialize sum = 0

3. For I = 1 to n

4. sum = sum + i

5. Output sum

Example 2: Selection Algorithm_


Write an algorithm to find the maximum of two numbers.
Solution:

1. Input a and b

2. If a > b then

3. Output a

4. Else

5. Output b

_Example 3: Iteration Algorithm_


Write an algorithm to calculate the factorial of a given number n.
_Solution:_
1. Input n

2. Initialize factorial = 1

3. For I = 1 to n

4. factorial = factorial x i

5. Output factorial

_Example 4: Recursion Algorithm_


Write an algorithm to calculate the Fibonacci sequence up to the nth term.
_Solution:_
1. Input n

2. If n = 1 then

3. Output 0

4. Else if n = 2 then

5. Output 1

6. Else

7. Output fib(n-1) + fib(n-2)

VI. Designing Solutions


- Use structured diagrams and pseudocodes to construct solutions.

- Decompose problems into smaller parts and design solutions for each.
- Example: Design a solution for the grade management system using
structured diagrams and pseudocodes
Lesson 9: Types of programming language
Differences between an Algorithm and a Program
A computer program is a set of instructions written in a programming
language that tells the computer what to do in order to solve a problem. In other
words, it is an algorithm expressed in a programming language.

1. Abstraction: Algorithms are abstract, focusing on the logic and steps,


while programs are concrete implementations.
2. Language: Algorithms are language-independent, while programs are
language-dependent (e.g., Python, Java, C++).
3. Purpose: Algorithms solve problems or achieve goals, while programs
execute algorithms to perform specific tasks.
4. Level of detail: Algorithms provide a high-level description, while
programs include detailed instructions and syntax.
Introduction to Programming
Programming or program development is the act of writing computer programs.

Programming Languages
A programming language is a set of words, symbols and rules for writing
computer programs. A programming language is determined by its syntax (rules
guiding the formulations of the program statement) and semantics (it describes the
meaning of the program statement i.e. it is the vocabulary of the language).
Two categories of programming languages exist: low-level languages and high-
level languages.

1. Low Level Languages


Low level languages are those that allow low-level access to hardware
features. There are two categories of low-level languages: machine language and
assembly language.

1. Machine Language:
It is the computer’s language. It is the language the computer understands.
Machine language instructions are written in binary (a series of 0s and 1s), and are
directly executable by the computer (processor). Each machine language statement
corresponds to one machine action. Machine language is the first generation of
programming languages.

2. Assembly Language:
It is a low-level language that uses words or symbolic codes called mnemonic
codes to represent machine language instructions. Assembly language is the
second generation of programming languages. ADD, LOAD, and STORE are
known as mnemonics.
Advantages of low-level languages

1. Programs are easier to write and understand when compared to machine


language.
2. It can produce small size program
3. It can produce very fast code as it allows low-level access to hardware
features.

Disadvantages of high level languages

1. Programs are not as easy to write and understand when compared to high
level languages
2. Programs are tied to specific computer hardware and can’t be reused on
another kind of computer.
2. High Level Languages
A high-level language is a language that is closer to human language.It
allows a programmer to write programs without having to understand the inner
workings of the computer. High-level languages are the third generation of
programming languages. Examples of high-level languages are C, BASIC
(Beginners All-purpose Symbolic Instruction Code), Pascal, Java, FORTRAN
(FORmular TRANslation), C++, COBOL (COmmon Business Oriental Language
), etc.

Advantages of high-level languages 1. Programs


are easy to write and understand

2. Built-in libraries to perform routine tasks


3. Can be ported to multiple hardware setups from same.
Disadvantages of high-level languages

1. Slower than second generation languages


2. Produce larger program files for same functionality than second generation
languages.
3. May not allow for low level hardware access.
Differences between Low level languages and high-level languages
1. High-Level Languages are easy to read and write, while Low-Level
Languages are difficult to read and write.
2. High-Level Languages are abstract and focus on logic, while
LowLevel Languages are close to machine language and focus on memory
management.
3. High-Level Languages are portable and can run on multiple platforms,
while Low-Level Languages are platform-dependent and require specific
hardware.
4. High-Level Languages have built-in support for data structures and
object-oriented programming, while Low-Level Languages require manual
memory management and do not support object-oriented programming
directly.

Lesson 10: Language translators


To run a program on a computer, the program needs to be translated into the
machine language of the computer on which it will run.
A language translator is a computer program that translates program instructions
from one language into another without loss of original meaning. There are three
types of language translators: assembler, compiler, and interpreter.

Assembler: An assembler translates instructions from assembly language into


machine language. This process is called assembling.

Compiler: A compiler is a computer program that translates an entire block


(i.e.at once) of instructions written in a high-level language into machine language
instructions before executing it. The high-level language program is called source
code and the generated machine language program is called object code. Types of
compiled languages are Pascal, C, C++, FORTRAN, JAVA, and Python.
Advantages of a Compiler

 Fast in execution
 he object/executable code produced by a compiler can be distributed or
executed without having the compiler present.
 The object program can be used whenever required without the need of
recompilation.

Disadvantages of a Compiler
 Debugging a program is much harder. Therefore, not so good at
finding errors
 When an error is found, the whole program has to be re-compiled
though compilation process is so fast.

Interpreter
An interpreter is a computer program that translates and executes instructions
written in a high-level language into machine language instructions one line at a
time. If a program performs a section code 1000 times, then the section is
translated into machine code 1000 times since each line is interpreted and then
executed. Types of interpreters: BASIC, Lisp, Logo, etc.

Advantages of an Interpreter
 Good at locating errors in programs since interpretation is done line by
line.
 Debugging is easier since the interpreter stops when it encounters an
error.
 If an error is corrected, there is no need to retranslate the whole
program.

Disadvantages of an Interpreter

 It is slow as interpretation is done line by line.


 Translation has to be done every time the program is to be executed
since no object code is produced.
 For the program to run, the interpreter must be present.
Differences between compilers and interpreters
1. Compilers translate source code into machine code before execution,
while Interpreters translate and execute source code line by line.
2. Compilers produce an executable file, while Interpreters do not
produce an executable file.
3. Compilers are faster in execution, while Interpreters are slower in
execution.
4. Compilers detect errors at compile-time, while Interpreters detect
errors at runtime.
Lesson 11: Integrated Development Environment IDE
Definition of Integrated Development Environment (IDE): An IDE is single
software that provides comprehensive facilities to computer programmers in
which all software development is done. It normally consists of: a source code
editor, a compiler or interpreter, automation tools, a debugger.
Practical: Make use of an IDE to create, edit, translate and run a program

CHAPTER THREE: IMPLEMENTING ALGORITHMS USING A


PROGRAMMING LANGUAGE

Lesson 12: Introduction to programming

Program Errors and Correction


1. Syntax Error: A syntax error is a place in a program where the syntax
rules of the programming language were not followed. A syntax error will stop
a program compilation or interpretation.

2. Semantic Error: Semantics specify the meaning of an orderly well-


formed program. A semantic error is a situation where the programmer
misunderstood the meaning of something in the programming language.
Compilation and interpretation do not detect semantic errors. Semantic errors are
detected from wrong results. They are also called logical errors.

Something may be syntactically correct but semantically incorrect.

3. Run-time Error
Run-time error is an error that occurs when a program is being executed. For
example, a run-time error may occur if division by 0 is attempted. A run-time
error may cause the program to stop execution.

4. Debugging
An error in a computer program is known as a bug and debugging is the
process of finding and removing bugs. Syntax errors and semantic errors are bugs.
A debugger is the software tool (or person) used for this purpose.
Key Words and Operators
1. Key Words
Any high-level language has its own set of predefined words which are
used to write programs. Each of these words has a specific meaning, and cannot
be used for any other purpose within the programming language IDE. These
words are known as key words or reserved words. Therefor A reserved word is a
key word in a programming language that has a specific meaning and cannot be
used for any other purpose. E.g. If, Else, For, To and goto.

2. Operators
Operators are used to perform various operations on data. They may be
arithmetic, relational or logical. Arithmetic operators are +, -, *, %, ++ and -- and
/. Relational operators include <, >, ==, <=, >= and !=. Logic operators are NOT
(!), AND (&&) and OR (||).

Evaluation
Lesson 13: Declarative instructions
Basic data types in a programming language
1. Integers (int): they are Whole numbers, either positive, negative, or
zero. Examples are: 1, 2, 3, -4, 0. They are used for counting, indexing, and
calculations
2. Floating-point numbers (float): They are Decimal numbers, either
positive, negative, or zero. Examples are: 3.14, -0.5, 0.0. They are used for
calculations requiring decimal precision.
3. Characters (char): These are Single characters, either letters, digits,
or special characters. Examples are: 'a', 'B', '1', '@'. They are used for text
processing and input/output operations.

4. Strings (string): These are Sequences of characters, either letters,


digits, or special characters. Examples are: "hello", 'hello', "123",
"abc@def". they are used for text processing, input/output operations, and
storing text data
5. Boolean values (bool): They are True or false values. Examples: true,
false. They are used for logical operations, conditional statements, and
decision-making

Practical: Write the instructions to declare variables and constants


correctly in a given programming language
Declaring Variables and Constants

Variables:
 Names given to memory locations storing data
 Declared using the syntax: `type variable_name;`

Examples:

 int x;` declares an integer variable named x


 `float y;` declares a floating-point variable named y

Constants:

 Values that cannot be changed during program execution


 Declared using the syntax: `const type constant_name = value;`
Examples:

 const float PI = 3.14;` declares a constant named PI with value


3.14
 `const int MAX_SIZE = 100;` declares a constant named
MAX_SIZE with value 100
Lesson 14: Operators
A. Arithmetic Operators:
1. Addition (+): This operator adds two or more numbers together. For
example, `2 + 3` equals `5`.
2. Subtraction (-): This operator subtracts one number from another. For
example, `5 - 3` equals `2`.
3. Multiplication (*): This operator multiplies two or more numbers
together. For example, `4 * 5` equals `20`.
4. Division (/): This operator divides one number by another. For
example, `10 / 2` equals `5`.
5. Modulus (%): This operator finds the remainder of a division
operation. For example, `17 % 5` equals `2`. B. Comparison Operators:
1. Equal to (==): This operator checks if two values are equal. For
example, `5 == 5` is true.
2. Not equal to (!=): This operator checks if two values are not equal.
For example, `5 != 3` is true.
3. Less than (<): This operator checks if one value is less than another.
For example, `3 < 5` is true.
4. Greater than (>): This operator checks if one value is greater than
another. For example, `5 > 3` is true.
5. Less than or equal to (<=): This operator checks if one value is less
than or equal to another. For example, `3 <= 5` is true.
6. Greater than or equal to (>=): This operator checks if one value is
greater than or equal to another. For example, `5 >= 3` is true. C. Logical
Operators:
1. And (&&): This operator checks if two conditions are both true. For
example, `true && true` is true.
2. Or (||): This operator checks if either of two conditions is true. For
example, `true || false` is true.
3. Not (!): This operator reverses the truth value of a condition. For
example, `! true` is false.
Lesson 74: Input, output and assignment instructions
A. Input:
1. Reading data from the user or a file using `scanf()` or `cin` in C++. For
example, `scanf("%d", &x)` reads an integer from the user and stores it
in the variable `x`.
B. Output: Displaying data to the screen or a file using `printf()` or
`cout` in C++. For example, `printf("%d", x)` displays the value of the
variable `x` on the screen.
C. Assignment: Storing a value in a variable using the assignment
operator (=). For example, `x = 5` assigns the value `5` to the variable `x`.

Lesson 15: Representing an algorithm as a program


Produce programs when given an algorithm
Lesson 16: Write, compile and execute a program 1
Make use of an IDE to create, edit, compile, and execute programs
Examples of IDE: Eclipse, Visual Studio, IntelliJ, NetBeans

Lesson 17: Write, compile and execute a program 2


Make use of an IDE to create, edit, compile, and execute programs
Lesson 18: Integration activities

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy