SlideShare a Scribd company logo
How to Program Damian Gordon
Ozymandias  I met a traveler from an antique land  Who said: Two vast and trunkless legs of stone  Stand in the desert . . . Near them, on the sand,  Half sunk, a shattered visage lies, whose frown,  And wrinkled lip, and sneer of cold command  Tell that its sculptor well those passions read  Which yet survive, stamped on these lifeless things,  The hand that mocked them, and the heart that fed.  And on the pedestal these words appear:  "My name is Ozymandias, king of kings:  Look on my works, ye Mighty, and despair!"  Nothing beside remains. Round the decay  Of that colossal wreck, boundless and bare  The lone and level sands stretch far away.
Characteristics of a System Input Output Proc1 Proc2 Proc3
Characteristics of a Computer System User Defined  Programs CPU Peripherals Networks Operating System  (O/S) Applications Software (S/W) Hardware (H/W) Computer System
ALGORITHMS
Algorithms An Algorithm is a series of instructions Examples of algorithms include Musical scores Knitting patterns  Row 1:  SL 1, K8, K2 tog, K1, turn Recipes
Algorithms In computers we typically design an algorithm before we convert it into a computer program, and execute it.
Algorithms Problem Solving Analyse Problem (Analysis) Develop Algorithm (Design) Convert into programming language (Development)
Example Making a Cup of Tea Utilities Cup, tea bag, kettle, water, power supply, sugar, user, milk, spoon/fork.
Example Steps Organise everything together Plug in kettle Put teabag in cup Put water into kettle Turn on kettle Wait for kettle to boil Add water to cup Remove teabag with spoon/fork Add milk and/or sugar Serve
Example : Step-wise Refinement  Step-wise refinement of step 4 (Put water into kettle) 4.1 Bring kettle to tap 4.2 Put kettle under water 4.3 Turn on tap 4.4 Wait for kettle to be full 4.5 Turn off tap
Control Statements
Example : Conditional Statement Conditional Statement so If  Sugar required  then  add sugar to tea endif If  <Condition>  {  <Statements> }
or Example : Conditional Statement If  <Condition>  {  <Statements1> }   else   {  <Statements2> }
Nested IF Statements If  <Condition>  then if  <Condition>  then <Statements> endif else if  <Condition> <Statements> endif endif
Program Correctness I Syntax : Rules which define a correct structure The cat sat on the mat (legal) Sat the on the cat mat (illegal)
Program Correctness II Semantics : Rules which attach meaning to symbols The mat sat on the cat (syntactically correct, but semantically incorrect) Choose a number 1 to 14, call this number N, write down the Nth month of the year (syntactically correct, but semantically incorrect)
Program Correctness III Logical : Correct & complete description of an operation Circumference = PI * radius Syntax = correct Semantics = correct Logically = incorrect Circumference =  PI * radius * radius
Iteration : WHILE Loop Consider filling the kettle WHILE  kettle not full DO  keep filling kettle ENDWHILE; So this will keep looping while the kettle is not full, as soon as it’s full, the loop finishes.
Iteration : WHILE Loop Consider the problem of searching for an entry in a phone book with only condition : Get first entry If this is the required entry  Then write down phone number Else get next entry If this is the correct entry then write done entry else get next entry if this is the correct entry …………… .
Iteration : WHILE Loop We may rewrite this as follows: get first entry; call this entry N; WHILE  N is NOT the required entry DO  Get next entry; call this entry N; ENDWHILE;
Iteration : WHILE Loop General Form of WHILE While  <Condition> { <Statements> }
Variables We can create a container to store values, we call these variables. A number variable can be declared as follows; int x; This means there a container created that is called ‘x’ and is of type ‘int’ (integer) If we wanted to declare a container for a single character it would be char y;
Variables To insert a value into a variable, we called this “assignment”, and we use the “=“ symbol, so x = 5; Which reads as “the integer variable x is assigned the value 5” For the character y y = “H” Which reads as “the character variable y is assigned the value H”
Increment If we do int x; x=5; To add one onto X, we can do the following; x = x + 1; Which means x gets a new value, which is the old value of x but with one added to it. We can also just do; int x = 5;
Check if number is prime How do we check if a number is prime ? If the number is X then it is prime if only 1 and X divide evenly into it. Or to restate, if the numbers 2 to X-1 do not divide evenly into X, then it is prime. Thus our program has to count from 2 to X-1 and divide that value by X, if the answer is a whole number X is not prime.
Check if number is prime N = 2; WHILE Prime > (N – 1) { IF (Prime % N eq 0){   Print “The Number is Not Prime” } N = N +1; }
Features of Algorithms Sequence Statement 1 Statement 2 Statement 3 Selection if (condition) { seq-of-stmts; } Iteration while (condition) { seq-of-stmts; }
EXAMPLES OF ALGORITHMS
Double Number
Double Number Program DoubleNumber: { get number; print number * 2; }
Double Number (Alternative) Program DoubleNumber: { get number; answer = number * 2; print answer;  }
Triple Number
Triple Number Program TripleNumber: { get number; answer = number * 3; print answer; }
Check for Odd/Even
Check for Odd/Even Program IsOddOrEven: { get number; IF(number /2 gives no remainder) THEN print “It’s Even”; ELSE print “It’s Odd”; EndIf; }
Count Next Ten
Count Next Ten Program CountToTen: { get number; StopNumber = number + 10; WHILE (StopNumber > Number) DO print number; number = number + 1; ENDWHILE; print “I’m Finished”; }
Sum Numbers 1 to 10
Sum Numbers 1 to 10 Program SumNos1to10: { Total = 0; Number = 1; WHILE (10 > Number) DO Total = Total + Number; Number = Number + 1; ENDWHILE; print Total; }
Check if Number is Prime
Check if number is prime How do we check if a number is prime ? If the number is X then it is prime if only 1 and X divide evenly into it. Or to restate, if the numbers 2 to X-1 do not divide evenly into X, then it is prime. Thus our program has to count from 2 to X-1 and divide that value by X, if the answer is a whole number X is not prime.
Check if number is prime N = 2; WHILE Prime > (N – 1) {   IF (Prime % N eq 0) { Print “The Number is Not Prime”; } N = N +1; }
JAVA Keywords
JAVA Keywords abstract double int strictfp  boolean else interface super break extends long switch byte final native synchronized case finally new this catch float package throw char for private throws class goto  protected transient  const  if public try continue implements return void default import short volatile do instanceof static while
Variables According to the syntax rules of Java, a name is a sequences of one or more characters. It must begin with a letter and must consist entirely of letters, digits, and the underscore character '_'. For example, here are some legal names: N  n  rate  x15  quite_a_long_name  HelloWorld Uppercase and lowercase letters are considered to be different, so that  HelloWorld, helloworld, HELLOWORLD, and hElloWorLD are all distinct names. Certain names are reserved for special uses in Java, and cannot be used by the programmer for other purposes. These are the  reserved words .
Assignment (I) In Java, the  only  way to get data into a variable -- that is, into the box that the variable names -- is with an  assignment statement . An assignment statement takes the form: variable  =  expression ; where  expression  represents anything that refers to or computes a data value. When the computer comes to an assignment statement in the course of executing a program, it evaluates the expression and puts the resulting data value into the variable. For example, consider the simple assignment statement rate = 0.07;
Assignment (II) The  variable  in this assignment statement is  rate, and the  expression  is the number 0.07. The computer executes this assignment statement by putting the number 0.07 in the variable rate, replacing whatever was there before. Now, consider the following more complicated assignment statement, which might come later in the same program: interest = rate * principal;
Declaring Variables (I) A variable can be used in a program only if it has first been  declared . A  variable declaration statement  is used to declare one or more variables and to give them names. When the computer executes a variable declaration, it sets aside memory for the variable and associates the variable's name with that memory. A simple variable declaration takes the form: type-name   variable-name-or-names ; The  variable-name-or-names  can be a single variable name or a list of variable names separated by commas. (We'll see later that variable declaration statements can actually be somewhat more complicated than this.)
Declaring Variables (II) Good programming style is to declare only one variable in a declaration statement, unless the variables are closely related in some way. For example: int numberOfStudents; String name; double x, y; boolean isFinished; char firstInitial, middleInitial, lastInitial;
Java Virtual Machine
Hello World Program public class HelloWorld { // A program to display the message // &quot;Hello World!&quot; on standard output public static void main(String[] args) { System.out.println(&quot;Hello World!&quot;); } }  // end of class HelloWorld
General Class Declaration  public class  program-name  { optional-variable-declarations-and-subroutines public static void main(String[] args) { statements } optional-variable-declarations-and-subroutines }
Bubblesort
Bubblesort Sorting lists of data is an important thing to be able to do
Array A list of variables is called an array. Instead of; int x1=0; int x2=0; int x3=0; int x4=0; We can do; int x[4]; And then we can access the elements as follows; x[0]=0; x[1]=0; x[2]=0; x[3]=0;
Comments To add in a few text comments into a program, to help the reader understand it, enclose the comment in “/*” and “*/”
Swapping values If x =5 and y=8, and we want to swap the two values, this will NOT work; x = y; y = x; Because if we look at these statements, what we are saying one step at a time Before : x =5 and y=8 x = y Now: x = 8 and y = 8 y = x; So same thing; x = 8 and y = 8
Swapping values So what we need to do is add another variable Z; Now if x =5 and y=8,  z = x; x = y; y = z; Because if we look at these statements, what we are saying one step at a time Before : x =5 and y=8 and z =0 z = x Now: x = 5 and y = 8 and z=5 x = y; Now: x = 8 and y = 8 and z=5 y = z; Now: x = 8 and y = 5 and z=5
 
Modularisation
Modularisation If we review the Bubblesort program, it is clear that there are two main processes occurring in the program ; Sort Array Print out solution
 
 
Bubblesort - Modularised main( ) { sort_array; print_out_solution; }
List of Variables
Which variables are owned by which module? myarray[]  is shared by everyone; outercount ,  innercount  and  swap_variable  are used by sort_array a new count variable should be used to print out variable values.
Bubblesort - Methods
 
 
Factorial
Find the index where a value X is stored
Find the index where a value X is stored Assume vector is called A and is of length 10. FIND-X-IN-A i = 0 While A(i) not = X and i not =10 i = i + 1 End-While If i not = 10 Print X, &quot; resides in vector element number &quot;, i else Print X, &quot; is not resident in vector A&quot; End-if END
Searching a sorted vector Search sorted vector A of length n for value x. Process Find the middle of the vector. If the value x is greater than the middle element, search the top half of the vector. If the value x is less than the middle element,  search the bottom half of the vector. If the value x is equal to the middle element, the value x has been found. If the bottom of the vector = the top of the vector the element is not there.
Constructs Subroutine   which searches the vector, given the vector name,  the vector top and bottom boundaries and the value being sought. In the subroutine: Test for empty vector If vector is not empty: when middle value = sought value, value is found when middle value < sought value, search upper half when middle value > sought value, search lower half Call the subroutine.  Position = BINARY-SEARCH(P, Q, A, X)
BINARY-SEARCH (P, Q, A, X) if P > Q  position = 0   else middle = (P+Q)/2 Case TRUE of X < A(middle): position =BINARY-SEARCH(P, middle - 1, A, X) X > A(middle):  position =BINARY-SEARCH(middle + 1, Q, A, X) X = A(middle): position = middle end-case   end-if END
/* program to take in an array and an element value and find the element in the array */ #define N 10 #include <stdio.h> main () { int  binary_search(int,int,int [],int); int a[N]= {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; int pos, x, start, end; printf (&quot;\nEnter the number you are looking for:&quot;); scanf (&quot;%d&quot;,&x); start = 0; end = N-1; pos = binary_search(start, end, a, x); if (pos >= 0) printf (&quot;\nThe value %d is in position %d&quot;,x,pos); else printf (&quot;\nThe value %d is not in the array&quot;,x); return 0; }

More Related Content

What's hot (20)

PPTX
CalStatementsNAV2013
Damith Shan Abeywickrema
 
PPTX
Conditional statement c++
amber chaudary
 
PDF
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
Mark Simon
 
PDF
nuts and bolts of c++
guestfb6ada
 
PPT
Decision Making and Branching in C
RAJ KUMAR
 
PPTX
Selection statements
Harsh Dabas
 
PPT
Mesics lecture 6 control statement = if -else if__else
eShikshak
 
PPTX
C decision making and looping.
Haard Shah
 
PDF
1584503386 1st chap
thuhiendtk4
 
PPT
Decision making and branching
Hossain Md Shakhawat
 
PDF
Loops and conditional statements
Saad Sheikh
 
PPTX
Decision making statements in C programming
Rabin BK
 
PPTX
Flow of control C ++ By TANUJ
TANUJ ⠀
 
PPTX
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
PPT
Branching in C
Prabhu Govind
 
PPT
C language control statements
suman Aggarwal
 
PPSX
Conditional statement
Maxie Santos
 
PDF
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
PPTX
Conditional statements
University of Potsdam
 
PPT
Control statements and functions in c
vampugani
 
CalStatementsNAV2013
Damith Shan Abeywickrema
 
Conditional statement c++
amber chaudary
 
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
Mark Simon
 
nuts and bolts of c++
guestfb6ada
 
Decision Making and Branching in C
RAJ KUMAR
 
Selection statements
Harsh Dabas
 
Mesics lecture 6 control statement = if -else if__else
eShikshak
 
C decision making and looping.
Haard Shah
 
1584503386 1st chap
thuhiendtk4
 
Decision making and branching
Hossain Md Shakhawat
 
Loops and conditional statements
Saad Sheikh
 
Decision making statements in C programming
Rabin BK
 
Flow of control C ++ By TANUJ
TANUJ ⠀
 
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
Branching in C
Prabhu Govind
 
C language control statements
suman Aggarwal
 
Conditional statement
Maxie Santos
 
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Conditional statements
University of Potsdam
 
Control statements and functions in c
vampugani
 

Viewers also liked (20)

PPTX
Python: The Iterator Pattern
Damian T. Gordon
 
PPTX
Python: Common Design Patterns
Damian T. Gordon
 
PPTX
Introduction to Python programming
Damian T. Gordon
 
PPTX
Python: Migrating from Procedural to Object-Oriented Programming
Damian T. Gordon
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
Prototyping and Wireframing tools
Damian T. Gordon
 
PPTX
Python: Basic Inheritance
Damian T. Gordon
 
PPTX
Python: Object-Oriented Testing (Unit Testing)
Damian T. Gordon
 
PPTX
Python: Object-oriented Testing
Damian T. Gordon
 
PPTX
Python: Multiple Inheritance
Damian T. Gordon
 
PPTX
Operating Systems: Virtual Memory
Damian T. Gordon
 
PPTX
Operating Systems: Memory Management
Damian T. Gordon
 
PDF
App Mapping - a selection of free applications to support first year students
Trevor Boland
 
PDF
Corso Javascript
Giuseppe Dell'Abate
 
PPT
Narrative ppnt minus trailers
abcdsmile
 
PPT
Evaluation Q4- Use of Technology
abcdsmile
 
PPTX
Usability, Accessibility, and Design Evaluation
Damian T. Gordon
 
PPT
Learning Styles for Virtual Learning Environments
Damian T. Gordon
 
PPTX
Creative Commons Sites
Damian T. Gordon
 
PPT
Retention Of Students Enrolled
Damian T. Gordon
 
Python: The Iterator Pattern
Damian T. Gordon
 
Python: Common Design Patterns
Damian T. Gordon
 
Introduction to Python programming
Damian T. Gordon
 
Python: Migrating from Procedural to Object-Oriented Programming
Damian T. Gordon
 
Python: Modules and Packages
Damian T. Gordon
 
Prototyping and Wireframing tools
Damian T. Gordon
 
Python: Basic Inheritance
Damian T. Gordon
 
Python: Object-Oriented Testing (Unit Testing)
Damian T. Gordon
 
Python: Object-oriented Testing
Damian T. Gordon
 
Python: Multiple Inheritance
Damian T. Gordon
 
Operating Systems: Virtual Memory
Damian T. Gordon
 
Operating Systems: Memory Management
Damian T. Gordon
 
App Mapping - a selection of free applications to support first year students
Trevor Boland
 
Corso Javascript
Giuseppe Dell'Abate
 
Narrative ppnt minus trailers
abcdsmile
 
Evaluation Q4- Use of Technology
abcdsmile
 
Usability, Accessibility, and Design Evaluation
Damian T. Gordon
 
Learning Styles for Virtual Learning Environments
Damian T. Gordon
 
Creative Commons Sites
Damian T. Gordon
 
Retention Of Students Enrolled
Damian T. Gordon
 
Ad

Similar to How to Program (20)

PPTX
Pseudocode
Harsha Madushanka
 
PPSX
Algorithms, Structure Charts, Corrective and adaptive.ppsx
DaniyalManzoor3
 
PPTX
Java introduction
Samsung Electronics Egypt
 
PPT
Unit I Advanced Java Programming Course
parveen837153
 
PPT
Chapter 5( programming) answer
smkengkilili2011
 
PDF
Lecture1.pdf
SakhilejasonMsibi
 
PPT
Session 1
pham vu
 
PPT
Session 1
pham vu
 
PPT
Unit1 C
arnold 7490
 
PPT
Unit1 C
arnold 7490
 
PPT
lec_4_data_structures_and_algorithm_analysis.ppt
SourabhPal46
 
PPT
lec_4_data_structures_and_algorithm_analysis.ppt
Mard Geer
 
PPT
Comp102 lec 4
Fraz Bakhsh
 
PPTX
JAVA programming language made easy.pptx
Sunila31
 
PPT
Data Structures- Part1 overview and review
Abdullah Al-hazmy
 
PPT
Programming Fundamentals - Lecture 1.ppt
FarhanGhafoor7
 
PDF
Cp module 2
Amarjith C K
 
PPT
Cobol basics 19-6-2010
SivaprasanthRentala1975
 
PPTX
Java fundamentals
HCMUTE
 
PPTX
Problem Solving PPT Slides Grade 10- 11students
chamm5
 
Pseudocode
Harsha Madushanka
 
Algorithms, Structure Charts, Corrective and adaptive.ppsx
DaniyalManzoor3
 
Java introduction
Samsung Electronics Egypt
 
Unit I Advanced Java Programming Course
parveen837153
 
Chapter 5( programming) answer
smkengkilili2011
 
Lecture1.pdf
SakhilejasonMsibi
 
Session 1
pham vu
 
Session 1
pham vu
 
Unit1 C
arnold 7490
 
Unit1 C
arnold 7490
 
lec_4_data_structures_and_algorithm_analysis.ppt
SourabhPal46
 
lec_4_data_structures_and_algorithm_analysis.ppt
Mard Geer
 
Comp102 lec 4
Fraz Bakhsh
 
JAVA programming language made easy.pptx
Sunila31
 
Data Structures- Part1 overview and review
Abdullah Al-hazmy
 
Programming Fundamentals - Lecture 1.ppt
FarhanGhafoor7
 
Cp module 2
Amarjith C K
 
Cobol basics 19-6-2010
SivaprasanthRentala1975
 
Java fundamentals
HCMUTE
 
Problem Solving PPT Slides Grade 10- 11students
chamm5
 
Ad

More from Damian T. Gordon (20)

PPTX
Introduction to Prompts and Prompt Engineering
Damian T. Gordon
 
PPTX
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
PPTX
TRIZ: Theory of Inventive Problem Solving
Damian T. Gordon
 
PPTX
Some Ethical Considerations of AI and GenAI
Damian T. Gordon
 
PPTX
Some Common Errors that Generative AI Produces
Damian T. Gordon
 
PPTX
The Use of Data and Datasets in Data Science
Damian T. Gordon
 
PPTX
A History of Different Versions of Microsoft Windows
Damian T. Gordon
 
PPTX
Writing an Abstract: A Question-based Approach
Damian T. Gordon
 
PPTX
Using GenAI for Universal Design for Learning
Damian T. Gordon
 
DOC
A CheckSheet for Inclusive Software Design
Damian T. Gordon
 
PPTX
A History of Versions of the Apple MacOS
Damian T. Gordon
 
PPTX
68 Ways that Data Science and AI can help address the UN Sustainability Goals
Damian T. Gordon
 
PPTX
Copyright and Creative Commons Considerations
Damian T. Gordon
 
PPTX
Exam Preparation: Some Ideas and Suggestions
Damian T. Gordon
 
PPTX
Studying and Notetaking: Some Suggestions
Damian T. Gordon
 
PPTX
The Growth Mindset: Explanations and Activities
Damian T. Gordon
 
PPTX
Hyperparameter Tuning in Neural Networks
Damian T. Gordon
 
PPTX
Early 20th Century Modern Art: Movements and Artists
Damian T. Gordon
 
PPTX
An Introduction to Generative Artificial Intelligence
Damian T. Gordon
 
PPTX
An Introduction to Green Computing with a fun quiz.
Damian T. Gordon
 
Introduction to Prompts and Prompt Engineering
Damian T. Gordon
 
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
TRIZ: Theory of Inventive Problem Solving
Damian T. Gordon
 
Some Ethical Considerations of AI and GenAI
Damian T. Gordon
 
Some Common Errors that Generative AI Produces
Damian T. Gordon
 
The Use of Data and Datasets in Data Science
Damian T. Gordon
 
A History of Different Versions of Microsoft Windows
Damian T. Gordon
 
Writing an Abstract: A Question-based Approach
Damian T. Gordon
 
Using GenAI for Universal Design for Learning
Damian T. Gordon
 
A CheckSheet for Inclusive Software Design
Damian T. Gordon
 
A History of Versions of the Apple MacOS
Damian T. Gordon
 
68 Ways that Data Science and AI can help address the UN Sustainability Goals
Damian T. Gordon
 
Copyright and Creative Commons Considerations
Damian T. Gordon
 
Exam Preparation: Some Ideas and Suggestions
Damian T. Gordon
 
Studying and Notetaking: Some Suggestions
Damian T. Gordon
 
The Growth Mindset: Explanations and Activities
Damian T. Gordon
 
Hyperparameter Tuning in Neural Networks
Damian T. Gordon
 
Early 20th Century Modern Art: Movements and Artists
Damian T. Gordon
 
An Introduction to Generative Artificial Intelligence
Damian T. Gordon
 
An Introduction to Green Computing with a fun quiz.
Damian T. Gordon
 

Recently uploaded (20)

PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 

How to Program

  • 1. How to Program Damian Gordon
  • 2. Ozymandias I met a traveler from an antique land Who said: Two vast and trunkless legs of stone Stand in the desert . . . Near them, on the sand, Half sunk, a shattered visage lies, whose frown, And wrinkled lip, and sneer of cold command Tell that its sculptor well those passions read Which yet survive, stamped on these lifeless things, The hand that mocked them, and the heart that fed. And on the pedestal these words appear: &quot;My name is Ozymandias, king of kings: Look on my works, ye Mighty, and despair!&quot; Nothing beside remains. Round the decay Of that colossal wreck, boundless and bare The lone and level sands stretch far away.
  • 3. Characteristics of a System Input Output Proc1 Proc2 Proc3
  • 4. Characteristics of a Computer System User Defined Programs CPU Peripherals Networks Operating System (O/S) Applications Software (S/W) Hardware (H/W) Computer System
  • 6. Algorithms An Algorithm is a series of instructions Examples of algorithms include Musical scores Knitting patterns Row 1: SL 1, K8, K2 tog, K1, turn Recipes
  • 7. Algorithms In computers we typically design an algorithm before we convert it into a computer program, and execute it.
  • 8. Algorithms Problem Solving Analyse Problem (Analysis) Develop Algorithm (Design) Convert into programming language (Development)
  • 9. Example Making a Cup of Tea Utilities Cup, tea bag, kettle, water, power supply, sugar, user, milk, spoon/fork.
  • 10. Example Steps Organise everything together Plug in kettle Put teabag in cup Put water into kettle Turn on kettle Wait for kettle to boil Add water to cup Remove teabag with spoon/fork Add milk and/or sugar Serve
  • 11. Example : Step-wise Refinement Step-wise refinement of step 4 (Put water into kettle) 4.1 Bring kettle to tap 4.2 Put kettle under water 4.3 Turn on tap 4.4 Wait for kettle to be full 4.5 Turn off tap
  • 13. Example : Conditional Statement Conditional Statement so If Sugar required then add sugar to tea endif If <Condition> { <Statements> }
  • 14. or Example : Conditional Statement If <Condition> { <Statements1> } else { <Statements2> }
  • 15. Nested IF Statements If <Condition> then if <Condition> then <Statements> endif else if <Condition> <Statements> endif endif
  • 16. Program Correctness I Syntax : Rules which define a correct structure The cat sat on the mat (legal) Sat the on the cat mat (illegal)
  • 17. Program Correctness II Semantics : Rules which attach meaning to symbols The mat sat on the cat (syntactically correct, but semantically incorrect) Choose a number 1 to 14, call this number N, write down the Nth month of the year (syntactically correct, but semantically incorrect)
  • 18. Program Correctness III Logical : Correct & complete description of an operation Circumference = PI * radius Syntax = correct Semantics = correct Logically = incorrect Circumference = PI * radius * radius
  • 19. Iteration : WHILE Loop Consider filling the kettle WHILE kettle not full DO keep filling kettle ENDWHILE; So this will keep looping while the kettle is not full, as soon as it’s full, the loop finishes.
  • 20. Iteration : WHILE Loop Consider the problem of searching for an entry in a phone book with only condition : Get first entry If this is the required entry Then write down phone number Else get next entry If this is the correct entry then write done entry else get next entry if this is the correct entry …………… .
  • 21. Iteration : WHILE Loop We may rewrite this as follows: get first entry; call this entry N; WHILE N is NOT the required entry DO Get next entry; call this entry N; ENDWHILE;
  • 22. Iteration : WHILE Loop General Form of WHILE While <Condition> { <Statements> }
  • 23. Variables We can create a container to store values, we call these variables. A number variable can be declared as follows; int x; This means there a container created that is called ‘x’ and is of type ‘int’ (integer) If we wanted to declare a container for a single character it would be char y;
  • 24. Variables To insert a value into a variable, we called this “assignment”, and we use the “=“ symbol, so x = 5; Which reads as “the integer variable x is assigned the value 5” For the character y y = “H” Which reads as “the character variable y is assigned the value H”
  • 25. Increment If we do int x; x=5; To add one onto X, we can do the following; x = x + 1; Which means x gets a new value, which is the old value of x but with one added to it. We can also just do; int x = 5;
  • 26. Check if number is prime How do we check if a number is prime ? If the number is X then it is prime if only 1 and X divide evenly into it. Or to restate, if the numbers 2 to X-1 do not divide evenly into X, then it is prime. Thus our program has to count from 2 to X-1 and divide that value by X, if the answer is a whole number X is not prime.
  • 27. Check if number is prime N = 2; WHILE Prime > (N – 1) { IF (Prime % N eq 0){ Print “The Number is Not Prime” } N = N +1; }
  • 28. Features of Algorithms Sequence Statement 1 Statement 2 Statement 3 Selection if (condition) { seq-of-stmts; } Iteration while (condition) { seq-of-stmts; }
  • 31. Double Number Program DoubleNumber: { get number; print number * 2; }
  • 32. Double Number (Alternative) Program DoubleNumber: { get number; answer = number * 2; print answer; }
  • 34. Triple Number Program TripleNumber: { get number; answer = number * 3; print answer; }
  • 36. Check for Odd/Even Program IsOddOrEven: { get number; IF(number /2 gives no remainder) THEN print “It’s Even”; ELSE print “It’s Odd”; EndIf; }
  • 38. Count Next Ten Program CountToTen: { get number; StopNumber = number + 10; WHILE (StopNumber > Number) DO print number; number = number + 1; ENDWHILE; print “I’m Finished”; }
  • 39. Sum Numbers 1 to 10
  • 40. Sum Numbers 1 to 10 Program SumNos1to10: { Total = 0; Number = 1; WHILE (10 > Number) DO Total = Total + Number; Number = Number + 1; ENDWHILE; print Total; }
  • 41. Check if Number is Prime
  • 42. Check if number is prime How do we check if a number is prime ? If the number is X then it is prime if only 1 and X divide evenly into it. Or to restate, if the numbers 2 to X-1 do not divide evenly into X, then it is prime. Thus our program has to count from 2 to X-1 and divide that value by X, if the answer is a whole number X is not prime.
  • 43. Check if number is prime N = 2; WHILE Prime > (N – 1) { IF (Prime % N eq 0) { Print “The Number is Not Prime”; } N = N +1; }
  • 45. JAVA Keywords abstract double int strictfp boolean else interface super break extends long switch byte final native synchronized case finally new this catch float package throw char for private throws class goto protected transient const if public try continue implements return void default import short volatile do instanceof static while
  • 46. Variables According to the syntax rules of Java, a name is a sequences of one or more characters. It must begin with a letter and must consist entirely of letters, digits, and the underscore character '_'. For example, here are some legal names: N n rate x15 quite_a_long_name HelloWorld Uppercase and lowercase letters are considered to be different, so that HelloWorld, helloworld, HELLOWORLD, and hElloWorLD are all distinct names. Certain names are reserved for special uses in Java, and cannot be used by the programmer for other purposes. These are the reserved words .
  • 47. Assignment (I) In Java, the only way to get data into a variable -- that is, into the box that the variable names -- is with an assignment statement . An assignment statement takes the form: variable = expression ; where expression represents anything that refers to or computes a data value. When the computer comes to an assignment statement in the course of executing a program, it evaluates the expression and puts the resulting data value into the variable. For example, consider the simple assignment statement rate = 0.07;
  • 48. Assignment (II) The variable in this assignment statement is rate, and the expression is the number 0.07. The computer executes this assignment statement by putting the number 0.07 in the variable rate, replacing whatever was there before. Now, consider the following more complicated assignment statement, which might come later in the same program: interest = rate * principal;
  • 49. Declaring Variables (I) A variable can be used in a program only if it has first been declared . A variable declaration statement is used to declare one or more variables and to give them names. When the computer executes a variable declaration, it sets aside memory for the variable and associates the variable's name with that memory. A simple variable declaration takes the form: type-name variable-name-or-names ; The variable-name-or-names can be a single variable name or a list of variable names separated by commas. (We'll see later that variable declaration statements can actually be somewhat more complicated than this.)
  • 50. Declaring Variables (II) Good programming style is to declare only one variable in a declaration statement, unless the variables are closely related in some way. For example: int numberOfStudents; String name; double x, y; boolean isFinished; char firstInitial, middleInitial, lastInitial;
  • 52. Hello World Program public class HelloWorld { // A program to display the message // &quot;Hello World!&quot; on standard output public static void main(String[] args) { System.out.println(&quot;Hello World!&quot;); } } // end of class HelloWorld
  • 53. General Class Declaration public class program-name { optional-variable-declarations-and-subroutines public static void main(String[] args) { statements } optional-variable-declarations-and-subroutines }
  • 55. Bubblesort Sorting lists of data is an important thing to be able to do
  • 56. Array A list of variables is called an array. Instead of; int x1=0; int x2=0; int x3=0; int x4=0; We can do; int x[4]; And then we can access the elements as follows; x[0]=0; x[1]=0; x[2]=0; x[3]=0;
  • 57. Comments To add in a few text comments into a program, to help the reader understand it, enclose the comment in “/*” and “*/”
  • 58. Swapping values If x =5 and y=8, and we want to swap the two values, this will NOT work; x = y; y = x; Because if we look at these statements, what we are saying one step at a time Before : x =5 and y=8 x = y Now: x = 8 and y = 8 y = x; So same thing; x = 8 and y = 8
  • 59. Swapping values So what we need to do is add another variable Z; Now if x =5 and y=8, z = x; x = y; y = z; Because if we look at these statements, what we are saying one step at a time Before : x =5 and y=8 and z =0 z = x Now: x = 5 and y = 8 and z=5 x = y; Now: x = 8 and y = 8 and z=5 y = z; Now: x = 8 and y = 5 and z=5
  • 60.  
  • 62. Modularisation If we review the Bubblesort program, it is clear that there are two main processes occurring in the program ; Sort Array Print out solution
  • 63.  
  • 64.  
  • 65. Bubblesort - Modularised main( ) { sort_array; print_out_solution; }
  • 67. Which variables are owned by which module? myarray[] is shared by everyone; outercount , innercount and swap_variable are used by sort_array a new count variable should be used to print out variable values.
  • 69.  
  • 70.  
  • 72. Find the index where a value X is stored
  • 73. Find the index where a value X is stored Assume vector is called A and is of length 10. FIND-X-IN-A i = 0 While A(i) not = X and i not =10 i = i + 1 End-While If i not = 10 Print X, &quot; resides in vector element number &quot;, i else Print X, &quot; is not resident in vector A&quot; End-if END
  • 74. Searching a sorted vector Search sorted vector A of length n for value x. Process Find the middle of the vector. If the value x is greater than the middle element, search the top half of the vector. If the value x is less than the middle element, search the bottom half of the vector. If the value x is equal to the middle element, the value x has been found. If the bottom of the vector = the top of the vector the element is not there.
  • 75. Constructs Subroutine which searches the vector, given the vector name, the vector top and bottom boundaries and the value being sought. In the subroutine: Test for empty vector If vector is not empty: when middle value = sought value, value is found when middle value < sought value, search upper half when middle value > sought value, search lower half Call the subroutine. Position = BINARY-SEARCH(P, Q, A, X)
  • 76. BINARY-SEARCH (P, Q, A, X) if P > Q position = 0 else middle = (P+Q)/2 Case TRUE of X < A(middle): position =BINARY-SEARCH(P, middle - 1, A, X) X > A(middle): position =BINARY-SEARCH(middle + 1, Q, A, X) X = A(middle): position = middle end-case end-if END
  • 77. /* program to take in an array and an element value and find the element in the array */ #define N 10 #include <stdio.h> main () { int binary_search(int,int,int [],int); int a[N]= {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; int pos, x, start, end; printf (&quot;\nEnter the number you are looking for:&quot;); scanf (&quot;%d&quot;,&x); start = 0; end = N-1; pos = binary_search(start, end, a, x); if (pos >= 0) printf (&quot;\nThe value %d is in position %d&quot;,x,pos); else printf (&quot;\nThe value %d is not in the array&quot;,x); return 0; }