Chapter 1 Introduction to Computer and C++ Programming.

32
Chapter 1 Introduction to Computer and C++ Programming

Transcript of Chapter 1 Introduction to Computer and C++ Programming.

Page 1: Chapter 1 Introduction to Computer and C++ Programming.

Chapter 1

Introduction to Computer and C++ Programming

Page 2: Chapter 1 Introduction to Computer and C++ Programming.

Computer Systems

• There are 3 classes of computers:– Personal Computers (PCs)– Workstations– Mainframes

• Network: – Consists of a number of computers connected

to share resources and information.– Including server and client computers.

Page 3: Chapter 1 Introduction to Computer and C++ Programming.

Computer Hardware

• Input devices:– Allowing users to communicate information to

computers.– Ex: keyboard, mouse, scanner…

• Output devices: – Allowing computer to communicate

information to users.– Ex: monitor, printer

Page 4: Chapter 1 Introduction to Computer and C++ Programming.

Computer Hardware (cont.)

• Central Processing Unit (CPU).– Is the Control unit (cerebellum) of all

operations of a computer– Containing Arithmetic and Logical Unit (ALU)

Page 5: Chapter 1 Introduction to Computer and C++ Programming.

Computer Hardware (cont.)

• Main memory: – Temporary storage for data– Very fast to access– Random access memory (RAM)– Locations: a number to present the memory

address– Units:

• Bit: the smallest unit, containing binary values (0 or 1)• Byte: equals to 8 bits (standard unit of memory)• Word: equals to 2 bytes (most computers)

Page 6: Chapter 1 Introduction to Computer and C++ Programming.

Computer Hardware (cont.)

• Secondary memory:– Permanent storage– Slower to access– Removable– Information stored a files (execution, data…)– Ex: floppy drive, hard drive, thumb drive,

CDs ...

Page 7: Chapter 1 Introduction to Computer and C++ Programming.

Main Components of Computers

InputDevice(s)

OutputDevice(s)

MainMemory

SecondaryMemory

CPU

Page 8: Chapter 1 Introduction to Computer and C++ Programming.

Computer Software

• Operating System (OS)– The SW to:

• Control behaviors of a computer.• Control communications to memories and devices• Manage resources

– Ex: DOS, UNIX, Linux, Windows, Android, Mac …

Page 9: Chapter 1 Introduction to Computer and C++ Programming.

Computer Software (cont.)

• Program– Is a set of instructions for a computer to

follow.– Usually known as application

• Data– Inputs to a program

Page 10: Chapter 1 Introduction to Computer and C++ Programming.

Computer Languages

• Machine language:– low level - On or Off - series of 0’s and 1’s. – Difficult for humans to read and debug.

Very fast for computer to process. – Specific to a particular machine. – Example:

0001 0010 0100 0001 (16-bit)

0000 0001 0000 0010 0000 0100 0000 0001 (32-bit)

Page 11: Chapter 1 Introduction to Computer and C++ Programming.

Computer Languages (cont.)

• Assembly language – consists of a command and one or two

storage locations which have been assigned letter names.

– Computer must interpret into machine language using an assembler

– Example: Add R2, R4, R1

Page 12: Chapter 1 Introduction to Computer and C++ Programming.

Computer Languages (cont.)

• Higher level languages– like English. Easy to read and understand. – Portable to many machines and has a language

standard. Slower to process. Commands are written and saved in a source file.

– A compiler program translates into machine language object or binary file and a linker connects it to other programs (such as to printer), loads in memory and executes. If there are any language (SYNTAX) errors the compiler cannot compile and the compilation stops.

– Example:R1 = R2 + R4;

Page 13: Chapter 1 Introduction to Computer and C++ Programming.

Programming and Problem Solving

• Programming is Problem Solving!!!!!

Page 14: Chapter 1 Introduction to Computer and C++ Programming.

Algorithm

• A sequence of precise instructions that lead to a solution or step-by-step set of instructions to solve a problem.

• Three requirements:1. There must be a finite number of steps.

2. Each step must be well defined.

3. Must accomplish the task.

Page 15: Chapter 1 Introduction to Computer and C++ Programming.

Program Design

– Precise Definition of the problem (WHAT’s) • What will the output of your program be?• What information is needed?• What formulas are needed

– Algorithm Design (HOW’s)• Define how to solve the problem

