0% found this document useful (0 votes)
63 views35 pages

CMP1001 Introduction To Programming (C++)

Uploaded by

Tarif Cemay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views35 pages

CMP1001 Introduction To Programming (C++)

Uploaded by

Tarif Cemay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

CMP1001

Introduction to
Programming (C++)
Computer Systems
A computer program is…
◦ A set of instructions for a computer to follow

Computer software is …
◦ The collection of programs used by a computer
◦ Includes:
◦ Editors
◦ Translators
◦ System Managers
Hardware
Three main classes of computers

◦ PCs (Personal Computer)


◦ Relatively small used by one person at a time

◦ Workstation
◦ Larger and more powerful than a PC

◦ Mainframe
◦ Still larger
◦ Requires support staff
◦ Shared by multiple users
Computer Organization
◦ Five main components
◦ Input devices
◦ Allows communication to the computer
◦ Output devices
◦ Allows communication to the user
◦ Processor (CPU)

◦ Main memory
◦ Memory locations containing the running program
◦ Secondary memory
◦ Permanent record of data often on a disk
The Processor
Typically called the CPU
◦ Central Processing Unit
◦ Follows program instructions
◦ Typical capabilities of CPU include:

add
subtract
multiply
divide
move data from location to location
Computer Memory (RAM)
•Stores data and program instructions for CPU to execute
• A program and its data must be brought to memory before they can be executed

•Stores intermediate and final results of processing.


•Volatile: Contents are erased when computer is turned off or reset.
•A memory unit is an ordered sequence of bytes, each holds eight bits.
•A byte is the minimum storage unit. No two data can share or split the same byte.

+1300042774 0100 1101 0111 1101 0001 0100 0001 0110


+1400593419 0101 0011 0111 1011 0101 1100 0000 1011
+1200274027 0100 0111 1000 1010 1011 1010 0110 1011
Larger Data Items
Some data is too large for a single byte
◦ Most integers and real numbers are too large

◦ Address refers to the first byte

◦ Next few consecutive bytes can store the additional


bits for larger data
Data or Code?
‘A’ may look like 01000001
65 may look like 01000001
An instruction may look like 01000001

How does the computer know the meaning


of 01000001?
◦ Interpretation depends on the current instruction

Programmers rarely need to be concerned with


this problem.
◦ Reason as if memory locations contain letters and
numbers rather than zeroes and ones
Secondary Memory
Main memory stores instructions and
data while a program is running.
Secondary memory
◦ Stores instructions and data between sessions
◦ A file stores data or instructions in
secondary memory
Memory Access
Random Access
◦ Usually called RAM
◦ Computer can directly access any memory location

Sequential Access
◦ Data is generally found by searching through
other items first
◦ More common in secondary memory
Computer Input
Computer input consists of
◦ A program

◦ Some data
Computer Software
The operating system
◦ Allows us to communicate with the computer
◦ Is a program
◦ Allocates the computer’s resources
◦ Responds to user requests to run other programs

Common operating systems include…


◦ UNIX Linux DOS
Windows Macintosh VMS

COPYRIGHT © 2003 PEARSON EDUCATION, INC.


Programming Languages
•Computers can not use human languages, and programming in the binary language of
computers is a very difficult, tedious process
•Therefore, most programs are written using a programming language and are converted to the
binary language used by the computer
•Three major categories of prog languages:
• Machine Language
• Assembly Language
• High level Language

13
Machine Language
•Natural language of a particular computer
•Primitive instructions built into every computer
•The instructions are in the form of binary code
•Any other types of languages must be translated down to this level

14
Assembly Languages
•English-like Abbreviations used for operations (Load R1, R8)
•Assembly languages were developed to make programming easier
•The computer cannot understand assembly language - a program called assembler is used to
convert assembly language programs into machine code

15
Low-level Languages
Assembly language must be translated to
machine language (zeros and ones)
0110 1001 1010 1011
The CPU can follow machine language

An assembly language command such as

ADD X Y Z

might mean add the values found at x and y


in memory, and store the result in location z.
High-level Languages
Common programming languages include …
C C++ Java Pascal Visual Basic FORTRAN
COBOL Lisp Scheme Ada

