52
COMP 102: PROGRAMMING I SPRING 2012 1 Material is based on *Programming Logic and Design, Fifth Edition, Comprehensive Chapter 1 * Introduction to Programming in C++: Algorithms,Flowcharts and Pseudocode by Brent Daviduck

Comp102 lec 1

Embed Size (px)

Citation preview

Page 1: Comp102   lec 1

COMP 102: PROGRAMMING ISPRING 2012

1

Material is based on *Programming Logic and Design, Fifth Edition, Comprehensive Chapter 1 * Introduction to Programming in C++: Algorithms,Flowcharts and Pseudocode by Brent Daviduck

Page 2: Comp102   lec 1

Course OutlineInstructor Information:Name Ms. Saira [email protected]@hotmail.comOffice S – 211Office HoursMonday, Wednesday 10:00 – 11:00Tuesday, Thursday 11:00 – 12:00Class Meet Tuesday, Thursday 9:00 – 10:50 AMCategory•Core course for all Computing Majors•Recommended General Education Course for students with some enthusiasm to learn programming and who want to feel real flavor of writing a programPre- Requisite NilWebsite https://sites.google.com/site/comp102seca/

Page 3: Comp102   lec 1

Course Outline

Text Book Java in Two Semesters by Quentin Charatan and Aaron Kans Java 6 Illuminated Second Edition By Julie Anderson, Herve Franceschi

Reference Books Art and Science of Java by Eric Roberts Thinking in Java, by Bruce Eckel Java, The Complete Reference, by Patrick Naughton, Herbert Schildt Java and Object Orientation: An introduction, John Hunt The Java Handbook , by Patrick Naughton, Michael Morrison Java in a Nutshell: A Desktop Quick Reference for Java Programmers , by David

Flanagan The Java Language Specification , by James Gosling , Bill Joy, Guy Steele Who's Afraid of Java?, by Steve Heller

Page 4: Comp102   lec 1

Course Outline

Grading:

•REGRADING CAN BE REQUESTED WITHIN FOLLOWING TIME LIMITS:QUIZZES AND HOMEWORKS: 2DAYS, EXAMS: 3DAYS

Lab Quiz (Consider them exams)

45%

Assignments and Quiz 7%Project 7%Group Quiz 1%Mid (Out of class) 20%Final 20%Total 100.00%

Page 5: Comp102   lec 1

Course Outline

Attendance and Quiz Policy: •Attendance in class is mandatory.•Inadequate attendance (less than 85%) may lead to disqualification from the Midterm and/or Final Exams.•Take attendance on random days.•There could be both announced and un-announced quizzes•Deadlines are hard deadlines.

Page 6: Comp102   lec 1

Course Outline

Academic Honest: •What is plagiarism?

“Plagiarism is defined as presenting someone else’s work as your own. Work

means any intellectual output, and typically includes text, data, images, sound

or performance.”(Office of Academic Appeals & Regulation 2005)

