0% found this document useful (0 votes)
17 views35 pages

Chapter 1

For freesh man student

Uploaded by

floweryears928
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views35 pages

Chapter 1

For freesh man student

Uploaded by

floweryears928
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter one

Introduction to programing
What is a programming?
• Computer
• an electronic device that accepts data,
• performs computations, and makes logical decisions according to
instructions that have been given to it
• then produces meaningful information in a form that is useful to the
user
• Computer programs
• Sequences of instructions expressed in specific programming language:
• the instructions that tells the computer what to do
• Is the process of creating computer software using a programming
Language.
• Computer programs (also know as source code)
• Is often written by professionals known as Computer Programmers
• example of programing languages are: Java, C, C++, python, PHP, Perl,
Ruby
• Computer programming
• is the process of writing the, testing it for errors (debugging)
INGREDIENTS:
 1 cup butter
 2 cups (370 grams)
granulated sugar
 4 large eggs
 3 cups (384 grams) cake INSTRUCTIONS:
flour • Step 1: Preheat oven to 350°. Grease and flour cake pan
• Step 2: Cream sugar and butter together in a mixing bowl. Add eggs, one at a
 1 tablespoon (12 grams) time, beating briefly after each addition. Mix in vanilla.
baking powder
 1 cup milk • Step 3: Combine flour and baking powder in a separate bowl. Add to the wet
ingredients and mix well. Add milk and stir until smooth.
 2 teaspoons vanilla • Step 4: Pour the mixture into prepared pan.
• Step 5: Bake for 28 to 30 minutes.
• Step 6: Remove from the oven and cool completely.
• Step 7: Enjoy!
1.2. Reasons to study programming

• Increased ability to express ideas.


• Improved background for choosing appropriate languages
• Greater ability to learn new languages
• Understand significance of implementation
• Ability to design new languages
• Overall advancement of computing
1.3. Types and usage of computer languages

a) MACHINE LANGUAGE:
• this is the only language computer actually understands (computer’s native
language)
• Machine dependent
• Cannot be run on another type of computer without significant alterations (different
computers understand different sequences)
• Also called Binary language(sequence of zeroes and ones )
• hard for humans to understand: 01010001…
… cont
b) ASSEMBLY LANGUAGE:
• Arising from the machine language difficulties,
• Writing programs in machine language is slow and tedious for
most programmers
• Here the sequence of 0’s and 1’s are replaced with symbols
• The language provided an opportunity to the programmers
to use English like words that were called Mnemonics
• Symbolic language(assembly) is machine dependent just
like machine language
• Assembler
• translate Assembly language to machine language
• still hard for humans to understand:
• ADD d0,d2
… cont
C) HIGH LEVEL LANGUAGE:
• High level language introduces the use of “almost English” language in writing
computer programs
• It is much easier to write a program than the low level language
• A program written in a high-level language is just like giving instructions to a
person in daily life.
• Examples are FORTRAN, COBOL, PASCAL, C, java, python etc.
• BASIC - Beginners All Purpose Symbolic Instruction Code
• COBOL - Common Business Oriented Language, widely used for business
data processing task
• FORTRAN - Formula Translation
• PASCAL - Named after French mathematician Blaise Pascal
• closer to English easier to read and understand:
• Python
• print("Hello, world!")
• The above computer program instructs the computer to print "Hello, World!" on the
computer screen.
Interpreting vs. Compiling Programs
• Each type of computer only “understands” its own
machine language (zeroes and ones)
• Thus we must translate from High-level language to
machine language
• a team of experts programs a translator, called a “compiler”
which translates entirety of a High-level language program to
an executable file in computer’s native machine language.
• An alternative to compiling your program is to
interpret your program
• each line of your program is translated into machine language
and immediately executed
… cont

• Like translating between natural languages


