chapter 1: getting started - cs.science.cmu.ac.th · chapter 1 getting started. ... –if we want...

51
204112 Structured Programming 1 Chapter 1 Getting Started

Upload: hacong

Post on 10-Jul-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 1

Chapter 1

Getting Started

Page 2: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 2

Outline

Introduction to Programming

Algorithm

Programming Style

The printf( ) Function

Common Programming Errors

Introduction to Modularity

Top-Down Program Development

Chapter Summary

Page 3: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 3

Introduction to Programming

Computer program

– is a sequence of instructions used to

operate a computer to produce a specific

result

Programming language

– is a set of instructions used to construct a

program

Page 4: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 4

IPO: Input, Process, Output

Input

Devices

CentralProcessing

Unit

Main

Memory

Output

Devices

SecondaryStorageDevices

Main Hardware Component

Page 5: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 5

Set of Instructions &

Library Functions

Page 6: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 6

FORmula TRANslation

(FORTRAN)

Page 7: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 7

COmmon Business

Oriented Language (COBOL)

Current trend

– Programming

Languages that

can manipulate

database:

–VB

–JAVA

–etc.

Page 8: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 8

Algorithm

is a step-by-step sequence of instructions that describes how to perform a computation

a solution to a computer programming problem

Example

– if we want to calculate the sum of whole numbers from 1 through 100

Page 9: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 9

Summing the numbers

1 through 100

Page 10: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 10

Summing the numbers

1 through 100 (cont.)

Page 11: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 11

Algorithms for summing the

numbers 1 through 100

Set n equal to 100

Set a equal to 1

Set b equal to 100

Calculate sum = n(a+b)/2 Formula

Print the sum

Page 12: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 12

Pseudocode & Flowchart

Pseudocode

– is English phrases used to describe the algorithm (the processing steps)

– English-like steps that describes the solution

Flowchart

– provides a pictorial representation of the algorithm using the symbols

– picture with specific blocks detailing out the logical flow of the solution

Page 13: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 13

From Algorithms to Programs

Coding

Page 14: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 14

From Algorithms to Programs

C Source Code#include <stdio.h>

#include <conio.h>

int main()

{

int n, remainder;

clrscr();

printf("Input n : ");

scanf("%d", &n);

remainder = n % 2;

if (remainder == 0)

printf ("n is even number\n");

else

printf ("n is odd number\n");

getch();

return 0;

}

Input N

Remainder = 0 ?

Output Answer

Start

End

Answer <- EVEN Answer <- ODD

Yes No

Remainder <- N modulo 2

Problem Statement

Read a number from the keyboard. Check and output if a given number N is ODD or EVEN

Page 15: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 15

Programming Style

Standard formint main( )

{

program statements in here;

return 0;

}

Remark: At the end of the program, Zero is usually returned to indicate error-free function termination.

clarity and ease in reading

Page 16: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 16

Programming Style (cont.)

Poor programming style

int

main

(

) { printf

(“Hello there world!”

); return 0;}

difficult to read and understand

Page 17: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 17

Comments

are explanatory remarks made within a program

have no effect on program execution

start of comment with /*

end of comment via */

Example/* this is a comment */

/* this program prints out a message */

Page 18: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 18

Pgm1-3.cpp & Output

Output

Comment

Page 19: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 19

The printf( ) Function

Page 20: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 20

Pgm1-1.cpp & Output

Output

printf( )

Preprocessor command

Page 21: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 21

Preprocessor Command

Begin with a pound sign “#”, do not end with “;’ e.g. #include<stdio.h>

The first step in the C program compilation stage

The “#include” preprocessor command causes the contents of header file in “<>” to be inserted where the #include command appears.

Page 22: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 22

C Libraries - #include …

Library file Functions

#include <stdio.h> Standard I/O functions – printf, scanf, getc, fopen,

fclose

#include <io.h> Traditional file operations – lock, create, filelength,

read, write

#include <math.h> Arithmetic – abs, floor, pow, sqrt

Logarithmic – log, exp,

Trignometric – sin, tan, cos, atan

Floating point – fabs, fmod

#include <string.h> String operations – strcpy, strcat, strcmp, strlen,

strrev, strlwr, strupr

Without including these libraries, you cannot write C programs that need to use these standard

function. Check your reference for details on the C libraries, their functions and how they can be used.

