SlideShare une entreprise Scribd logo
1  sur  51
Introduction to Programming
(CS1123)
By
Dr. Muhammad Aleem,
Department of Computer Science,
Mohammad Ali Jinnah University, Islamabad
Fall 2013
What is a Computer?
• A computer is a electro-mechanical device that
works semi-automatically to process input data
according to the stored set of instructions and
produces output or resultant data.
A Computer
System
Instructions
Data
Results
Components of a Computer System
Main Memory
CPU
Arithmetic and Logic Unit
Control Unit
Computer
Peripheral Devices / Connected devices
Keyboard Mouse Display CD Rom Hard Disk
Computer Instructions and Programs
• Instruction: A computer instruction is a command
or directive given to a computer to perform
specific task.
Examples: Add 2 and 5, Print “Hello World”
• Program: A program is sequence of instructions
written in programming language that directs a
computer to solve a problem
Examples: Draw a square, etc.
Computer Instructions and Programs
• Program “Draw a square”
1 – Draw a vertical line of length n inches
2 – Draw a horizontal line of n inches
3– Draw a vertical line of length n inches
4 – Draw a horizontal line of n inches
Computer Software System
Operating Systems
(Windows, Linux, MAC, Solaris)
Compilers / Libraries
(C++, C, Java)
Application Programs
(.cpp, .c, .java,)
Computer Hardware
Programming Languages
• Classification of programming languages:
1. Machine language
2. Low-level languages
3. High-level languages
1. Machine level languages
• A computer understands only sequence of bits or 1’s
and 0’s (the smallest piece of information)
• A computer program can be written using machine
languages (01001101010010010….)
• Very fast execution
• Very difficult to write and debug
• Machine specific (different codes on different
machines)
2. Low level languages
• English encrypted words instead of codes (1’s and
0’s)
• More understandable (for humans)
• Example: Assembly language
• Requires: “Translation” from Assembly code to
Machine code
compare:
cmpl #oxa,n
cgt end_of_loop
acddl #0x1,n
end_of_loop:
1001010101001101
1110010110010100
0101010111010010
0110100110111011
1101100101010101
Assembler
Assembly Code Machine Code
3. High level languages
• Mostly machine independent
• Close to natural language (English like language
keywords)
• Easy to write and understand programs
• Easy to debug and maintain code
• Requires compilers to translate to machine code
• Slower than low-level languages
3. High level languages
• Some Popular High-Level languages
– COBOL (COmmon Business Oriented Language)
– FORTRAN (FORmula TRANslation)
– BASIC (Beginner All-purpose Symbolic Instructional Code)
– Pascal (named for Blaise Pascal)
– Ada (named for Ada Lovelace)
– C (whose developer designed B first)
– Visual Basic (Basic-like visual language by Microsoft)
– C++ (an object-oriented language, based on C)
– Java
Programming Paradigms
• Programming paradigm is the fundamental
style of computer programming
1. Imperative
2. Functional
3. Logical
4. Object Oriented
Imperative Paradigm
• Machine based modes (Sequence of steps
required to compute)
• Statements executed sequentially step-wise
• Emphasis on How is to compute
• How to obtain the required results or output
• Example languages: C, Pascal, Ada, etc.
Imperative Paradigm Languages
Functional/Logical Paradigm
• Specify what is to be computed using
programming abstractions.
• Responsibility of a programmer is to specify What
is to compute
• How to compute is implementation dependent
and is managed by language compiler
• Example languages: Lisp, Scheme, Erlang, Haskell,
Scala etc.
Object-Oriented Paradigm
• Programming with Abstract Data Types, to model
real world
• Basic Program Unit: Class (a programmer defined
type)
– A entity that contains data and methods which work
on the data
• Basic Run-time Unit: Object
– Instance of a class during execution
Problem Solving Steps
1. Understand the problem
2. Plan the logic
3. Code the program
4. Test the program
5. Deploy the program into production
1. Understanding the Problem
• Problems are often described in natural language
like English.
• Users may not be able to specify needs well, and
the needs may changing frequently
• Identify the requirements
1. Inputs or given data-items
2. Required output(s) or desired results
3. Indirect inputs (may not be given directly, you
have to calculate or assume)
1. Understanding the Problem
– Example: Calculate the area of a circle having
the radius of 3 cm
• Inputs:
– Radius=3
• Output:
– Area
• Indirect Inputs:
– Pi=3.14
– Area = 3.14 * (3*3) = 28.27
2. Plan the Logic
• Identify/Outline small steps in sequence, to
achieve the goal (or desired results)
• Tools such as flowcharts and pseudocode can be
used:
1. Flowchart: a pictorial representation of the logic
steps
2. Pseudocode: English-like representation of the logic
• Walk through the logic before coding
3. Code the Program
• Code the program:
–Select the programming language
–Write the program instructions in the selected
programming language
–Use the compiler software to translate the
program into machine understandable code or
executable file
–Syntax errors (Error in program instructions) are
identified by the compiler during compilation
and can be corrected.
4. Test the Program
• Testing the program
–Execute it with sample data and check the
results
–Identify logic errors if any (undesired results
or output) and correct them
–Choose test data carefully to exercise all
branches of the logic (Important)
5. Deploy the Program
• Putting the program into production
–Do this after testing is complete and all known
errors have been corrected
Introduction to Pseudocode
• One of the popular representation based on
natural language
• Widely used
–Easy to read and write
–Allow the programmer to concentrate on the
logic of the problem
• Structured in English language (Syntax/grammar)
What is Pseudocode (continued...)
• English like statements
• Each instruction is written on a separate line
• Keywords and indentation are used to signify
particular control structures.
• Written from top to bottom, with only one
entry and one exit
• Groups of statements may be formed into
modules
Some Basic Constructs
• Print  Output is to be sent to the Printer
• Write  Output is to be written to a file
• Display  Output is to be written to the screen
• Prompt  required before an input instruction
Get, causes the message to be sent to the screen
• Compute /Calculate  Calculation performed by
computer
• Calculation operations: +, -, *, /, ()
Some Basic Constructs
• Set  Used to set inital value,
E.g., Set Marks to 0
• =  Place values from right hand-side item to left
hand side
E.g., Marks = 67
• Save  Save the variable in file or disk
E.g., Save Marks
Comparison/Selection
IF student_Marks are above 50 THEN
Result = “Pass“
ELSE
Result = “Fail“
ENDIF 
• Making decision by comparing values
•
• Keywords used:
•IF, THEN, ELSE
IF student_Marks are above 50 THEN
Result = “Pass“
ELSE
Result = “Fail“
ENDIF
Comparison/Selection
CASE of <condition-variable>
Case <valueA>
Do Step1
Case <valueB>
Do Step2
Case <valueC>
Do Step3
ENDCASE
• Case Strtucture
 Multiple banching based on value of condition
