Comp102 lec 1

Preview:

Citation preview

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

Course OutlineInstructor Information:Name Ms. Saira AnwarEmailSairaAnwar@fccollege.edu.pkSairaAnwar@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/

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

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%

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.

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)

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!

Course Outline

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

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”.

Course Outline

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

Good programmers can rule the kingdom !

11

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

Takes some values as INPUTPROCESS ThemProduce some result as OUTPUT

Program

13

PROGRAM(Algorithm)

Input (Data) Output (Results)

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

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

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

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

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

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

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

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

Using Software to Translate the Program into Machine Language

22

Creating an executable program

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

Constructs24

Basic Programming Tools

Sequence/ Sequential Execution Unconditional

Transfer Input or output Processing

25

Branching

Conditional Transfer

26

??

Loops

Conditional Loop Counted Loop

27

Three constructs28

Flowcharts for three constructs29

Using Flowchart Symbols and Pseudocode Statements

30

FLOW CHARTS

31

Elements of Flowcharts32

Symbol Name

Flowline

Terminal

Input/Output

Processing

Decision

Continued…33

Symbol Name

Connector

Off page Connector

Predefined process

Annotation

34

Simple Sequence

IFTHENELSE Structure35

DOWHILE Structure36

DEVELOPING FLOWCHARTS

Putting algorithms to work

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.

Terminators

Oval Shape One line in or out Used to indicate the

beginning and end of a process

Start

Stop

Data Boxes

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

or output of data from the system.

Input

Output

Process Boxes

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

stepProcess

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

Subprocess Box

Rectangle shaped, with bars on the sides

One line in, One line out

Used to include a predefined process

Subprocess

Bad design…

What is wrong with this chart?

The box has two lines in, one line out…

Lines must join other lines, never boxes!

Bad design…

What is wrong with this flowchart?

Never use curved lines. Always straight Always vertical or

horizontal.

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.

Flowchart Conversion from Fahrenheit-to-Celsius

47

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

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

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

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

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