– Testing• Check if the problem solved.

Page 16: Chapter 1 Introduction to Computer and C++ Programming.

Algorithm Design (example)

Determine how many times a name occurs in a list of names.

1. Get the list of names.2. Get the name being checked.3. Set a counter to 0.4. For each name in the list, do the followings:

• Compare the name with the targeted name.• If the names are the same, increase the counter.

5. Announce the answer is the number indicated by the counter.

Page 17: Chapter 1 Introduction to Computer and C++ Programming.

Object Oriented Programming (OOP)

• A new approach where we model objects in the real world developed in the 1990’s. Used first for simulations, data programs, it has now become the industry standard. Objects are defined by code called classes.

Page 18: Chapter 1 Introduction to Computer and C++ Programming.

OOP Characteristics

• Encapsulation: Combining the data of the object with its behavior and hiding the implementation from the user.

• Inheritance: Allowing one object to reuse code of another object.

• Polymorphism: Giving different meanings to the same name.

Page 19: Chapter 1 Introduction to Computer and C++ Programming.

Software Development Cycle

1. Analysis and specification of the task (Problem definition).

2. Design the software (Object and algorithm design)

3. Implementation (Coding)

4. Testing

5. Maintenance and Enhancement

Who programs first, finishes last!

Page 20: Chapter 1 Introduction to Computer and C++ Programming.

Example of SW Design

• Compute the miles per gallon for a trip of a certain distance in miles

Page 21: Chapter 1 Introduction to Computer and C++ Programming.

Example of SW Design (cont.)

• Analysis: (WHAT’s)– Distance (miles)– Gas amount (gallons)– MPG (miles per gallon)

Identifier Type I/O Description

miles Double input miles > 0.0

gallons Double input gallons > 0.0

MPG Double output Miles per gallon

Data Dictionary

Page 22: Chapter 1 Introduction to Computer and C++ Programming.

Example of SW Design (cont.)

• Initial Design: (HOW’s)– Read in distance– Read in gas amount– Calculate MPG– Print out result

Page 23: Chapter 1 Introduction to Computer and C++ Programming.

Example of SW Design (cont.)

• Refined Design: (HOW’s)1. Read in distance (miles)

• Check if distance <= 0.0, then go back to 1

2. Read in gas amount (gallons)• Check if gallons <= 0.0, then go back to 2

3. Calculate MPG = miles/gallons

4. Print out result

Page 24: Chapter 1 Introduction to Computer and C++ Programming.

Example of SW Design (cont.)

• Structure chart: – (Top Down Design: Divide and Conquer -

Break down the main problem into subproblems and the data flow between them.

– Minimum of 3 subtasks: Input, Process, Output

Page 25: Chapter 1 Introduction to Computer and C++ Programming.

Example of SW Design (cont.)

Compute Miles pergallon

Get Input Compute MPG Output

MilesGallonsMPG

MilesGallons

Print Header Print Results

MilesGallonsMPG

Header_Out

Level 0

Level 1

Level 2

Structure ChartExample 1

Error Check

MilesGallons

MilesGallons

Page 26: Chapter 1 Introduction to Computer and C++ Programming.

Example of SW Design (cont.)

YES

YESYES

NO

NO

Get distanceStart

Distance > 0?

Get gallons

Calculate MPG

Output result End

Gallons > 0?

FLOW CHART

Page 27: Chapter 1 Introduction to Computer and C++ Programming.

Introduction to C++ 1. BCPL - Binary Coded Programming Language - System language,

supported integers only - Martin Richard

2. B -evolved from BCPL as a general purpose language, however allows only native (machine dependent) data types- Ken Thompson

3. C - separated data types from machine dependency - Dennis Ritchie in 1970’s. More portable, simpler. Became popular with influx of PC’s. – UNIX - first operating system written in higher level language and thus

portable– C allows access to machine addresses and is sometimes considered a

mid or hybrid language. Dangerous because it allows user to alter operating system.

4. C ++ developed in 1980’s by Bjarne Stroustrup is an extension of C with the inclusion of object-oriented programming. C++ overlaps C.

Page 28: Chapter 1 Introduction to Computer and C++ Programming.

Sample C++ Program//Sample Program#include <iostream>using namespace std;int main (){

int number_of pods, peas_per_pod, total_peas;

cout << “Press return after entering a number.\n”;cout << “Enter the number of pods:\n”;cin >> number_of_pods;cout “Enter the number of peas in a pod:\n”;cin >> peas_per_pods;

total_peas = number_of_pods * peas_per_pod;

cout << “If you have ”;cout << numer_of_pods;cout << “ pea pods\n”;cout << “ and ” << peas_per_pods;cout << “ peas in each pod, then\n”;cout << “you have” << total_peas;cout << “ peas in all the pods.\n”;

return 0;}

