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

Slides:



Advertisements
Similar presentations
1 © 1998, 2001, 2002Calvin College, Keith Vander Linden, Jeremy D. Frens, Larry R. Nyhoff Objects (Chap. 2) Variables and Constants.
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Input & Output: Console
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 Chapter-01 Introduction to Software Engineering.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
1 Chapter-01 Introduction to Software Engineering.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Objects Types, Variables, and Constants 1 Chapter 3.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given.
Chapter 2 Variables.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
1 Chapter-01 Introduction to Software Engineering.
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Objects Types, Variables, and Constants Chapter 3.
A Sample Program #include using namespace std; int main(void) { cout
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
LESSON 2 Basic of C++.
Chapter 2: Introduction to C++
Computing Fundamentals
Basic Elements of C++.
solve the following problem...
Chapter 2: Introduction to C++
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Chapter 2: Introduction to C++.
Unit 3: Variables in Java
Chapter 2 Variables.
Object-Centered Design
The Fundamentals of C++
Programming Fundamental-1
Presentation transcript:

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

Course Description Programming for computer science majors and others. Systematic development of algorithms and programs, programming Style, and design considerations.Programming for computer science majors and others. Systematic development of algorithms and programs, programming Style, and design considerations. Concepts of problem solving, structured programming in C++ programming language, fundamental algorithms and techniques, and computer systems concepts.Concepts of problem solving, structured programming in C++ programming language, fundamental algorithms and techniques, and computer systems concepts. 2

Solving Problems with Computers 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.

How to think computationally Familiar with interacting with a machine but not a human!Familiar with interacting with a 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 have a memory! –Suppose you have a CPU! –Suppose you have peripheral equipment! 4

Objectives and Textbook Main Objectives: To understand basic computer science concepts.To understand basic computer science concepts. To understand a typical C++ program-development environment.To understand a typical C++ program-development environment. To become familiar with data types, arithmetic operators and decision making statements.To become familiar with data types, arithmetic operators and decision making statements.Textbook: Joel Adams and Larry Nyhoff, C++: An Introduction to Computing, 3/e or new versions. Prentice Hall, © 2003 ISBN:

Solve a Problem with a Program Design the program (OCD)Design the program (OCD) –Describe the behavior –Identify the objects –Identify the operations –Describe the algorithm Code the programCode the program Test the programTest the program Maintain/Upgrade the programMaintain/Upgrade the program 6

7

1. Design the Program - Object-Centered Design (OCD) Let’s solve this snow weight problem: Write a program that, given a width and length of a roof, and the depth of the snow, computes the weight of the snow on the roof. 8

OCD 1: Describe the desired behavior of the program: Our program should display a prompt for the length, width, and depth on the screen, read the length, the width, and the depth from the keyboard, compute the corresponding weight, and display the weight, along with a descriptive label on the screen.Our program should display a prompt for the length, width, and depth on the screen, read the length, the width, and the depth from the keyboard, compute the corresponding weight, and display the weight, along with a descriptive label on the screen. 9

OCD 2: Identify the Objects the nouns in the behavioral description: Our program should display a prompt for the length, width, and the depth on the screen, read the length the width, and the depth from the keyboard, compute the corresponding weight, and display the weight, along with a descriptive label on the screen.Our program should display a prompt for the length, width, and the depth on the screen, read the length the width, and the depth from the keyboard, compute the corresponding weight, and display the weight, along with a descriptive label on the screen. 10 These make up the objects in our program.

OCD 3: Identify Operations the verbs in the behavioral description: Our program should display a prompt for the length, width, and the depth on the screen, read the length, width, and the depth from the keyboard, compute the corresponding weight, and display the weight, along with a descriptive label on the screen. 11 These make up the operations in our program.

OCD 4: Describe the Algorithm Organize the objects and operations into a sequence of steps that solves the problem, called an algorithm. 0. Display a prompt for the length, width, the depth on the screen. 1. Read length, width, and depth from the keyboard. 2. Compute weight from length, width and depth. 3. Display weight, plus an informative label on the screen. 12

2. Code the Program Once we have designed an algorithm, the next step is to translate that algorithm into a high level language like C++. This involves figuring out how to –represent our objects, and –perform our operations, in C++ (with the aid of a book, if necessary...) 13

Representing Objects A. Determine a type and name for each object: 14

Performing Operations B. Identify the C++ operator to perform a given operation, if there is one To compute pressure, we need to find the weight formula in a reference book...

Volume-to-Wieght In a reference book, we find that –A cubic meter of water is 1000kg, called weight per cubic meter, or simply wpcm; –A cubic meter of snow can be melted into 1/10 (0.1, called rate) of cube of water. –Volume = length * width * depth Computing the weight thus conduct the operations (*) on the objects (length, width, depth, and 1/10) to our problem... 16

Coding: Program Stub 17 /* weight.cpp is program to compute the weight of the snow on a roof.*/ #include using namespace std; #include int main() { getch(); }

Coding: Declaring Constants 18 /* weight.cpp is program to compute the weight of the snow on a roof.*/ #include using namespace std; #include int main() { const double rate = 0.1, wpcm = 1000; getch(); }

Coding: Algorithm Steps 0 19 /* weight.cpp is program to compute the weight of the snow on a roof.*/ #include using namespace std; #include 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(); }

Coding: Algorithm Steps 1 20 /* weight.cpp is program to compute the weight of the snow on a roof.*/ #include using namespace std; #include 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(); }

Coding: Algorithm Step 2 21 /* weight.cpp is program to compute the weight of the snow on a roof.*/ #include using namespace std; #include 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; }

