COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science...

42
COSC1557: Introduction to Computing •Haibin Zhu, PhD. •Professor •Department of Computer Science •Nipissing University •(C) 2014

Transcript of COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science...

Page 1: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

COSC1557: Introduction to Computing

•Haibin Zhu, PhD.•Professor•Department of Computer Science•Nipissing University•(C) 2014

Page 2: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Course DescriptionCourse Description•Programming for computer science majors Programming for computer science majors and others. Systematic development of and others. Systematic development of algorithms and programs, programming algorithms and programs, programming Style, and design considerations.Style, and design considerations.

•Concepts of problem solving, structured Concepts of problem solving, structured programming in C++ programming programming in C++ programming language, fundamental algorithms and language, fundamental algorithms and techniques, and computer systems techniques, and computer systems concepts. concepts.

2

Page 3: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Solving Problems with Solving Problems with ComputersComputers

3

Writing a program:1. Design an algorithm for your program

(Computational Thinking).2. Code your design.(To tell the computer, or another guy, how

to work!)3. Test your program.4. Maintain/upgrade your program as

necessary.

Page 4: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

How to think How to think computationallycomputationally• Familiar with interacting with a Familiar with interacting with a

machine but not a human!machine but not a human!

• Understand the machine.Understand the machine.

• Make you a machine!Make you a machine!– Suppose you are a computer!Suppose you are a computer!

– Suppose you have a memory!Suppose you have a memory!

– Suppose you have a CPU!Suppose you have a CPU!

– Suppose you have peripheral Suppose you have peripheral equipment!equipment! 4

Page 5: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Objectives and TextbookObjectives and TextbookMain Objectives:Main Objectives:

• To understand basic computer science concepts.To understand basic computer science concepts.

• To understand a typical C++ program-To understand a typical C++ program-development environment.development environment.

• To become familiar with data types, arithmetic To become familiar with data types, arithmetic operators and decision making statements. operators and decision making statements.

Textbook:Textbook:

Joel Adams and Larry Nyhoff, C++: An Introduction to Computing, 3/e or new versions. Prentice Hall, © 2003 ISBN: 0-13-091426-6.

5

Page 6: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Solve a Problem with a Solve a Problem with a ProgramProgram• Design the program (OCD)Design the program (OCD)

– Describe the behaviorDescribe the behavior

– Identify the objectsIdentify the objects

– Identify the operationsIdentify the operations

– Describe the algorithmDescribe the algorithm

• Code the programCode the program

• Test the programTest the program

• Maintain/Upgrade the programMaintain/Upgrade the program6

Page 7: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

7

Page 8: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

1. Design the Program - 1. Design the Program - Object-Centered Design (OCD)Object-Centered Design (OCD)

Let’s solve this Let’s solve this snow weight problemsnow weight problem::

Write a program that, given a width and length of a Write a program that, given a width and length of a roof, and the depth of the snow, computes the roof, and the depth of the snow, computes the weight of the snow on the roof.weight of the snow on the roof.

8

Page 9: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

OCD 1: Describe the OCD 1: Describe the desired behavior of the desired behavior of the program:program:• Our program should display a prompt for the Our program should display a prompt for the

length, width, and depth on the screen, read length, width, and depth on the screen, read the length, the width, and the depth from the the length, the width, and the depth from the keyboard, compute the corresponding keyboard, compute the corresponding weight, and display the weight, along with a weight, and display the weight, along with a descriptive label on the screen.descriptive label on the screen.

9

Page 10: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

OCD 2: Identify the OCD 2: Identify the ObjectsObjects

the the nounsnouns in the behavioral description: in the behavioral description:

• Our program should display a prompt for the length , Our program should display a prompt for the length , width, and the depth on the screen, read the length width, and the depth on the screen, read the length the width, and the depth from the keyboard, the width, and the depth from the keyboard, compute the corresponding weight, and display the compute the corresponding weight, and display the weight, along with a descriptive label on the screen.weight, along with a descriptive label on the screen.

