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

C++ Program Anatomy

Embed Size (px)

DESCRIPTION

Basic C++ program anatomy for beginners

Citation preview

Page 1: C++ Program Anatomy

1

2 Anatomy of a Program 1

Anatomy of a Program!

1E3!

2 Anatomy of a Program 2

Objectives!

n  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.!

Page 2: C++ Program Anatomy

2

2 Anatomy of a Program 3

Test on Chapter 1!

n  What’s secondary storage?!n  Name two input devices.!n  Give an example of a machine language

instruction.!n  A byte is …?!!

When  we  RUN  a  program  we  are  running  

A.  An  object  file  B.  A  C++  file  C.  An  assembly  

language  file  D.  A  machine  language  

file  

2 Anatomy of a Program 4

Page 3: C++ Program Anatomy

3

2 Anatomy of a Program 5

Programming Environment!n  iMacs running Mac OS X!n  Smultron text editor!

n  To type in and edit your program files!n  Terminal utility!

n  Gives UNIX interface!n  “make myprog”!

n  Make compiles (and link) myprog.cpp program file!n  “./myprog”!

n  To run the compiled program myprog!

2 Anatomy of a Program 6

Enter/edit your program using Smultron (your version may look different) Save it with .cpp extension to your own filespace. Save often!

Smultron for Entering/Editing!

Page 4: C++ Program Anatomy

4

Compile and Run …!

2 Anatomy of a Program 7

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!!!

./ runs the compiled program

Enter data and hit return

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

This line tells us what make decided to do …

Make sure make succeeds …!

2 Anatomy of a Program 8

You probably forgot to save the edited program

Don’t use .cpp with make! Tell it what it is to produce.

You mistyped the filename or the file is not in the current directory

Watchout for these responses!!

This runs the old program, since none of our attempts to compile the new one succeeded!

This is what make should do – call the c++ compiler

Page 5: C++ Program Anatomy

5

Errors: Things can go wrong…!

2 Anatomy of a Program 9

The program has syntax errors, on lines 12, 15, and 23, all because I didn’t declare n.

A new version of the program gives the wrong answer! This is a “bug”.

NB Remember you have to recompile (make) after each edit/save

2 Anatomy of a Program 10

A first program!n  On the next slide is a complete C++ program.!n  It looks scary but I don’t expect you to get it all

at once or even this month!!n  The program will ask a user for an integer and

will return the sum of the integers from 1 to that number.!

n  First think about what the program might need to do…!n  (This is where being in the lecture is invaluable!!)!

Page 6: C++ Program Anatomy

6

2 Anatomy of a Program 11

C++ Program to Add Integers from 1 to an entered number!

Declare three integer variables, i, n, sum.!

Read in a value for n!

Output a message and/or a value.!

Do the computation!

2 Anatomy of a Program 12

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!

Page 7: C++ Program Anatomy

7

A.  120  B.  5  C.  0  D.  None  

of  these  

The  value  printed  is:  

2 Anatomy of a Program 13

Now compute factorial!n  Given the program on

your handout, and the fragment we just studied, write a program to compute factorial of n, n!.!

n  n! = 1*2*…*n or!n  n! = n * n-1 * … *1!

2 Anatomy of a Program 14

Page 8: C++ Program Anatomy

8

2 Anatomy of a Program 15

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 16

C++ Program skeleton!

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

This bit changes from program to program

A comment - ignored!

main is a function that returns an integer (int)!

End of main definition!

main returns the !integer 0!

These bits are standard

gobbledy-gook

Page 9: C++ Program Anatomy

9

2 Anatomy of a Program 17

Variables!n  Variables are places which store values.!n  i, n, sum are names of variables.!n  Valid variable names!

n  Start with a letter or _ (underscore)!n  Rest is letters, digits, or _ !

n  Case sensitive - rate, RATE, Rate all different.!n  Use meaningful names.!

n  total_pay, annual_rate, sum !n  Don’t use reserved keywords.!

n  E.g. int, for, while, …!

2 Anatomy of a Program 18

Variables and Data Types!n  The system needs to know what kinds of things

you will want to store in the variable.!n  Known as the Data type !n  int is the data type for integers!

n  e.g. 125, -34678!n  double is for decimal number (See next slide)!n  bool is the data type for true/false values!n  char is used to hold a letter/digit or other symbol!

n  e.g. ‘a’, ‘A’, ‘4’, ‘&’, ‘ ‘ (a space)

n  string is for sequences of characters (See later)!

Page 10: C++ Program Anatomy

10

2 Anatomy of a Program 19

Double data type!n  We’ll use double for all non-integer values.!

n  float can be used for smaller less precise numbers!n  Uses scientific notation!

2 Anatomy of a Program 20

String data type!

n  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

we’ve seen.!n  You have to include the string library at the

top of your program!n  #include <string>!

Page 11: C++ Program Anatomy

11

2 Anatomy of a Program 21

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 22

Giving Variables Values!n  Reading!

n  cin >> annual_rate;!n  Assignment (C++ uses “=“ for this!!!)!

n  sum = 0;!n  name = “Mary”;!

n  NB Initialisation!n  The initial value of a variable is arbitrary.!

n  But always something; never blank/empty.!n  You must always give it an initial value before use.!

n  E.g. counter = 0; found = false; cin >> name;!

Page 12: C++ Program Anatomy

12

2 Anatomy of a Program 23

Assignment!

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

n  Give (or assign) counter the value 0.!n  sum = sum + 4;!

n  Give sum the value got by adding 4 to the old value of sum.!

3 sum

7

2 Anatomy of a Program 24

Input and Output!n  iostream contains definitions for cout and cin.!n  cout << “Hello world\n”;!

n  Send the message to the standard output device (screen)!

n  “\n” is a special sequence meaning “newline”.!n  cout << answer;!

n  Send the value of answer to the output device!n  cin >> number;!

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

n  These examples assume that answer and number have been declared as variables.!

Page 13: C++ Program Anatomy

13

2 Anatomy of a Program 25

Let’s try some variations!n  Simple problems :!

n  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  Compute n! (i.e. the factorial of n).!n  Print out the integers from 1 to n.!n  Print n down to 1. (“i--” subtracts 1 from i).!n  Print the even numbers 2 to 50. (“i = i+2” adds 2 to i).!

2 Anatomy of a Program 26

Next …!

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

n  Arithmetic operators and operator precedence!n  Type conversion!n  Details of input and output!

!