chapter 1: getting started - cs.science.cmu.ac.th · chapter 1 getting started. ... –if we want...
TRANSCRIPT
204112 Structured Programming 1
Chapter 1
Getting Started
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
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
204112 Structured Programming 4
IPO: Input, Process, Output
Input
Devices
CentralProcessing
Unit
Main
Memory
Output
Devices
SecondaryStorageDevices
Main Hardware Component
204112 Structured Programming 5
Set of Instructions &
Library Functions
204112 Structured Programming 6
FORmula TRANslation
(FORTRAN)
204112 Structured Programming 7
COmmon Business
Oriented Language (COBOL)
Current trend
– Programming
Languages that
can manipulate
database:
–VB
–JAVA
–etc.
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
204112 Structured Programming 9
Summing the numbers
1 through 100
204112 Structured Programming 10
Summing the numbers
1 through 100 (cont.)
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
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
204112 Structured Programming 13
From Algorithms to Programs
Coding
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
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
204112 Structured Programming 16
Programming Style (cont.)
Poor programming style
int
main
(
) { printf
(“Hello there world!”
); return 0;}
difficult to read and understand
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 */
204112 Structured Programming 18
Pgm1-3.cpp & Output
Output
Comment
204112 Structured Programming 19
The printf( ) Function
204112 Structured Programming 20
Pgm1-1.cpp & Output
Output
printf( )
Preprocessor command
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.
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.
204112 Structured Programming 23
Pgm1-2.cpp & Output
Output
new line: \n
204112 Structured Programming 24
\n
If the backslash was
omitted from the
second printf( ) call
in Pgm1-2.cpp
Original Output
New Output (without \)
204112 Structured Programming 25
An Example of \n
Output
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( )
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
204112 Structured Programming 28
Introduction to Modularity
204112 Structured Programming 29
Introduction to Modularity
(cont.)
204112 Structured Programming 30
Functions
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
204112 Structured Programming 32
Keywords (Reserved Words)
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
204112 Structured Programming 34
The main( ) Function
204112 Structured Programming 35
The main( ) Function (cont.)
204112 Structured Programming 36
A Sample main( ) Function
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
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.
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
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
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
204112 Structured Programming 42
Pgm1-4.cpp & Output
Output
Calculate the circumference of the circle
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
204112 Structured Programming 44
Modularity & Top-Down Design
204112 Structured Programming 45
Modularity & Top-Down Design
(cont.)
204112 Structured Programming 46
Program Translation
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
204112 Structured Programming 48
Chapter Summary
main( )
printf( )
The body of function
{
All program statements in here;
}
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!”);
204112 Structured Programming 50
Q&A
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