Coding: Algorithm Step 3 22 /* weight.cpp is program to compute the weight of the snow on a roof.*/ #include 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(); }

3. Testing the program Run your program using sample data (whose correctness is easy to check): 23 Snow Weight Calculator!! Enter the length and the width of the roof, and the depth of snow(meter): The weight of the snow of depth: 2 meters on a roof of :20 meters * 30 meters is: kgs

4. Maintain/Upgrade your program After your program is used, you may get feedbacks from users.After your program is used, you may get feedbacks from users. You may go back to redesign your program and make it better.You may go back to redesign your program and make it better. 24

Objects 25 Variables and Constants

Our Snow Weight Problem /* weight.cpp is program to compute the weight of the snow on a roof.*/ #include #include using namespace std; int main() { const double rate = 0.1, wpcm = 1000; const double rate = 0.1, wpcm = 1000; cout << " \n Snow Weight Calculator!! " cout << " \n Snow Weight Calculator!! " << " \n Enter the length and the width of the roof, and the depth of snow(meter): " ; << " \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 cout << " \nThe weight of the snow of depth: " <<depth << " meters on a roof of : " << length << " meters on a roof of : " << length << " meters * " <<width << " meters * " <<width << " meters is: " <<weight << " meters is: " <<weight << " kgs " <<endl; << " kgs " <<endl;getch();} 26

Expressions In a C++ program, any sequence of objects and operations that combine to produce a value is called an expression. Here is an example from our scuba problem: double weight= length*width*depth*rate*wpcm; Today, we’re going to focus on C++ objects... 27

Object Categories There are three kinds of objects: Literals: unnamed objects having a value (... )Literals: unnamed objects having a value ( 0, -3, 2.5, 2.998e8, 'A', "Hello\n",... ) Variables: named objects whose values can change during program executionVariables: named objects whose values can change during program execution Constants: named objects whose values do not change during program executionConstants: named objects whose values do not change during program execution 28

Literals – literals are whole numbers: –int literals are whole numbers: -27, 0, 4, +4 – literals are real numbers, and can be: –double literals are real numbers, and can be: fixed-point:,...fixed-point: , 0.5, 1.414,... floating-point:,...floating-point: 2.998e8, e9,... –There are just two literals: –There are just two bool literals: false, true – literals are single ASCII characters:... –char literals are single ASCII characters: 'A', 'a', '9', '$', '?',... – literals are ASCII character sequences:,... –string literals are ASCII character sequences: "Hello", "Goodbye", "Goodbye\n",... 29

Variable Declarations Variables are used to store values, and can be either initialized or uninitialized... Examples: int age = 18; double GPA = 3.25, credits; char letterGrade = 'A'; bool ok, done = false; Pattern: Pattern: Type Name [ = Expression ] ; 30

Assignment Statements The value of a variable can be changed using an assignment statement... Examples: age = 19; credits = hours * 3.0; letterGrade = 'B'; done = true; Pattern: Pattern: Name = Expression; 31

Constant Declarations Constants are used to represent a value with a meaningful name, and must be initialized. Examples: const int MAX_SCORE = 100; const double PI = ; const char MIDDLE_INITIAL = 'A'; const string PROMPT = "Enter a number: "; Pattern: Pattern: const Type Name = Expression; 32

Identifiers Technically, the name of an object is called an identifier (it identifies the object). C++ identifiers must begin with a letter (underscores are permitted, but discouraged) followed by zero or more letters, digits or underscores. Valid:,... Valid: age, r2d2, myGPA, MAX_SCORE,... Invalid:,... Invalid: 123go, coffee-time, sam’s, $name,... To be continued. 33

Conventions To keep variable and constant objects distinct: Constant names are all uppercase, with multiple words separated by underscores (e.g., )Constant names are all uppercase, with multiple words separated by underscores (e.g., MAX_SCORE ) Variable names are all lowercase, with the first letter of each word after the first capitalized (e.g., )Variable names are all lowercase, with the first letter of each word after the first capitalized (e.g., lastName ) 34

Objects char Objects... are represented in memory by a code –ASCII code uses 8 bits to represent a character, allowing for 2 8 = 256 different characters. –Unicode uses 16 bits to represent a character, allowing for 2 16 = 65,536 different characters. ASCII is the most commonly used code: '0' = 48 = 'A' = 65 = 'a' = 97 =

Escape Characters C++ provides a number of escape characters: '\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

Objects int Objects Three forms: –decimal (base-10): begin with a non-zero or sign (-45, -2, 0, +21, 36, 65536,...) –octal (base-8): a zero followed by digits (01, 02, 03, 04, 05, 06, 07, 010, 011, 012,...) –hexadecimal (base-16): zero-x followed by digits with a, b, c, d, e, f = 10, 11, 12, 13, 14, 15 (0x1, 0x2,..., 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11,...) 37

Summary Writing a program consists of these steps: 1. Design an algorithm for your program. 2. Code your design. 3. Test your program. 4. Maintain/upgrade your program as necessary. OCD is a methodology for designing programs: 1. Describe the desired behavior of the program. 2. Identify the objects required. 3. Identify the operations required. 4. Organize objects and operations into an algorithm, refining object and operation lists as necessary. 38

Summary (iii) C++ provides three kinds of objects: literals, variables and constants. Literals have a “built-in” type; a declaration statement is the means by which a type is associated with a variable or constant. The C++ fundamental types include,,,,,,,, and. The C++ fundamental types include bool, char, int, short, long, unsigned, float, double, and long double. 39

My understanding of Education 40 Knowledge StudentsProfessor Learn and Create Transfer Learn

My understanding of Knowledge Transfer 41

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