Chapter 6 Grade 12

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Chapter 6

Fundamentals of programming
A computer is an electronic device that can be instructed to carryout sequences of arithmetic or
logical operations automatically via computer programming.

Modern computers have the ability to follow generalized sets of operations, called
programs. These programs enable computers to perform an extremely wide range of tasks.

Computers are used as control systems for a wide variety of industrial and consumer devices like
microwave ovens, remote controls, industrial robots, computer-aided design, personal computers,
and mobile devices.
Program: A set of directions (instructions) telling a computer exactly what to do.

programmer or developer: A person who write/develop program follow series of steps and
cycles starting from collecting user specifications until the software is ready for delivery

Programming Languages: Languages for specifying sequences of directions to a computer.

Algorithm
 A sequence of language independent steps which may be followed to solve a problem.

 The sequence of instructions for solving a particular problem.

 There are three basic logic structures to write algorithm:

1. Sequence
2. Selection 3. Iteration
1.sequential logic

 used for performing instructions one after another in sequence.

 The logic flow of an algorithm is from the top to the bottom

Example:

 Design an algorithm that adds two numbers and display the result

Solution

 Step 1: read the first number

 Step 2: read the second number

 Step 3: add the numbers

 Step 4: display the result

In the above problem the instructions are executed in the order they are from top to bottom

Example:

Design an algorithm which calculates area of a circle

Solution

1. Start.

2. Input radius in cm.

3. Calculate, area = 3.14 * (radius * radius)

4. Display area.

5. Stop

2. Selection logic

 Also known as decision logic

 Used for making decision

 Selecting the proper path out of two or more alternative paths in the program logic

 If condition is true do process 1, else do process 2

 Example 1
 Design an algorithm which calculates a quadratic equation

Ax2 + BX + C = 0

 Read the value of A, B and C

and store them in the memory.

 Step 2: If A = 0 and B = 0 then print

"No root exists" and stop. Else continue.

 Step 3: If A = 0 and B is not equal to 0,

then r1 = - C/B and output the

root. Else continue.

 Step 4: Compute D = (B2- 4AC)

 Step 5: If D = 0, then compute

r1 = r2 =( -B/2A), output r1 and r2 .

Else continue.

 Step 6: If D < 0, then write "Roots are

not real" and stop. Else continue.

 Step 7: If D > 0, then calculate

r1 = ( -B + sqrt(B2- 4AC)/(2A))

r2 = ( -B -sqrt(B2- 4AC)/(2A)) and display r1 and r2

3. Iteration

 Used when one or more instructions may be executed several times depending on some
condition.

 Single instructions used repeatedly

 Example. Design an algorithm that adds 100 numbers and display the result
 Step1 : Set i =1

Set sum=0

 Step 2: Read no(i)

 Step 3: Add sum and no(i)

Increment i

 Step 4: If i<=100 go to step 2

else continue

 Step 5: Write sum

Flow chart
Programmers use different kinds of tools or aids which help them in developing programs and
algorithms faster and better.

Flowcharts are the well-known representations of algorithms.

Flow charts

-Show the sequence of instructions in a single program.

-Boxes of different shapes are used to denote different types of operations.

-Actual instructions are written within these boxes.

-Boxes are connected by solid lines having arrow marks to indicate the flow of operation.

-Since a flowchart shows the flow of operations in pictorial form, any error in the logic of the
procedure can be detected easily.
Flowchart Symbols

Example
Introduction to phyton
Python is a versatile programming language that is widely used in data science, web
development, and artificial intelligence. At Data Science Cookie, we offer an Introduction to
Python program designed to equip participants with the basic skills needed to start coding in
Python.

Python is a multi-purpose programming language (due to its many extensions), examples are
scientific computing and calculations, simulations, web development.

Python Editors
An Editor is a program where you create your code (and where you can run and test it). Most
Editors have also features for Debugging. For simple Python programs you can use the IDLE
Editor, but for more advanced programs a better editor is recommended.
Examples of Python Editors:

• Python IDLE

• Visual Studio Code

• Spyder

• Visual Studio

• PyCharm

• Wing Python IDE

• Jupyter Notebook

These editors are shortly described below and in more detail later in this textbook. Which editor
you should use depends on your background, what kind of code editors you have used
previously, your programming skills, what you are going to develop in Python, etc.

You might also like