Writing Your First Programs: Mr. Dave Clausen La Cañada High School
Writing Your First Programs: Mr. Dave Clausen La Cañada High School
Exercise in Frustration
q q q q q q
Plan well (using paper and pencil) Start early Be patient Handle Frustration Work Hard Dont let someone else do part of the program for you. Understand the Concepts Yourself!
12/31/11 Mr. Dave Clausen 3
12/31/11
2. Develop an Algorithm
Algorithm:
a finite sequence of effective statements that when applied to the problem, will solve it.
Effective Statement:
a clear unambiguous instruction that can be carried out.
12/31/11
12/31/11
12/31/11
Steps in Coding
Edit the program Compile the program Run the program
Syntax errors
Maintenance
12/31/11 Mr. Dave Clausen 10
q q
Each subdivision is called stepwise refinement. Each task is called a module We can use a structure chart to show relationships between modules.
12/31/11 Mr. Dave Clausen 11
S M S u b t a s k S u b
t r u
c t u T
r e
a r t
a in t a s k
a s k S u b t a s k
12/31/11
12
Pseudocode
is written in English with C++ like sentence structure and indentations. Major Tasks are numbered with whole numbers Subtasks use decimal points for outline.
12/31/11
13
12/31/11
14
Second-Level Refinement
Update checkbook Get Information Get Starting Balance
12/31/11
Display Results
15
Pseudocode
Checkbook.cpp
12/31/11 Mr. Dave Clausen 16
Writing Programs
q
C++ Vocabulary
reserved words
have a predefined meaning that cant be changed
library identifiers
words defined in standard C++ libraries
12/31/11
17
Writing Programs
q
Syntax
rules for construction of valid statements, including
order of words punctuation
12/31/11 Mr. Dave Clausen 18
Library Identifiers
q
iomanip
setprecision setw
math
pow sin sqrt
12/31/11 Mr. Dave Clausen 19
Identifiers
q
Must start with a letter of the alphabet or underscore _ (we will not use underscores to start identifiers) length determined by compiler
Turbo C++ Win 4.5 32 characters Borland C++ unlimited Codewarrior 255 characters aim for 8 to 15 characters
Comments Preprocessor Directives Constant Declaration Section Type Declaration Section Function Declarations Main Program Heading: int main( )
Declaration Section Statement Section
12/31/11
21
12/31/11
22
Executable Statement
basic unit of grammar
library identifiers, programmer defined identifiers, reserved words, numbers and/or characters
Type int
represent integers or whole numbers Some rules to follow:
Plus signs do not need to be written before the number Minus signs must be written when using negative #s Decimal points cannot be used Commas cannot be used Leading zeros should be avoided (octal or base 8 #s) limits.h int_max int_min
12/31/11 Mr. Dave Clausen 24
Type double
used to represent real numbers many programmers use type float, the AP Board likes the extra precision of double avoid leading zeros, trailing zeros are ignored limits.h, float.h
dbl_max, dbl_min, dbl_dig
12/31/11
25
Type char
used to represent character data
a single character which includes a space See Appendix 4 in our text
must be enclosed in single quotes eg. d Escape sequences treated as single char
\n \ \ \t newline apostrophe double quote tab
Mr. Dave Clausen 26
12/31/11
Strings
used to represent textual information string constants must be enclosed in double quotation marks eg. Hello world!
empty string new line char or string \n the word \hello\ (puts quotes around hello )
Output
q
#include <iostream.h>
cout pronounced see-out cout << \n; cout << endl; cout << Hello world!; cout << Hello world! << endl;
printadd.cpp
12/31/11 Mr. Dave Clausen 28
Formatting Integers
q
#include <iomanip.h>
manipulators)
(input/output
Setprecision
q
Precision is set and will remain until the programmer specifies a new precision
The decimal uses one position Trailing zeros are printed the specified number of places Leading plus signs are omitted Leading minus signs are printed and use 1 position Digits are rounded, not truncated.
12/31/11
30
Test Programs
q
q q q q
Test programs are short programs written to provide an answer to a specific question. You can try something out Play with C+ + Ask what if questions Experiment: try and see
12/31/11
31