• Compiler: human translator translates book in its entirety
and then translated book is printed and read
• Interpreter: human interpreter translates each spoken
statement in sequence as speaker is speaking
1.4. Programming paradigms
( group assignment)
Introduction
• A computer is a useful tool for solving a variety of problems.
• To make a computer do anything (i.e. solve a problem), you have to
write a computer program.
• In a computer program you tell a computer, step by step, exactly what
you want it to do.
• The computer then executes the program, following each step.
What is Algorithm?
• The sequence of steps to be performed in order to
solve a problem by the computer is known as an
algorithm.
• Algorithms can be expressed in many different
notations, including natural languages, pseudocode,
flowcharts and programming languages.
• Natural language expressions of algorithms tend to
be verbose and ambiguous, and are rarely used for
complex or technical algorithms
Characteristics of a good algorithm

Output unambiguity
6 1
Input Characteristics of Flexibility
5 Algorithm
2
Language
Finiteness
4 3 independent
Guidelines for Developing an Algorithm:

An algorithm will be enclosed by START (or BEGIN) and STOP (or
END)
To accept data from the user, generally used statements are INPUT,
READ, GET or OBTAIN
To display the result or any message, generally used statements are
PRINT, DISPLAY, or WRITE
Generally, COMPUTE or CALCULATE is used while describing
mathematical expressions, and based on the situation relevant
operators can be used.
Algorithm Examples
1. Algorithm to compute square of a number
• Step 1: Start
• Step 2: Read the Number
• Step 3: Compute square = Number * Number
• Step 4: Print square
• Step 5: Stop

2 Algorithm to find the average of three numbers


• Step 1: start
• Step 2: read values of x, y, z
• Step 3: s = q + y+ z
• Step 4: A = s/3
• Step 5: write value of A
• Step 6: stop
Algorithm Examples

3. Algorithm: Calculation of Simple Interest


• Step 1: Start
• Step 2: Read principle (P), time (T), and rate (R)
• Step 3: Calculate I = P*T*R/100
• Step 4: Print I as Interest
• Step 5: Stop
• Pseudocode and flowcharts are structured ways to
express algorithms that avoid many ambiguities
common in natural language statements, while
remaining independent of a particular
implementation language.
• Programming languages are primarily intended for
expressing algorithms in a form that can be executed
by a computer, but are often used to define or
document algorithms.
Flowchart
• Flowchart is a graphical or symbolic representation of
an algorithm or process.
• It is the diagrammatic representation of the step-by-
step solution to a given problem.
• It describes what operations (and in what sequence)
are required to solve a given problem.
• It doesn’t depend on any particular programming
language.
• Flowchart uses different symbols (geometrical shapes)
to represent different processes and arrows to show
relationship
Purpose of Flowcharting:
An aid in developing the logic of a program.
Provides means of communication with others about the
program.
A guide in coding the program.
Verification that all possible conditions have been
considered in a program.
Documentation for the program.
Example 1: - Draw flowchart of an
algorithm to add two numbers and
display their result.
• Algorithm description:-
• Step 1: Start
• Step 2: Read the two numbers (A and B)
• Step 3: Assign the sum of A and B to
Sum
• Step 4: Display the result ( Sum)
• Step 5: Stop
• Example 2:- Write an algorithm description
and draw a flow chart to check a number is
negative or not.
• Algorithm description:-
• Step 1: Start
• Step 2: Read number x
• Step 3: If x is greater than zero
write a message x is positive
number
• Step 4: else write a message x
is negative number
• Step 5: Stop
• Example 3: - Write the algorithmic description and
draw a flow chart to find the following sum.
Sum = 1+2+3+…. + 50
Algorithmic description:-

1.Initialize a variable sum to 0


2.Initialize another variable i to 1. This variable will act as a counter for the loop.
3.Repeat the following steps until i is greater than 50:
• Add the current value of i to the sum variable
• Increment i by 1 (i = i + 1).
4.Print the final value of sum.
#include <iostream>

using namespace std;