10

These make up the These make up the objectsobjects in our program. in our program.

Page 11: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

OCD 3: Identify OCD 3: Identify OperationsOperations

the the verbsverbs in the behavioral description: in the behavioral description:

Our program should display a prompt for the Our program should display a prompt for the length, width, and the depth on the screen, read length, width, and the depth on the screen, read the length, width, and the depth from the the length, width, and the depth from the keyboard, compute the corresponding weight, keyboard, compute the corresponding weight, and display the weight, along with a descriptive and display the weight, along with a descriptive label on the screen.label on the screen.

11

These make up the These make up the operationsoperations in our program. in our program.

Page 12: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

OCD 4: Describe the OCD 4: Describe the AlgorithmAlgorithm

Organize the objects and operations into a Organize the objects and operations into a sequence of steps that solves the problem, sequence of steps that solves the problem, called an called an algorithmalgorithm..

0. Display a prompt for 0. Display a prompt for the length, width, the depth the length, width, the depth on the on the screenscreen..

1. Read length, width, and 1. Read length, width, and depthdepth from the from the keyboardkeyboard..

2. Compute 2. Compute weight weight from from length, width and depthlength, width and depth..

3. Display 3. Display weightweight, plus an informative , plus an informative labellabel on the on the screenscreen..

12

Page 13: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

2. Code the Program2. Code the Program

Once we have designed an algorithm, the Once we have designed an algorithm, the next step is to translate that algorithm next step is to translate that algorithm into a high level language like C++.into a high level language like C++.

This involves figuring out how to This involves figuring out how to

– represent our objects, andrepresent our objects, and

– perform our operations,perform our operations,

in C++ (with the aid of a book, if in C++ (with the aid of a book, if necessary...)necessary...)

13

Page 14: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Representing ObjectsRepresenting Objects

A. Determine a type and name for each object:A. Determine a type and name for each object:

14

Object C++ Type Name

a prompt the length the width the depth the screen the keyboard the weight a label

string double double double ostream istream double string

-- length width depth cout cin weight --

Page 15: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Performing OperationsPerforming OperationsB. Identify the C++ operator to perform B. Identify the C++ operator to perform

a given operation, if there is one...a given operation, if there is one...

15

Operation Library? Name

Display a string Read length, width, depth(doubles) Compute weight Display weight (a double)

iostream iostream -- iostream

<< >> -- <<

To compute pressure, we need to find the To compute pressure, we need to find the weight formula in a reference book...weight formula in a reference book...

Page 16: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Volume-to-WieghtVolume-to-Wieght

In a reference book, we find thatIn a reference book, we find that– A cubic meter of water is 1000kg, called A cubic meter of water is 1000kg, called

weight per cubic meter, or simply wpcm;weight per cubic meter, or simply wpcm;

– A cubic meter of snow can be melted into A cubic meter of snow can be melted into 1/10 (0.1, called rate) of cube of water.1/10 (0.1, called rate) of cube of water.

– Volume = length * width * depthVolume = length * width * depth

Computing the weight thus conduct the Computing the weight thus conduct the operations (*) on the objects (length, operations (*) on the objects (length, width, depth, and 1/10) to our problem...width, depth, and 1/10) to our problem... 16

Page 17: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Coding: Program StubCoding: Program Stub

17

/* weight.cpp is program to compute the weight /* weight.cpp is program to compute the weight of the snow on a roof.*/of the snow on a roof.*/

#include <iostream>using namespace std;#include <conio.h>

int main(){getch();}

Page 18: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Coding: Declaring Coding: Declaring ConstantsConstants

18

/* weight.cpp is program to compute the weight of the snow /* weight.cpp is program to compute the weight of the snow on a roof.*/on a roof.*/

#include <iostream>using namespace std;#include <conio.h>int main(){

const double rate = 0.1, wpcm = 1000; getch();}

