0% found this document useful (0 votes)
3 views

Practical Programming -L05

The document provides an overview of programming concepts, including definitions of programs, types of machines, and the structure of programmable machines. It covers algorithms, flowcharts, and pseudocode, along with practical exercises for applying these concepts. Additionally, it outlines the program translation cycle and suggests next steps for further learning in programming.

Uploaded by

Jaafar Abbakar
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)
3 views

Practical Programming -L05

The document provides an overview of programming concepts, including definitions of programs, types of machines, and the structure of programmable machines. It covers algorithms, flowcharts, and pseudocode, along with practical exercises for applying these concepts. Additionally, it outlines the program translation cycle and suggests next steps for further learning in programming.

Uploaded by

Jaafar Abbakar
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/ 21

University of Juba

Programming with C++


Practical Training
Lecture 5: Recap of Lectures 1-4
Overview
1. Introduction to Programming
2. Algorithms and Their Characteristics
3. Flowcharts and Pseudocode
4. Recap of Key Concepts
5. Practical Exercises
6. Program Translation Cycle
7. Summary and Next Steps
8. Q&A
9. Discussion
1. Introduction to Programming
What is a Program?
• A program is a set of instructions that a machine can understand and
execute automatically.
• It takes inputs, applies operations, and produces outputs.
Types of Machines
• Fixed-Program Machines: Hardwired for specific tasks (e.g.,
traditional weaving looms).
• Programmable Machines: Can execute different programs without
hardware changes (e.g., computers, smartphones).
Programming Languages
• Low-Level Languages: Machine language and assembly language.
• High-Level Languages: C++, Java, Python (easier to read and write).
1. Introduction to Programming
Structure of a Programmable Machine
• CPU: Central Processing Unit (brain of the computer).
• Memory: RAM (temporary storage) and ROM (permanent
storage).
• Input/Output Devices: Keyboard, mouse, monitor, etc.
• Buses: Data, address, and control buses for communication.
• Clock: Synchronizes operations.
• Software: System and application software.
• Power Supply: Provides energy to the machine.
2. Algorithms and Their Characteristics
What is an Algorithm?
• A step-by-step procedure to solve a problem or perform a task.
Characteristics of a Good Algorithm
• Input: Clearly defined inputs.
• Output: Produces at least one output.
• Clear Instructions: Unambiguous and easy to follow.
• Finiteness: Terminates after a finite number of steps.
• Effectiveness: Simple and executable with available resources.
• Language Independence: Works regardless of the programming language.
Variables and Constants in Algorithms
• Variables: Named storage locations that hold values (e.g., x, result).
• Constants: Fixed values that cannot be changed (e.g., 2, 'a').
• Arithmetic Expressions: Combination of variables, constants, and
operations (e.g., x * (y - 10)).
3. Flowcharts and Pseudocode
Flowcharts
• Visual representation of algorithms using standardized symbols.
• Purpose: Simplifies complex processes, enhances
communication, and aids debugging.
Control Structures:
• Sequential: Steps executed in order.
• Selection (Decision-Making): IF-THEN, IF-THEN-ELSE,
CASE.
• Looping (Repetition): WHILE, REPEAT-UNTIL, FOR.
3. Flowcharts and Pseudocode
Pseudocode
• A human-readable way to describe the steps of an algorithm.
Basic Constructs:
• SEQUENCE: Steps executed in order.
• IF-THEN-ELSE: Conditional execution.
• WHILE, REPEAT-UNTIL, FOR: Looping.
• CASE: Multiple conditions.
Rules for Writing Pseudocode:
• Capitalize constructs.
• Use indentation.
• Avoid language-specific syntax.
• Keep it simple.
4. Recap of Key Concepts
From Lecture 1:
Understanding programs, types of machines, and the structure of
programmable machines.
From Lecture 2:
Algorithms, their characteristics, and how to create and test them.
From Lecture 3:
Flowcharts, their symbols, and control structures.
From Lecture 4:
Pseudocode, its constructs, and best practices for writing it.
5. Practical Exercises
Exercise 1: Write pseudocode to find the sum of all odd numbers between 1
and 50.
Exercise 2: Write pseudocode to reverse a list of numbers.
Exercise 3: Design a flowchart to calculate the area of different shapes (e.g.,
circle, rectangle, triangle).
Exercise 4: Create a flowchart to check the strength of a password (e.g., weak,
medium, strong).
Exercise 5: Write pseudocode to calculate compound interest given principal,
rate, and time.
Exercise 6: Write pseudocode to generate the multiplication table for a given
number.
5. Practical Exercises
Exercise 1: Write pseudocode to find the sum of all odd numbers
between 1 and 50.