int main() {
int sum = 0;
int i;
// Loop from 1 to 50 and add each
number to the sum
for (i = 1; i <= 50; i++) {
sum += i;
}

// Print the final sum


cout << "The sum of 1 to 50 is: " << sum
<< endl;

return 0;
} 50. Inside the loop, the current value
This program uses a for loop to iterate from 1 to
of i is added to the sum variable. After the loop completes, the final sum is printed to
the console.
Pseudocode
• Derived from pseudo and code is a compact and informal
high-level description of a computer algorithm that uses
the structural conventions of programming.
• It is easy for humans to read
• No single standard/ syntax for writing pseudocode
• There's no single standard for writing pseudocode, as long as it
clearly conveys the program's logic.
• It is not an executable program
• It's not a real programming language
• Writing it will save you time later during the construction
& testing phase of a program's development.
Rules used to write pseudocode
1. Write only one statement per line
 Each statement in your pseudocode should express just one action for the
computer.
2. Capitalize initial keyword
We will use just a few keywords: Common keywords like "if,"
"else," "while," "for," "function," and "return" are used to represent
control flow and program structures
• INPUT/READ – indicates a user will be inputting something.
• OUTPUT/WRITE – indicates that an output will appear on the screen.
• WHILE – a loop (iteration that has a condition at the beginning)
• FOR – a counting loop (iteration)
• IF – THEN – ELSE – a decision (selection) in which a choice is made any
instructions that occur inside a selection or iteration are usually indented.
3. Indent to show hierarchy
• indentation refers to the practice of adding spaces or tabs at the beginning
of lines of code to visually represent the structure and hierarchy of the
program
• We will use a particular indentation pattern in each of the design
structures:
• Consistent indentation improves readability and visually shows the
program's structure.
Sequence: Statements that execute one after the other are typically
kept at the same level of indentation
Selection(if-else): indent the statements that fall inside the selection
structure, but not the keywords that form the selection
Looping(for, while): ident the statements that fall inside the loop,
but not the keywords that form the loop
Pseudocode examples
#include <iostream>
Pseudocode Example
using namespace std;
: Issue for driver license
BEGIN int main() {
OUTPUT "Enter your age" int age;

INPUT age cout << "Enter your age: ";


IF age>=18 then cin >> age;

OUTPUT "You can take if (age >= 18) {


driving licence" cout << " You can take driving licence."
ELSE << endl;
}
OUTPUT "You can't take else {
driving licence" cout << " You can't take driving
ENDIF licence" << endl;
}
END
return 0;
}
4. End multiline structures
• See how the IF/ELSE/ENDIF is constructed above.
• The ENDIF is always in line with the IF.
• Always use scope terminators for loops and iteration.
Pseudocode Example :
Print Numbers from 1 to 100.
BEGIN
NUMBER counter
FOR counter = 1 TO 100 STEP 1 DO
OUTPUT counter
ENDFOR
END
5. Keep statements language independent
E.g. Draw a flowchart and write a pseudocode to find the
greater number between two numbers.
Solution
Pseudocode
Flow Chart
BEGIN
INPUT A
INPUT B
IF A > B THEN
OUTPUT A+ "is higher"
ELSE
OUTPUT B+ "is higher"
ENDIF
END
Translating algorithms to programming languages
• Example:-
• Write a program that obtains two integer numbers
from the user. It will print out the sum of those
numbers.
• Algorithm:
• Prompt the user to enter the first integer
• Prompt the user to enter a second integer
• Compute the sum of the two user inputs
• Display an output that explains the answer as the sum
• Display the result
Basic program development tips
The program we design in any programming language need
to be:
Reliable: the program should always do what it is expected
to do and handle all types of exception.
Maintainable: the program should be in a way that it could
be modified and upgraded when the need arises.
Portable: It needs to be possible to adapt the software
written for one type of computer to another with minimum
modification.
Efficient: the program should be designed to make optimal
use of time, space and other resources of the computer
Program Development Cycle:

1. Analyze: Define the problem


2. Design: Plan the solution to the problem
4. Code: Translate the algorithm into a programming language.
5. Debug and Test: Locate and remove any errors in the
program.
6. Complete the Documentation: Organize all the materials that
describe the program
Quiz
• Data Structures( Chapter 5)
• What is a computer program and Algorithm write the
difference of the two?
• Differentiate between an interpreter and compiler
• State why High level language is very easy to use
compared with the earlier ones
• Write the algorithmic description and draw a flow
chart that find the difference and the division of two
numbers and display the results

You might also like