Page 19: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Coding: Algorithm Steps 0Coding: Algorithm Steps 0

19

/* weight.cpp is program to compute the weight of the /* weight.cpp is program to compute the weight of the snow on a roof.*/snow on a roof.*/#include <iostream>using namespace std;#include <conio.h>int main(){

const double rate = 0.1, wpcm = 1000; cout << "\n Snow Weight Calculator!! " << "\n Enter the length and the width of the roof, and the depth of snow(meter):"; getch();}

Page 20: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Coding: Algorithm Steps 1Coding: Algorithm Steps 1

20

/* weight.cpp is program to compute the weight of the snow /* weight.cpp is program to compute the weight of the snow on a roof.*/on a roof.*/#include <iostream>using namespace std;#include <conio.h>int main(){ const double rate = 0.1, wpcm = 1000; cout << "\n Snow Weight Calculator!! " << "\n Enter the length and the width of the

roof, and the depth of snow(meter):";double length, width, depth;cin >> length;cin >> width;cin >> depth;

getch();}

Page 21: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Coding: Algorithm Step 2Coding: Algorithm Step 2

21

/* weight.cpp is program to compute the weight of the snow on /* weight.cpp is program to compute the weight of the snow on a roof.*/a roof.*/

#include <iostream>using namespace std;#include <conio.h>int main(){ const double rate = 0.1, wpcm = 1000; cout << "\n Snow Weight Calculator!! " << "\n Enter the length and the width of the

roof, and the depth of snow(meter):";double length, width, depth;cin >> length;cin >> width;cin >> depth;

double weight=length*width*depth*rate*wpcm;

}

Page 22: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Coding: Algorithm Step 3Coding: Algorithm Step 3

22

/* weight.cpp is program to compute the weight of the snow on a roof.*//* weight.cpp is program to compute the weight of the snow on a roof.*/#include <iostream>#include <conio.h>using namespace std;int main(){

const double rate = 0.1, wpcm = 1000; cout << "\n Snow Weight Calculator!! " << "\n Enter the length and the width of the roof, and the depth of

snow(meter):";double length, width, depth;cin >> length;cin >> width;cin >> depth;double weight=length*width*depth*rate*wpcm;

cout << "\nThe weight of the snow of depth: "<<depth << " meters on a roof of :" << length << " meters * " <<width

<< " meters is: "<<weight << " kgs " <<endl;

getch();

}

Page 23: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

3. Testing the program3. Testing the programRun your program using sample data (whose correctness is easy to Run your program using sample data (whose correctness is easy to

check):check):

23

Snow Weight Calculator!! Enter the length and the width of the roof, and the depth of snow(meter):20 30 2

The weight of the snow of depth: 2 meters on a roof of :20 meters * 30 meters is:

120000 kgs

Page 24: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

4. Maintain/Upgrade your 4. Maintain/Upgrade your programprogram

• After your program is used, you After your program is used, you may get feedbacks from users.may get feedbacks from users.

• You may go back to redesign your You may go back to redesign your program and make it better.program and make it better.

24

Page 25: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

ObjectsObjects

25

Variables and Constants

Page 26: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Our Snow Weight ProblemOur Snow Weight Problem/* weight.cpp is program to compute the weight of the snow on a /* weight.cpp is program to compute the weight of the snow on a

roof.*/roof.*/#include <iostream>#include <iostream>#include <conio.h>#include <conio.h>using namespace std;using namespace std;int main()int main(){{

const double rate = 0.1, wpcm = 1000;const double rate = 0.1, wpcm = 1000; cout << cout << ""\n Snow Weight Calculator!!\n Snow Weight Calculator!! " " << << ""\n Enter the length and the width of the roof, and the \n Enter the length and the width of the roof, and the

depth of snow(meter):depth of snow(meter):"";;double length, width, depth;double length, width, depth;cin >> length;cin >> length;cin >> width;cin >> width;cin >> depth;cin >> depth;double weight=length*width*depth*rate*wpcm;double weight=length*width*depth*rate*wpcm;