• Three keywords used:
• CASE of, Case, ENDCASE
Comparison/Selection
• Case Strtucture
 Example...
Repeat a group of Statements
DOWHILE student_total < 50
Read student record
Print student name
Add 1 to student_total
ENDDO
• Repeating set of instruction base on some condition
• Keyword used:
•DOWHILE, ENDDO
Example Pseudocode Program
• A program is required to read three
numbers, add them together and print their
total.
Inputc Processing Output
Number1
Number2
Number3
total
Solution Algorithm
Add_three_numbers
Set total to 0
Read number1
Read number2
Read number3
Total = number1 + number2 + number3
Print total
END
Repeat Until (Loop)
• Repeat-Until statement (loop)
REPEAT
Do StepA
Do StepB
UNTIL conditionN is True
- What is the Difference between (While & Repeat) ?
Example:…
Flowcharts
• “A graphic representation of a sequence of
operations to represent a computer program”
• Flowcharts show the sequence of instructions in a
single program or subroutine.
A Flowchart
• A Flowchart
– Shows logic of an algorithm or problem
– Shows individual steps and their interconnections
– E.g., control flow from one action to the next
Name Symbol Description
Oval Beginning or End of the Program
Parallelogram Input / Output Operations
Rectangle Processing for example, Addition,
Multiplication, Division, etc.
Diamond
Denotes a Decision (or branching)
for example IF-Then-Else
Arrow
Denotes the Direction of logic
flow
Flowchart Symbols
Example 1
Step 1: Input M1,M2,M3,M4
Step 2: GRADE = (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
START
Input
M1,M2,M3,M4
GRADE(M1+M2+M3+M4)/4
IS
GRADE<50
STOP
YN
Print
“FAIL”
Print
“PASS”
Example 2
• Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Example 2
Algorithm
• Step 1: Read Lft
• Step 2: Lcm = Lft x 30
• Step 3: Print Lcm
Flowchart
START
Read Lft
Lcm = Lft x 30
STOP
Print LCM
Example 3
• Write an algorithm and draw a flowchart that will read
the two sides of a rectangle and calculate its area.
Example 3
Algorithm
• Step 1: Read W,L
• Step 2: A = L x W
• Step 3: Print A
START
Read
W, L
A  L x W
STOP
Print
A
DECISION STRUCTURES
• The expression A>B is a logical expression
• It describes a condition we want to test
• if A>B is true (if A is greater than B) we take the action
on left
• Print the value of A
• if A>B is false (if A is not greater than B) we take the
action on right
• Print the value of B
IF–THEN–ELSE STRUCTURE
• The algorithm for the flowchart is as follows:
If A>B then
print A
Else
print B
endif
is
A>B
Y N
Print A Print B
• Multiple branching based on a single condition
CASE STRUCTURE
CASE
Condition
Do Step A Do Step B Do Step C Do Step D
Relational / Logical Operators
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
 Greater than or equal to
 Less than or equal to
 Not equal to
Example 4
• Write an algorithm that reads two values, finds largest
value and then prints the largest value.
ALGORITHM
Step 1: Read VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX
-- DRAW the Flow Chart for the Program
Selection Structure
• A Selection structure can be based on
1. Dual-Alternative (two code paths)
2. Single Alternative (one code path)
is
A>B
Y N
Print A Print B
Dual Alternative Example 
Selection Structure
• Single Alternative Example
Pseudocode: IF GPA is greater than 2.0 Then
Print “Promoted ”
End IF
GPA
> 2.0
N Y
Print
“Promoted”
Loop Structure
• Repetition (WHILE structure)
– Repeats a set of actions based on the answer to a
question/condition
pseudocode: DoWHILE <Some-True-Condition>
Do Something
ENDDO
Loop Structure
• REPEAT-UNTIL structure
– Repeats a set of actions until a condition remains True
– pseudocode: REPEAT
Do-Something
UNTIL <Some True Condition>

Contenu connexe

Tendances

Introduction to Problem Solving Techniques- Python
Introduction to Problem Solving Techniques- PythonIntroduction to Problem Solving Techniques- Python
Introduction to Problem Solving Techniques- PythonPriyankaC44
 
Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityReggie Niccolo Santos
 
STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2Bro Shola Ajayi
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to ProgrammingChaffey College
 
Computer programming and utilization
Computer programming and utilizationComputer programming and utilization
Computer programming and utilizationDigvijaysinh Gohil
 
Logic Formulation 1
Logic Formulation 1Logic Formulation 1
Logic Formulation 1deathful
 
Unit 1 python (2021 r)
Unit 1 python (2021 r)Unit 1 python (2021 r)
Unit 1 python (2021 r)praveena p
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++Online
 
Problem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CProblem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CPrabu U
 
Ch0 computer systems overview
Ch0 computer systems overviewCh0 computer systems overview
Ch0 computer systems overviewAboubakarIbrahima
 
Problem solving (C++ Programming)
Problem solving (C++ Programming)Problem solving (C++ Programming)
Problem solving (C++ Programming)Umair Younas
 
Part II: Assembly Fundamentals
Part II: Assembly FundamentalsPart II: Assembly Fundamentals
Part II: Assembly FundamentalsAhmed M. Abed
 

Tendances (20)

Introduction to Problem Solving Techniques- Python
Introduction to Problem Solving Techniques- PythonIntroduction to Problem Solving Techniques- Python
Introduction to Problem Solving Techniques- Python
 
Unit 1 psp
Unit 1 pspUnit 1 psp
Unit 1 psp
 
Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State University
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Computer programming and utilization
Computer programming and utilizationComputer programming and utilization
Computer programming and utilization
 
algorithm
algorithmalgorithm
algorithm
 
Logic Formulation 1
Logic Formulation 1Logic Formulation 1
Logic Formulation 1
 
Unit 1 python (2021 r)
Unit 1 python (2021 r)Unit 1 python (2021 r)
Unit 1 python (2021 r)
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++
 
Problem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CProblem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to C
 
Basics of python
Basics of pythonBasics of python
Basics of python
 
Ch0 computer systems overview
Ch0 computer systems overviewCh0 computer systems overview
Ch0 computer systems overview
 
Flowcharts
FlowchartsFlowcharts
Flowcharts
 
Problem solving (C++ Programming)
Problem solving (C++ Programming)Problem solving (C++ Programming)
Problem solving (C++ Programming)
 
Part II: Assembly Fundamentals
Part II: Assembly FundamentalsPart II: Assembly Fundamentals
Part II: Assembly Fundamentals
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
L1
L1L1
L1
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 

Similaire à Cs1123 2 comp_prog

Programming requirements for beginning in software engineering.pptx
Programming requirements for beginning in software engineering.pptxProgramming requirements for beginning in software engineering.pptx
Programming requirements for beginning in software engineering.pptxTeddyDaka
 
Pj01 1-computer and programming fundamentals
Pj01 1-computer and programming fundamentalsPj01 1-computer and programming fundamentals
Pj01 1-computer and programming fundamentalsSasidharaRaoMarrapu
 
Introduction to computer programming
Introduction to computer programmingIntroduction to computer programming
Introduction to computer programmingSangheethaa Sukumaran
 
BCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.pptBCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.pptKirti Verma
 
algorithms and flow chart overview.pdf
algorithms and flow chart overview.pdfalgorithms and flow chart overview.pdf
algorithms and flow chart overview.pdfAmanPratik11
 
Lec-ProblemSolving.pptx
Lec-ProblemSolving.pptxLec-ProblemSolving.pptx
Lec-ProblemSolving.pptxmiansaad18
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing TechniquesAppili Vamsi Krishna
 
Problem-solving and design 1.pptx
Problem-solving and design 1.pptxProblem-solving and design 1.pptx
Problem-solving and design 1.pptxTadiwaMawere
 
Pseudo code.pptx
Pseudo code.pptxPseudo code.pptx
Pseudo code.pptxChaya64047
 
Computer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module iComputer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module iAjit Nayak
 
Computer Programming Computer Programming
Computer Programming Computer ProgrammingComputer Programming Computer Programming
Computer Programming Computer Programmingarifhasan88
 
AlgorithmAndFlowChart.pdf
AlgorithmAndFlowChart.pdfAlgorithmAndFlowChart.pdf
AlgorithmAndFlowChart.pdfSusieMaestre1
 

Similaire à Cs1123 2 comp_prog (20)

Programming requirements for beginning in software engineering.pptx
Programming requirements for beginning in software engineering.pptxProgramming requirements for beginning in software engineering.pptx
Programming requirements for beginning in software engineering.pptx
 
Pj01 1-computer and programming fundamentals
Pj01 1-computer and programming fundamentalsPj01 1-computer and programming fundamentals
Pj01 1-computer and programming fundamentals
 
Introduction to computer programming
Introduction to computer programmingIntroduction to computer programming
Introduction to computer programming
 
BCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.pptBCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.ppt
 
Algorithm.pdf
Algorithm.pdfAlgorithm.pdf
Algorithm.pdf
 
algorithms and flow chart overview.pdf
algorithms and flow chart overview.pdfalgorithms and flow chart overview.pdf
algorithms and flow chart overview.pdf
 
Programming logic &practices
Programming logic &practices Programming logic &practices
Programming logic &practices
 
Lec-ProblemSolving.pptx
Lec-ProblemSolving.pptxLec-ProblemSolving.pptx
Lec-ProblemSolving.pptx
 
Lecture1
Lecture1Lecture1
Lecture1
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
Problem-solving and design 1.pptx
Problem-solving and design 1.pptxProblem-solving and design 1.pptx
Problem-solving and design 1.pptx
 
Pseudo code.pptx
Pseudo code.pptxPseudo code.pptx
Pseudo code.pptx
 
UNIT-111.pptx
UNIT-111.pptxUNIT-111.pptx
UNIT-111.pptx
 
Computer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module iComputer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module i
 
01CHAP_1.PPT
01CHAP_1.PPT01CHAP_1.PPT
01CHAP_1.PPT
 
Computer Programming Computer Programming
Computer Programming Computer ProgrammingComputer Programming Computer Programming
Computer Programming Computer Programming
 
AlgorithmAndFlowChart.pdf
AlgorithmAndFlowChart.pdfAlgorithmAndFlowChart.pdf
AlgorithmAndFlowChart.pdf
 
Algorithms - Introduction to computer programming
Algorithms - Introduction to computer programmingAlgorithms - Introduction to computer programming
Algorithms - Introduction to computer programming
 
Cse
CseCse
Cse
 
PPS Unit-1.pdf
PPS Unit-1.pdfPPS Unit-1.pdf
PPS Unit-1.pdf
 

Plus de TAlha MAlik

Data file handling
Data file handlingData file handling
Data file handlingTAlha MAlik
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structuresTAlha MAlik
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointersTAlha MAlik
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operationsTAlha MAlik
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_ifTAlha MAlik
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constantsTAlha MAlik
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
Cs1123 9 strings
Cs1123 9 stringsCs1123 9 strings
Cs1123 9 stringsTAlha MAlik
 

Plus de TAlha MAlik (12)

Data file handling
Data file handlingData file handling
Data file handling
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointers
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
Cs1123 7 arrays
Cs1123 7 arraysCs1123 7 arrays
Cs1123 7 arrays
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constants
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Cs1123 1 intro
Cs1123 1 introCs1123 1 intro
Cs1123 1 intro
 
Cs1123 9 strings
Cs1123 9 stringsCs1123 9 strings
Cs1123 9 strings
 

Dernier

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 

Dernier (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Cs1123 2 comp_prog

  • 1. Introduction to Programming (CS1123) By Dr. Muhammad Aleem, Department of Computer Science, Mohammad Ali Jinnah University, Islamabad Fall 2013
  • 2. What is a Computer? • A computer is a electro-mechanical device that works semi-automatically to process input data according to the stored set of instructions and produces output or resultant data. A Computer System Instructions Data Results
  • 3. Components of a Computer System Main Memory CPU Arithmetic and Logic Unit Control Unit Computer Peripheral Devices / Connected devices Keyboard Mouse Display CD Rom Hard Disk
  • 4. Computer Instructions and Programs • Instruction: A computer instruction is a command or directive given to a computer to perform specific task. Examples: Add 2 and 5, Print “Hello World” • Program: A program is sequence of instructions written in programming language that directs a computer to solve a problem Examples: Draw a square, etc.
  • 5. Computer Instructions and Programs • Program “Draw a square” 1 – Draw a vertical line of length n inches 2 – Draw a horizontal line of n inches 3– Draw a vertical line of length n inches 4 – Draw a horizontal line of n inches
  • 6. Computer Software System Operating Systems (Windows, Linux, MAC, Solaris) Compilers / Libraries (C++, C, Java) Application Programs (.cpp, .c, .java,) Computer Hardware
  • 7. Programming Languages • Classification of programming languages: 1. Machine language 2. Low-level languages 3. High-level languages
  • 8. 1. Machine level languages • A computer understands only sequence of bits or 1’s and 0’s (the smallest piece of information) • A computer program can be written using machine languages (01001101010010010….) • Very fast execution • Very difficult to write and debug • Machine specific (different codes on different machines)
  • 9. 2. Low level languages • English encrypted words instead of codes (1’s and 0’s) • More understandable (for humans) • Example: Assembly language • Requires: “Translation” from Assembly code to Machine code compare: cmpl #oxa,n cgt end_of_loop acddl #0x1,n end_of_loop: 1001010101001101 1110010110010100 0101010111010010 0110100110111011 1101100101010101 Assembler Assembly Code Machine Code
  • 10. 3. High level languages • Mostly machine independent • Close to natural language (English like language keywords) • Easy to write and understand programs • Easy to debug and maintain code • Requires compilers to translate to machine code • Slower than low-level languages
  • 11. 3. High level languages • Some Popular High-Level languages – COBOL (COmmon Business Oriented Language) – FORTRAN (FORmula TRANslation) – BASIC (Beginner All-purpose Symbolic Instructional Code) – Pascal (named for Blaise Pascal) – Ada (named for Ada Lovelace) – C (whose developer designed B first) – Visual Basic (Basic-like visual language by Microsoft) – C++ (an object-oriented language, based on C) – Java
  • 12. Programming Paradigms • Programming paradigm is the fundamental style of computer programming 1. Imperative 2. Functional 3. Logical 4. Object Oriented
  • 13. Imperative Paradigm • Machine based modes (Sequence of steps required to compute) • Statements executed sequentially step-wise • Emphasis on How is to compute • How to obtain the required results or output • Example languages: C, Pascal, Ada, etc.
  • 15. Functional/Logical Paradigm • Specify what is to be computed using programming abstractions. • Responsibility of a programmer is to specify What is to compute • How to compute is implementation dependent and is managed by language compiler • Example languages: Lisp, Scheme, Erlang, Haskell, Scala etc.
  • 16. Object-Oriented Paradigm • Programming with Abstract Data Types, to model real world • Basic Program Unit: Class (a programmer defined type) – A entity that contains data and methods which work on the data • Basic Run-time Unit: Object – Instance of a class during execution
  • 17. Problem Solving Steps 1. Understand the problem 2. Plan the logic 3. Code the program 4. Test the program 5. Deploy the program into production
  • 18. 1. Understanding the Problem • Problems are often described in natural language like English. • Users may not be able to specify needs well, and the needs may changing frequently • Identify the requirements 1. Inputs or given data-items 2. Required output(s) or desired results 3. Indirect inputs (may not be given directly, you have to calculate or assume)
  • 19. 1. Understanding the Problem – Example: Calculate the area of a circle having the radius of 3 cm • Inputs: – Radius=3 • Output: – Area • Indirect Inputs: – Pi=3.14 – Area = 3.14 * (3*3) = 28.27
  • 20. 2. Plan the Logic • Identify/Outline small steps in sequence, to achieve the goal (or desired results) • Tools such as flowcharts and pseudocode can be used: 1. Flowchart: a pictorial representation of the logic steps 2. Pseudocode: English-like representation of the logic • Walk through the logic before coding
  • 21. 3. Code the Program • Code the program: –Select the programming language –Write the program instructions in the selected programming language –Use the compiler software to translate the program into machine understandable code or executable file –Syntax errors (Error in program instructions) are identified by the compiler during compilation and can be corrected.
  • 22. 4. Test the Program • Testing the program –Execute it with sample data and check the results –Identify logic errors if any (undesired results or output) and correct them –Choose test data carefully to exercise all branches of the logic (Important)
  • 23. 5. Deploy the Program • Putting the program into production –Do this after testing is complete and all known errors have been corrected
  • 24. Introduction to Pseudocode • One of the popular representation based on natural language • Widely used –Easy to read and write –Allow the programmer to concentrate on the logic of the problem • Structured in English language (Syntax/grammar)
  • 25. What is Pseudocode (continued...) • English like statements • Each instruction is written on a separate line • Keywords and indentation are used to signify particular control structures. • Written from top to bottom, with only one entry and one exit • Groups of statements may be formed into modules
  • 26. Some Basic Constructs • Print  Output is to be sent to the Printer • Write  Output is to be written to a file • Display  Output is to be written to the screen • Prompt  required before an input instruction Get, causes the message to be sent to the screen • Compute /Calculate  Calculation performed by computer • Calculation operations: +, -, *, /, ()
  • 27. Some Basic Constructs • Set  Used to set inital value, E.g., Set Marks to 0 • =  Place values from right hand-side item to left hand side E.g., Marks = 67 • Save  Save the variable in file or disk E.g., Save Marks
  • 28. Comparison/Selection IF student_Marks are above 50 THEN Result = “Pass“ ELSE Result = “Fail“ ENDIF  • Making decision by comparing values • • Keywords used: •IF, THEN, ELSE IF student_Marks are above 50 THEN Result = “Pass“ ELSE Result = “Fail“ ENDIF
  • 29. Comparison/Selection CASE of <condition-variable> Case <valueA> Do Step1 Case <valueB> Do Step2 Case <valueC> Do Step3 ENDCASE • Case Strtucture  Multiple banching based on value of condition • Three keywords used: • CASE of, Case, ENDCASE
  • 31. Repeat a group of Statements DOWHILE student_total < 50 Read student record Print student name Add 1 to student_total ENDDO • Repeating set of instruction base on some condition • Keyword used: •DOWHILE, ENDDO
  • 32. Example Pseudocode Program • A program is required to read three numbers, add them together and print their total. Inputc Processing Output Number1 Number2 Number3 total
  • 33. Solution Algorithm Add_three_numbers Set total to 0 Read number1 Read number2 Read number3 Total = number1 + number2 + number3 Print total END
  • 34. Repeat Until (Loop) • Repeat-Until statement (loop) REPEAT Do StepA Do StepB UNTIL conditionN is True - What is the Difference between (While & Repeat) ? Example:…
  • 35. Flowcharts • “A graphic representation of a sequence of operations to represent a computer program” • Flowcharts show the sequence of instructions in a single program or subroutine.
  • 36. A Flowchart • A Flowchart – Shows logic of an algorithm or problem – Shows individual steps and their interconnections – E.g., control flow from one action to the next
  • 37. Name Symbol Description Oval Beginning or End of the Program Parallelogram Input / Output Operations Rectangle Processing for example, Addition, Multiplication, Division, etc. Diamond Denotes a Decision (or branching) for example IF-Then-Else Arrow Denotes the Direction of logic flow Flowchart Symbols
  • 38. Example 1 Step 1: Input M1,M2,M3,M4 Step 2: GRADE = (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print “FAIL” else Print “PASS” endif START Input M1,M2,M3,M4 GRADE(M1+M2+M3+M4)/4 IS GRADE<50 STOP YN Print “FAIL” Print “PASS”
  • 39. Example 2 • Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
  • 40. Example 2 Algorithm • Step 1: Read Lft • Step 2: Lcm = Lft x 30 • Step 3: Print Lcm Flowchart START Read Lft Lcm = Lft x 30 STOP Print LCM
  • 41. Example 3 • Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.
  • 42. Example 3 Algorithm • Step 1: Read W,L • Step 2: A = L x W • Step 3: Print A START Read W, L A  L x W STOP Print A
  • 43. DECISION STRUCTURES • The expression A>B is a logical expression • It describes a condition we want to test • if A>B is true (if A is greater than B) we take the action on left • Print the value of A • if A>B is false (if A is not greater than B) we take the action on right • Print the value of B
  • 44. IF–THEN–ELSE STRUCTURE • The algorithm for the flowchart is as follows: If A>B then print A Else print B endif is A>B Y N Print A Print B
  • 45. • Multiple branching based on a single condition CASE STRUCTURE CASE Condition Do Step A Do Step B Do Step C Do Step D
  • 46. Relational / Logical Operators Relational Operators Operator Description > Greater than < Less than = Equal to  Greater than or equal to  Less than or equal to  Not equal to
  • 47. Example 4 • Write an algorithm that reads two values, finds largest value and then prints the largest value. ALGORITHM Step 1: Read VALUE1, VALUE2 Step 2: if (VALUE1 > VALUE2) then MAX  VALUE1 else MAX  VALUE2 endif Step 3: Print “The largest value is”, MAX -- DRAW the Flow Chart for the Program
  • 48. Selection Structure • A Selection structure can be based on 1. Dual-Alternative (two code paths) 2. Single Alternative (one code path) is A>B Y N Print A Print B Dual Alternative Example 
  • 49. Selection Structure • Single Alternative Example Pseudocode: IF GPA is greater than 2.0 Then Print “Promoted ” End IF GPA > 2.0 N Y Print “Promoted”
  • 50. Loop Structure • Repetition (WHILE structure) – Repeats a set of actions based on the answer to a question/condition pseudocode: DoWHILE <Some-True-Condition> Do Something ENDDO
  • 51. Loop Structure • REPEAT-UNTIL structure – Repeats a set of actions until a condition remains True – pseudocode: REPEAT Do-Something UNTIL <Some True Condition>