Page 23: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 23

Pgm1-2.cpp & Output

Output

new line: \n

Page 24: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 24

\n

If the backslash was

omitted from the

second printf( ) call

in Pgm1-2.cpp

Original Output

New Output (without \)

Page 25: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 25

An Example of \n

Output

Page 26: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 26

Common Programming Errors

Omitting the parentheses after mainmain main( )

Omitting or incorrectly typing the opening brace { that signifies the start of a function body

Omitting or incorrectly typing the closing brace } that signifies the end of a function

Misspelling the name of a functionprint( ) printf( )

Page 27: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 27

Common Programming Errors

(cont.)

Forgetting to close the message to printf( )

with a double quote (“ ”) symbol

Omitting the semicolon (;) at the end of each

statement

Forgetting the \n to indicate a new line

Page 28: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 28

Introduction to Modularity

Page 29: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 29

Introduction to Modularity

(cont.)

Page 30: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 30

Functions

Page 31: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 31

Functions (cont.)

The names permissible for functions are

referred to as identifiers

Rules

– first character of the name must be a letter or

underscore ( _ )

– only letters, digits, or underscores may follow the

initial letter

– cannot be one of the keywords

– may have no more than 31 characters

Page 32: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 32

Keywords (Reserved Words)

Page 33: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 33

An Example of

Valid & Invalid C Identifiers

grosspay taxCalc addNums DegToRad

1AB3 salestax netpay while

multTwo E*6 bessel1

Case-sensitive language

– TOTAL, total, TotaL three distinct names

Page 34: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 34

The main( ) Function

Page 35: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 35

The main( ) Function (cont.)

Page 36: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 36

A Sample main( ) Function

Page 37: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 37

Top-Down

Program Development

The five steps in the top-down development

procedure

– Step1: determine the desired output

– Step2: determine the input items

– Step3: design the program

• determine an algorithm

• do a hand calculation

– Step4: write the program

– Step5: test the output

Page 38: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 38

An Example of Problem

The circumference, C, of a circle is given by

the formula C = 2r, where is the constant

3.1416 (accurate to four decimal places), and r

is the radius of the circle. Using this

information, write a C program to calculate the

circumference of a circle that has a 2-inch

radius.

Page 39: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 39

Step1: Determine the

Desired Output

calculate, print, determine, find, or

compare can be used to determine the

desired output

to calculate the circumference of a circle

Page 40: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 40

Step2: Determine the

Input Items

input item is the name of an input

quantity

input value is a specific number

input item the radius of the circle

input value 2

Page 41: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 41

Step3: Design the Program

consists of 2 steps

– determine an algorithm (pseudocode)Assign a value to r

Calculate the circumference using the formula C = 2r

Display the result

– do a hand calculation

• substitute radius of 2 inches into the formula

• 2(3.1416)2 = 12.5664 inches

Page 42: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 42

Pgm1-4.cpp & Output

Output

Calculate the circumference of the circle

Page 43: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 43

Step5: Test the Output

is to verify that a program works correctly

and fulfills its requirements

The same program can be used over

and over with new input data

Program error is called a bug

Debugging includes locating, correcting,

and verifying the correction

Page 44: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 44

Modularity & Top-Down Design

Page 45: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 45

Modularity & Top-Down Design

(cont.)

Page 46: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 46

Program Translation

Page 47: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 47

Program Translation (cont.)

Interpreter

– each statement in the source program is translated

individually and executed immediately

Compiler

– all the statements in the source program are

translated before any one statement is executed

The result of compiling a source program is

called an executable program

Page 48: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 48

Chapter Summary

main( )

printf( )

The body of function

{

All program statements in here;

}

Page 49: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 49

Chapter Summary (cont.)

All C statements must be terminated by

a semicolon (;)

printf( ) can be a message enclosed with

double quotes

– printf(“Hello there world!”);

Page 50: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 50

Q&A

Page 51: Chapter 1: Getting Started - cs.science.cmu.ac.th · Chapter 1 Getting Started. ... –if we want to calculate the sum of whole ... number N is ODD or EVEN. 204112 Structured Programming

204112 Structured Programming 51

Practice

Write algorithm to solve the following:

input 5 numbers and display how many odd numbers and how many even numbers.

Example: If I input 5, 8, 9, 4, 7

The number of odd is 3

The number of even is 2