cout << cout << ""\nThe weight of the snow of depth: \nThe weight of the snow of depth: ""<<depth<<depth << << "" meters on a roof of : meters on a roof of :"" << length << length << << " meters * "" meters * " <<width <<width

<<<< " " meters is: meters is: ""<<weight<<weight <<<< " " kgskgs " " <<endl;<<endl;

getch();getch();}}

26

Page 27: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

ExpressionsExpressions

In a C++ program, any sequence of In a C++ program, any sequence of objectsobjects and and operationsoperations that combine to that combine to produce a value is called an produce a value is called an expressionexpression..

Here is an example from our scuba Here is an example from our scuba problem:problem: double weight= length*width*depth*rate*wpcm;

Today, we’re going to focus on C++ Today, we’re going to focus on C++ objectsobjects...... 27

Page 28: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Object CategoriesObject Categories

There are three kinds of objects:There are three kinds of objects:

• LiteralsLiterals: unnamed objects having a : unnamed objects having a value value ((0, -3, 2.5, 2.998e8, 'A', "Hello\n", ... ...))

• VariablesVariables: named objects whose values : named objects whose values can change during program executioncan change during program execution

• ConstantsConstants: named objects whose values : named objects whose values do not change during program do not change during program executionexecution 28

Page 29: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

LiteralsLiterals

– int literals are whole numbers: literals are whole numbers: -27, 0, 4, +4

– double literals are real numbers, and can be: literals are real numbers, and can be:• fixed-point: fixed-point: -0.333, 0.5, 1.414, ..., ...

• floating-point: floating-point: 2.998e8, 0.2998e9, ..., ...

– There are just twoThere are just two bool literals: literals: false, true

– char literals are single ASCII characters: literals are single ASCII characters: 'A', 'a', '9', '$', '?', ... ...

– string literals are ASCII character sequences: literals are ASCII character sequences: "Hello", "Goodbye", "Goodbye\n", ..., ...

29

Page 30: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Variable DeclarationsVariable Declarations

Variables are used to store values, and can Variables are used to store values, and can be either be either initializedinitialized or or uninitializeduninitialized......

Examples:Examples:int age = 18;

double GPA = 3.25, credits;

char letterGrade = 'A';

bool ok, done = false;

Pattern: Pattern: Type Name [ = Expression ] ;30

Page 31: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Assignment StatementsAssignment Statements

The value of a variable can be changed The value of a variable can be changed using an assignment statement...using an assignment statement...

Examples:Examples:age = 19;

credits = hours * 3.0;

letterGrade = 'B';

done = true;

Pattern: Pattern: Name = Expression; 31

Page 32: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Constant DeclarationsConstant Declarations

Constants are used to represent a value with Constants are used to represent a value with a meaningful name, and a meaningful name, and must be initializedmust be initialized..

Examples:Examples:const int MAX_SCORE = 100;

const double PI = 3.14159;

const char MIDDLE_INITIAL = 'A';

const string PROMPT = "Enter a number: ";

Pattern:Pattern: const Type Name = Expression;32

Page 33: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

IdentifiersIdentifiers

Technically, the name of an object is called Technically, the name of an object is called an an identifieridentifier (it identifies the object). (it identifies the object).

C++ identifiers must begin with a letter C++ identifiers must begin with a letter (underscores are permitted, but (underscores are permitted, but discouraged) followed by zero or more discouraged) followed by zero or more letters, digits or underscores.letters, digits or underscores.

Valid: Valid: age, r2d2, myGPA, MAX_SCORE,...,...

Invalid: Invalid: 123go, coffee-time, sam’s, $name,...,...

To be continued. 33

Page 34: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

ConventionsConventions

To keep variable and constant objects To keep variable and constant objects distinct:distinct:

• Constant names are all uppercase, Constant names are all uppercase, with multiple words separated by with multiple words separated by underscores (e.g., underscores (e.g., MAX_SCORE))

