Вы находитесь на странице: 1из 13

Anatomy of a

Program!
1E3!

2 Anatomy of a Program

Objectives!
To understand the role of basic C++
program elements.!
n To provide a skeleton from which to
generate simple programs.!
n To understand how to compile and run
programs.!
!
n Read Chapter 2 of the textbook.!
n

2 Anatomy of a Program

Test on Chapter 1!
Whats secondary storage?!
n Name two input devices.!
n Give an example of a machine language
instruction.!
n A byte is ?!
!
n

2 Anatomy of a Program

When we RUN a program we are


running
A.
B.
C.

D.

An object le
A C++ le
An assembly
language le
A machine language
le

2 Anatomy of a Program

Programming Environment!
n
n

iMacs running Mac OS X!


Smultron text editor!
n

Terminal utility!
n

Gives UNIX interface!

make myprog!
n

To type in and edit your program files!

Make compiles (and link) myprog.cpp program file!

./myprog!
n

To run the compiled program myprog!


2 Anatomy of a Program

Smultron for Entering/Editing!


Enter/edit your program
using Smultron (your
version may look different)



Save it with .cpp
extension to your own
filespace.

Save often!

2 Anatomy of a Program

Compile and Run !


Use the Terminal utility to enter Unix commands
to your iMac.

cd command allows
you to change directory
if needed.

ls allows you to make
sure your program is
here

make compiles the
program. NB No
file extension!!!

Enter data and
hit return

./ runs the compiled


program

2 Anatomy of a Program

This line tells us


what make decided
to do
7

Make sure make succeeds !


This is what make should do
call the c++ compiler
Watchout for these responses!!

You probably forgot to save the


edited program
Dont use .cpp
with make! Tell
it what it is to
produce.

This runs the old program, since


none of our attempts to compile
the new one succeeded!

You mistyped the filename or


the file is not in the current
directory

2 Anatomy of a Program

Errors: Things can go wrong!


The program has
syntax errors, on
lines 12, 15, and
23, all because I
didnt declare n.
NB Remember you have to recompile (make) after each edit/save
A new version of the
program gives the
wrong answer! This is
a bug.
2 Anatomy of a Program

A first program!
n
n

On the next slide is a complete C++ program.!


It looks scary but I dont expect you to get it all
at once or even this month!!
The program will ask a user for an integer and
will return the sum of the integers from 1 to that
number.!
First think about what the program might need
to do!
n

(This is where being in the lecture is invaluable!!)!

2 Anatomy of a Program

10

C++ Program to Add Integers


from 1 to an entered number!

Declare three integer


variables, i, n, sum.!
Output a message and/
or a value.!
Read in a value for n!
Do the computation!

2 Anatomy of a Program

11

The computation!
Initialise sum!
For each value of i, from 1!
up to and including n!
adding 1 to i each time!
do this:!
Add the current i
to sum!

2 Anatomy of a Program

12

The value printed is:


A.
B.
C.
D.

120
5
0
None
of
these

2 Anatomy of a Program

13

Now compute factorial!


n

n
n

Given the program on


your handout, and
the fragment we just
studied, write a
program to compute
factorial of n, n!.!
n! = 1*2**n or!
n! = n * n-1 * *1!

2 Anatomy of a Program

14

Next!
n

The rest of this topic briefly!


n Outlines

the standard gobbledy-gook that all


programs will have.!
n Introduces variables, data types and
assignment.!
n Explains cin and cout.!
n

The FOR statement is not covered till later.!

2 Anatomy of a Program

15

C++ Program skeleton!


A comment - ignored!
Allow the program to use built-in
facilities for Input and Output (IO)!

main is a function that


returns an integer (int)!

This bit changes



from program to program

2 Anatomy of a Program

These bits are



standard

gobbledy-gook

main returns the !
integer 0!
End of main definition!
16

Variables!
n
n
n

Variables are places which store values.!


i, n, sum are names of variables.!
Valid variable names!
n
n

n
n

Case sensitive - rate, RATE, Rate all different.!


Use meaningful names.!
n

Start with a letter or _ (underscore)!


Rest is letters, digits, or _ !

total_pay, annual_rate, sum !

Dont use reserved keywords.!


n

E.g. int, for, while, !


2 Anatomy of a Program

17

Variables and Data Types!


n

The system needs to know what kinds of things


you will want to store in the variable.!
n
n

Known as the Data type !


int is the data type for integers!
n

n
n
n

double is for decimal number (See next slide)!


bool is the data type for true/false values!
char is used to hold a letter/digit or other symbol!
n

e.g. 125, -34678!

e.g. a, A, 4, &, (a space)

string is for sequences of characters (See later)!


2 Anatomy of a Program

18

Double data type!


n

Well use double for all non-integer values.!


n

float can be used for smaller less precise numbers!

Uses scientific notation!

2 Anatomy of a Program

19

String data type!


A sequence of zero or more characters.!
n E.g. Lucy, mary had a lamb, !
n is the empty string.!
n This data type is not built-in like the ones
weve seen.!
n

n You

have to include the string library at the


top of your program!
n #include <string>!
2 Anatomy of a Program

20

10

Variable declaration!
n

Variables and their data type must be


declared before use.!

This says allocate a


(big) space for a
floating point number,
and call the space
amountDue.

2 Anatomy of a Program

21

Giving Variables Values!


n

Reading!
n

Assignment (C++ uses = for this!!!)!


n
n

cin >> annual_rate;!


sum = 0;!
name = Mary;!

NB Initialisation!
n

The initial value of a variable is arbitrary.!

You must always give it an initial value before use.!

But always something; never blank/empty.!


E.g. counter = 0; found = false; cin >> name;!
2 Anatomy of a Program

22

11

Assignment!
= is the assignment operator.!
n counter = 0;!
n

n Give

(or assign) counter the value 0.!

sum = sum + 4;!


n Give

sum the value got by adding 4 to the old


value of sum.!
sum

3
7

2 Anatomy of a Program

23

Input and Output!


n
n

iostream contains definitions for cout and cin.!


cout << Hello world\n;!
Send the message to the standard output device
(screen)!
n \n is a special sequence meaning newline.!
n

cout << answer;!


n

cin >> number;!


n

Send the value of answer to the output device!


Get/Read a value from the standard input device
(keyboard).!

These examples assume that answer and number


have been declared2 Anatomy
as variables.!
of a Program
24

12

Lets try some variations!


n

Simple problems :!
Read in an amount in pounds and output its
equivalent in euros (1 = 1.27).!
n Read in two numbers, write out their sum, difference
and product.!
n

Using a FOR loop:!


n
n
n
n

Compute n! (i.e. the factorial of n).!


Print out the integers from 1 to n.!
Print n down to 1. (i-- subtracts 1 from i).!
Print the even numbers 2 to 50. (i = i+2 adds 2 to i).!

2 Anatomy of a Program

25

Next !
We cant cover every detail in class.!
n Read Chapter 2 of the textbook.!
n You need to cover, by yourself!
n

n Arithmetic

operators and operator precedence!


n Type conversion!
n Details of input and output!

!
2 Anatomy of a Program

26

13

Вам также может понравиться