START
SET sum = 0
FOR i FROM 1 TO 50 STEP 2 DO
sum = sum + i
END FOR
PRINT sum
END
5. Practical Exercises
Exercise 2: Write pseudocode to reverse a list of numbers.
START
DEFINE list as an array
SET reversed_list = EMPTY
FOR i FROM LENGTH(list) DOWNTO 1 DO
APPEND list[i] TO reversed_list
END FOR
PRINT reversed_list
END
5. Practical Exercises
For this, you would create a flowchart with the following steps:
1.Start
2.Choose the shape (circle, rectangle, triangle)
3.Based on the shape:
• Circle: Input radius, calculate Area = π * r^2
• Rectangle: Input length and width, calculate Area =
length * width
• Triangle: Input base and height, calculate Area = 0.5 *
base * height
4.Display the result
5.End
5. Practical Exercises
Exercise 4: Flowchart to check the strength of a password

1.Start
2.GET password
3.Check length:
1. If less than 6 characters → Weak
2. If 6-10 characters with letters & numbers → Medium
3. If more than 10 characters with special symbols →
Strong
4.Display password strength
5.End
5. Practical Exercises
Exercise 5: Pseudocode to calculate compound interest

START
READ principal, rate, time
CALCULATE amount = principal * (1 + rate/100)^time
CALCULATE compound_interest = amount - principal
PRINT compound_interest
END
5. Practical Exercises
Exercise 6: Pseudocode to generate the multiplication table for a
given number.

START
READ number
FOR i FROM 1 TO 10 DO
PRINT number, "x", i, "=", number * i
END FOR
END
6. Program Translation Cycle
The program translation cycle consist of the following steps:
Source Code Preprocessing Compilation

The preprocessor processes The compiler translates the


The programmer writes
directives like #include, preprocessed source code
the code in a high-level
#define, and macros. into assembly language or
programming language
It generates an expanded source intermediate code.
(e.g., C, C++, Python,
code file by replacing macros The output is typically an
Java).
and including header files. object file (e.g., .obj or .o).

Assembly Linking Loading Execution


The linker combines multiple The loader loads the The CPU executes the
The assembler converts the
object files and libraries into a executable file into memory machine code instructions.
assembly code into machine
single executable file. for execution. The program runs and
code (binary code).
It resolves external references It allocates memory and produces the desired
The output is a machine-
and ensures all functions and prepares the program for output.
readable object file.
variables are correctly linked. execution.
6. Program Translation Cycle
Types of Program Translation
Compilation:
The entire source code is translated into machine code before execution.
Example: C, C++.
Interpretation:
The source code is translated and executed line by line.
Example: Python, JavaScript.
Just-In-Time (JIT) Compilation:
Combines compilation and interpretation. The code is compiled into machine
code during execution.
Example: Java (using JVM), .NET (using CLR).
7. Summary and Next Steps
Key Takeaways:
Programming involves writing instructions for machines to execute.
Algorithms are step-by-step procedures to solve problems.
Flowcharts and pseudocode are tools to design and communicate
algorithms.
Next Steps:
Practice creating algorithms, flowcharts, and pseudocode for
various problems.
Translate pseudocode into actual code using C++ or another
programming language.
Explore advanced topics like functions, exception handling, and
object-oriented programming.
8. Q&A
9. Discussion
Discussion Questions:
What challenges did you face while creating flowcharts or writing
pseudocode?
How do flowcharts and pseudocode help in the programming
process?
Homework Assignment
Write pseudocode for the following problems:
Calculate the sum of all even numbers between 1 and 100.
Find the smallest number in a list of 5 numbers.

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