Office of Academic Appeals & Regulation, 2005, Section 2.1 of the published procedures on “ Cheating, plagiarism and fraudulent or fabricated coursework ” (available online at http://www.leeds.ac.uk/AAandR/cpff.htm - accessed 6th Aug 2005)

Page 7: Comp102   lec 1

Course Outline

Plagiarism is serious: •All cases are reported and go on your student record.•Penalties vary from written warnings, re-sitting modules to expulsion from the University.•Why? – plagiarism undermines the University’s reputation and the quality of its degrees and research, plus plagiarists miss out on learning!

Page 8: Comp102   lec 1

Course Outline

Plagiarism Detection: •Subject experts knowledge of the discipline and its literature.•Changes in writing style, inappropriate writing style.•Electronically.

Page 9: Comp102   lec 1

Course Outline

References: •“Write the text taken from others in double quotes and italicize the text. Put referencec at the end.”[1].•If your ideas match to someone put references [2].•If you want to put multiple references. Put them like it [3][4][5][6].•You have to double quote the major terms like “Software Engineering”.

Page 10: Comp102   lec 1

Course Outline

“The only stupid question is the one which is never asked”[Unknown]

Page 11: Comp102   lec 1

Good programmers can rule the kingdom !

11

Page 12: Comp102   lec 1

Understanding Computer Components and Operations

12

Hardware and software: the two major components of any computer system

Hardware: equipment, or devices Software: programs that contain

instructions for the computer Four major operations in a computer:

Input Processing Output Storage

Page 13: Comp102   lec 1

Takes some values as INPUTPROCESS ThemProduce some result as OUTPUT

Program

13

PROGRAM(Algorithm)

Input (Data) Output (Results)

Page 14: Comp102   lec 1

Programming Task14

A typical programming task can be divided into two phases:

Problem solving phase produce an ordered sequence of steps that

describe solution of problem this sequence of steps is called an algorithm

Implementation phase implement the program in some programming

language

Page 15: Comp102   lec 1

Understanding Computer Components and Operations

15

Input devices: allow data to enter the computer Mouse, keyboard, scanner

Processing: working on the data Organizing data Checking data for accuracy Mathematical or other manipulations on data

Central Processing Unit (CPU): hardware that performs the tasks

Page 16: Comp102   lec 1

Understanding Computer Components and Operations

16

Output devices: provide data to the user Printer, monitor, speakers

Programming language: special language containing instructions for the computer Visual Basic, Java, C#, C++, COBOL

Syntax: rules governing word usage and punctuation in the language

Page 17: Comp102   lec 1

Understanding Computer Components and Operations

17

Machine language: controls the computer’s on/off circuitry

Compiler or interpreter: software that translates programming languages to machine language

Program must be free of syntax errors to be run, or executed, on a computer

To function properly, the logic must be correct

Algorithm = Syntax + Semantics

Page 18: Comp102   lec 1

Understanding the Programming Process

18

Six programming phases: Understand the problem Plan the logic Code the program Use software to translate the

program to machine language Test the program Put the program into production

Page 19: Comp102   lec 1

Understanding the Problem19

May be the most difficult phase Users may not be able to articulate their

needs well User needs may be changing frequently Programmers may have to learn the user’s

functional job tasks Failure to understand the problem is the

major cause of most project failures

Page 20: Comp102   lec 1

Planning the Logic20

Plan the steps that the program will take Use tools such as flowcharts and

pseudocode Flowchart: a pictorial representation of

the logic steps Pseudocode: Natural language

representation of the logic Walk through the logic before coding by

desk-checking the logic

Page 21: Comp102   lec 1

Pseudo Code and Flow Charts

21

Pseudo Code Algorithms written in structural English Good way to begin a solution design Program flow is not always clear

Flow Charts Graphical representation Express flow of control

Page 22: Comp102   lec 1

Using Software to Translate the Program into Machine Language

22

Creating an executable program

Page 23: Comp102   lec 1

Points to Note:23

1. The process consists of repeated application of simple steps

2. All steps are unambiguous (clearly defined)

3. We are capable of doing all those steps

4. Only a limited no. of steps needs to be taken

5. Once all those steps are taken according to the prescribed sequence, the required result will be found

6. Moreover, the process will stop at that point

Page 24: Comp102   lec 1

Constructs24

Basic Programming Tools

Page 25: Comp102   lec 1

Sequence/ Sequential Execution Unconditional

Transfer Input or output Processing

25

Page 26: Comp102   lec 1

Branching

Conditional Transfer

26

??

Page 27: Comp102   lec 1

Loops

Conditional Loop Counted Loop

27

Page 28: Comp102   lec 1

Three constructs28

Page 29: Comp102   lec 1

Flowcharts for three constructs29

Page 30: Comp102   lec 1

Using Flowchart Symbols and Pseudocode Statements

30

Page 31: Comp102   lec 1

FLOW CHARTS

31

Page 32: Comp102   lec 1

Elements of Flowcharts32

Symbol Name

Flowline

Terminal

Input/Output

Processing

Decision

Page 33: Comp102   lec 1

Continued…33

Symbol Name

Connector

Off page Connector

Predefined process

Annotation

Page 34: Comp102   lec 1

34

Simple Sequence

Page 35: Comp102   lec 1

IFTHENELSE Structure35

Page 36: Comp102   lec 1

DOWHILE Structure36

Page 37: Comp102   lec 1

DEVELOPING FLOWCHARTS

Putting algorithms to work

Page 38: Comp102   lec 1

What is a flowchart?

Flowcharts are one method for creating an Algorithm.

They allow a process to be described in a step-by-step manner.

Other Algorithm types are Pseudocode and Prose.

Page 39: Comp102   lec 1

Terminators

Oval Shape One line in or out Used to indicate the

beginning and end of a process

Start

Stop

Page 40: Comp102   lec 1

Data Boxes

Parallelogram shaped One line in, One line out Used to indicate the input

or output of data from the system.

Input

Output

Page 41: Comp102   lec 1

Process Boxes

Rectangle shaped One line in, One line out Used to indicate a process

stepProcess

Page 42: Comp102   lec 1

Decision Boxes

Diamond shaped One line in, two lines out Must contain a binary

question (Yes/No, True/False, 0/1, etc)

Used to branch a program dependent upon a condition being met

Yes/No Question

Page 43: Comp102   lec 1

Subprocess Box

Rectangle shaped, with bars on the sides

One line in, One line out

Used to include a predefined process

Subprocess

Page 44: Comp102   lec 1

Bad design…

What is wrong with this chart?

The box has two lines in, one line out…

Lines must join other lines, never boxes!

Page 45: Comp102   lec 1

Bad design…

What is wrong with this flowchart?

Never use curved lines. Always straight Always vertical or

horizontal.

Page 46: Comp102   lec 1

Remember…

Drawing flowcharts is actually easy. Breaking tasks into small steps is often

much harder than it might seem. If you can flowchart it, you can program

it.

Page 47: Comp102   lec 1

Flowchart Conversion from Fahrenheit-to-Celsius

47

Page 48: Comp102   lec 1

Flowchart for Computing sum, product and average

48

Prompt the user and getnumber1 and number2

Prompt the user and getnumber1 and number2

sum = number1 + number2sum = number1 + number2

Print sum, product and averagePrint sum, product and average

product = number1 * number2product = number1 * number2

average =sum/2average =sum/2

StartStartStartStart

EndEndEndEnd

Page 49: Comp102   lec 1

Flowchart for Computing circumference and area of a circle

49

Prompt the user andget radius of the circlePrompt the user and

get radius of the circle

circumference = 2*3.14*radiuscircumference = 2*3.14*radius

Print circumference andarea of the circle

Print circumference andarea of the circle

Area = 3.14*radius*radius;Area = 3.14*radius*radius;

StartStartStartStart

EndEndEndEnd

Page 50: Comp102   lec 1

Flowchart for Computing min and max of 2 numbers

50

Prompt the user and get number1 and number2Prompt the user and get number1 and number2

Print min and maxPrint min and max

number1 < number2 ?number1 < number2 ?

min = number1min = number1

yes

max = number2max = number2

min = number2min = number2

max = number1max = number1

no

StartStartStartStart

EndEndEndEnd

Page 51: Comp102   lec 1

Flowchart : Computing min of 3 numbers

51

Prompt the user and get number1, number2 and number3

Prompt the user and get number1, number2 and number3

number1 < number2 ?number1 < number2 ?yes

StartStartStartStart

number1 < number3 ?number1 < number3 ?number2 < number3 ?number2 < number3 ?

min = number2min = number2min = number3min = number3

no

min = number3min = number3 min = number1min = number1

yes yes

no no

Print minPrint min

EndEndEndEnd

Page 52: Comp102   lec 1

Flowchart Computing min of 3 numbers

52

Prompt the user and get number1, number2 and number3

Prompt the user and get number1, number2 and number3

number2 < min?number2 < min?

StartStartStartStart

no

min = number1min = number1

min = number2min = number2

yes

number3 < min?number3 < min?

no min = number3min = number3

yes

Print minPrint min

EndEndEndEnd