• Variable names are all lowercase, with Variable names are all lowercase, with the first letter of each word after the the first letter of each word after the first capitalized (e.g., first capitalized (e.g., lastName))

34

Page 35: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

char Objects Objects

... are represented in memory by a ... are represented in memory by a codecode– ASCII code uses 8 bits to represent a ASCII code uses 8 bits to represent a

character, allowing for 2character, allowing for 288 = 256 different = 256 different characters.characters.

– Unicode uses 16 bits to represent a character, Unicode uses 16 bits to represent a character, allowing for 2allowing for 21616 = 65,536 different characters. = 65,536 different characters.

ASCII is the most commonly used code:ASCII is the most commonly used code:'0' = 48 = 00110000

'A' = 65 = 01000001

'a' = 97 = 0110000135

Page 36: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Escape CharactersEscape Characters

C++ provides a number of C++ provides a number of escape escape characterscharacters::'\n' newline character

'\t' horizontal tab

'\v' vertical tab

'\f' form feed

'\a' alert/bell

'\\' backslash char

'\'' apostrophe

'\"' double quote

'\xdd' char with hex code |

36

Page 37: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

int Objects Objects

Three forms:Three forms:– decimaldecimal (base-10): begin with a non-zero or (base-10): begin with a non-zero or

sign (-45, -2, 0, +21, 36, 65536, ...)sign (-45, -2, 0, +21, 36, 65536, ...)

– octaloctal (base-8): a zero followed by digits (base-8): a zero followed by digits (01, 02, 03, 04, 05, 06, 07, 010, (01, 02, 03, 04, 05, 06, 07, 010, 011, 012, ...)011, 012, ...)

– hexadecimalhexadecimal (base-16): zero-x followed by (base-16): zero-x followed by digits with a, b, c, d, e, f = 10, 11, 12, 13, digits with a, b, c, d, e, f = 10, 11, 12, 13, 14, 15 (0x1, 0x2, ..., 0x7, 0x8, 0x9, 0xa, 14, 15 (0x1, 0x2, ..., 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, ...)0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, ...) 37

Page 38: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

SummarySummaryWriting a program consists of these steps:Writing a program consists of these steps:

1. Design an algorithm for your program.1. Design an algorithm for your program.2. Code your design.2. Code your design.3. Test your program.3. Test your program.4. Maintain/upgrade your program as necessary.4. Maintain/upgrade your program as necessary.

OCD is a methodology for designing OCD is a methodology for designing programs:programs:

1. Describe the desired 1. Describe the desired behaviorbehavior of the program. of the program.2. Identify the 2. Identify the objectsobjects required. required.3. Identify the 3. Identify the operationsoperations required. required.4. Organize objects and operations into an 4. Organize objects and operations into an

algorithmalgorithm, refining object and operation lists as , refining object and operation lists as necessary.necessary.

38

Page 39: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Summary (iii)Summary (iii)

C++ provides three kinds of objects: C++ provides three kinds of objects: literals, variables and constants.literals, variables and constants.

Literals have a “built-in” type; a declaration Literals have a “built-in” type; a declaration statement is the means by which a type statement is the means by which a type is associated with a variable or constant.is associated with a variable or constant.

The C++ fundamental types include The C++ fundamental types include bool, , char, , int, , short, , long, , unsigned, , float, , double, and , and long double..

39

Page 40: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

My understanding of My understanding of EducationEducation

40

Knowledge

Students Professor

Learn and Create

Transfer

Learn

Page 41: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

My understanding of My understanding of Knowledge TransferKnowledge Transfer

41

Page 42: COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.

Shared Models Shared Models

• Basic logic thinking abilityBasic logic thinking ability

• Basic descriptive abilityBasic descriptive ability

• Basic knowledge to use a computerBasic knowledge to use a computer

• And basic math:And basic math:

• Placement test!Placement test!

42