These high – level languages


◦ Resemble human languages
◦ Are designed to be easy to read and write
◦ Use more complicated instructions than
the CPU can follow
◦ Must be translated to zeros and ones for the CPU
to execute a program
Compilers
Translate high-level language to machine language

◦ Source code
◦ the original program in a high level language
◦ Object code
◦ the translated version in machine language
Linkers
Some programs we use are already compiled
◦ Their object code is available for us to use
◦ For example: Input and output routines

A Linker combines
◦ The object code for the programs we write
and
◦ The object code for the pre-compiled routines
into
The machine language program the CPU can run
History Note
First programmable computer
◦ Designed by Charles Babbage
◦ Began work in 1822
◦ Not completed in Babbage’s lifetime

First programmer
◦ Ada Augusta, Countess of Lovelace
◦ Colleague of Babbage
Programming
and Problem Solving
Algorithm
◦ A sequence of precise instructions which
leads to a solution

Program
◦ An algorithm expressed in a language the computer
can understand
Program Design
Programming is a creative process
◦ No complete set of rules for creating a program

Program Design Process


◦ Problem Solving Phase
◦ Result is an algorithm that solves the problem
◦ Implementation Phase
◦ Result is the algorithm translated into a programming
language
Problem Solving Phase
Be certain the task is completely specified
◦ What is the input?
◦ What information is in the output?
◦ How is the output organized?

Develop the algorithm before implementation


◦ Experience shows this saves time in getting your
program to run.
◦ Test the algorithm for correctness
Implementation Phase
Translate the algorithm into a programming
language
◦ Easier as you gain experience with the language

Compile the source code


◦ Locates errors in using the programming language

Run the program on sample data


◦ Verify correctness of results

Results may require modification of


the algorithm and program
Introduction to C++
Where did C++ come from?
◦ Derived from the C language
◦ C was derived from the B language
◦ B was derived from the BCPL language

Why the ‘++’?


◦ ++ is an operator in C++ and results in a cute pun
C++ History
C developed by Dennis Ritchie at AT&T
Bell Labs in the 1970s.
◦ Used to maintain UNIX systems
◦ Many commercial applications written in c

C++ developed by Bjarne Stroustrup at AT&T


Bell Labs in the 1980s.
◦ Overcame several shortcomings of C
◦ Incorporated object oriented programming
◦ C remains a subset of C++
Program Layout (1/3)
Compiler accepts almost any pattern of line
breaks and indentation
Programmers format programs so they
are easy to read
◦ Place opening brace ‘{‘ and closing brace ‘}’
on a line by themselves
◦ Indent statements
◦ Use only one statement per line
Program Layout (2/3)
Variables are declared before they are used
◦ Typically variables are declared at the beginning of
the program
◦ Statements (not always lines) end with a semi-colon

Include Directives
#include <iostream>
◦ Tells compiler where to find information about items
used in the program
◦ iostream is a library containing definitions of cin and cout
Program Layout (3/3)
using namespace std;
Tells the compiler to use names in iostream in
a “standard” way

To begin the main function of the program


int main()
{
To end the main function
return 0;
}
◦ Main function ends with a return statement
Running a C++ Program
C++ source code is written with a text
editor

The compiler on your system converts


source code to object code.

The linker combines all the object code


into an executable program.
32
Run a Program
Obtain code in Display 1.10
Compile the code
Fix any errors the compiler indicates and
re-compile the code
Run the program
Now you know how to run a program on
your system
1.4

Testing and Debugging


Bug
◦ A mistake in a program

Debugging
◦ Eliminating mistakes in programs
◦ Term used when a moth caused a failed relay
on the Harvard Mark 1 computer. Grace Hopper
and other programmers taped the moth in logbook
stating:
“First actual case of a bug being found.”
Program Errors
Syntax errors
◦ Violation of the grammar rules of the language
◦ Discovered by the compiler
◦ Error messages may not always show correct location of
errors

Run-time errors
◦ Error conditions detected by the computer at run-time

Logic errors
◦ Errors in the program’s algorithm
◦ Most difficult to diagnose
◦ Computer does not recognize an error

You might also like