Page 29: Chapter 1 Introduction to Computer and C++ Programming.

Sample C++ Program (cont.)//Sample Program#include <iostream>using namespace std;int main (){

int number_of pods, peas_per_pod, total_peas;

cout << “Press return after entering a number.\n”;cout << “Enter the number of pods:\n”;cin >> number_of_pods;cout “Enter the number of peas in a pod:\n”;cin >> peas_per_pods;

total_peas = number_of_pods * peas_per_pod;

cout << “If you have ”;cout << numer_of_pods;cout << “ pea pods\n”;cout << “ and ” << peas_per_pods;cout << “ peas in each pod, then\n”;cout << “you have” << total_peas;cout << “ peas in all the pods.\n”;

return 0;}

Comment:Anything after // is not concerned by the compiler. Include directive:

#include: a preprocessing directive to link this library to this program prior to execution.The <> says look in directory where the compiler is stored first. Later, you will write your own libraries and store them on your disk. Then you will use “file.h” which tells the compiler to look in the current directory first (where your program is stored).iostream: This is a library containing standard I/O functions. C++ is a very powerful language that can do many things. To facilitate the programmer’s job, C++ contains code to do common tasks (such as graphics, input/output, math functions, etc.) and groups these functions in libraries.

C++ divides names into namespaces. The standard language names are in the standard (std) namespace. We need to use this line to use the standard libraries.

Page 30: Chapter 1 Introduction to Computer and C++ Programming.

Sample C++ Program (cont.)//Sample Program#include <iostream>using namespace std;int main (){

int number_of pods, peas_per_pod, total_peas;

cout << “Press return after entering a number.\n”;cout << “Enter the number of pods:\n”;cin >> number_of_pods;cout “Enter the number of peas in a pod:\n”;cin >> peas_per_pods;

total_peas = number_of_pods * peas_per_pod;

cout << “If you have ”;cout << numer_of_pods;cout << “ pea pods\n”;cout << “ and ” << peas_per_pods;cout << “ peas in each pod, then\n”;cout << “you have” << total_peas;cout << “ peas in all the pods.\n”;

return 0;}

Return statement:returns the value computed by the function and control back to the operating system.

int main ()A C++ program is a function that may call other functions to accomplish subtasksmain: is the main program function—it is always used.{} is used to group together the code inside a function. begin-end.

semicolons denote the end of each line in C++.

Variable Declarations (Storing data): sets up storage spaces in the memory to store information and attaches the name (area). The word int tells the computer it will need enough storage space to store an integer number and to use that format.

Output statement:sends output to the standard output device (screen)String constants (in double quotes) are to be printed as is, without the quotes. In example above, “Enter... “ is called a prompt. All user input must be preceded by a prompt to tell the user what is expected.\n advances the cursor to the start of the next line rather than to the next space. Backslash is the escape symbol. It means ignore the usual meaning of the next symbol and go for the alternate.Variables are displayed when named in cout. cout<<total_peas;

Input statement: cin>> variable; stores value typed from standard input device (keyboard) into variable.

Page 31: Chapter 1 Introduction to Computer and C++ Programming.

Compiling and Running a C++ Program

• Source code: The file you type in with the editor.

• Compile and Build: Translates your code into machine language-object code and adds system libraries to your code.– Compile detects syntax errors.– Build detects linking errors

• Run: Links your code to the standard routines and executes.

Page 32: Chapter 1 Introduction to Computer and C++ Programming.

Testing and Debugging

• Syntax errors: improper use of the language. Caught by the compiler. Fatal

• Runtime errors: violations in execution (divide by zero, negative square root) Fatal—program terminates

• Logic errors: mistake in algorithm: non fatal - time consuming

• Warnings: you are doing something that can be done but is usually ill-advised.

• DEBUGGER: a tool to watch a program during execution. Used for runtime and logic errors.