© 2000 Scott S Albert Problem Solving Using C++ Structured Programming 256 Chapter 2.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
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/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
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++.
JavaScript, Third Edition
Basic Elements of C++ Chapter 2.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
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.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
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 Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
elementary programming
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
The Fundamentals of C++
Presentation transcript:

© 2000 Scott S Albert Problem Solving Using C++ Structured Programming 256 Chapter 2

© 2000 Scott S Albert C++ and Other Computer Languages C++ has many similarities to other computer languages –Very similar to Java and Smalltalk Shares many concepts with –Visual Basic, Delphi Inherits many characteristics from –Pascal, PL/1, C, Algol, Fortran, Cobol

© 2000 Scott S Albert Variables Used to store data during program execution –Results of calculations –Temporary values –Control information Can hold many different types of data –Numbers, characters, strings of characters and objects ( collections of data and programs )

© 2000 Scott S Albert What is a program variable? A named location to store data –a container for data It can hold only one type of data –for example only integers, only floating point (real) numbers, or only characters

© 2000 Scott S Albert Post Office Metaphor Post Offices contain a section of PO boxes –Boxes are like memory. Available to store all kinds of information. Boxes are allocated, assigned a name (number). Computer memory –Can store any type of data (numbers, strings etc.) –Available for allocation and assignment

© 2000 Scott S Albert Creating variables All program variables must be declared before using them A variable declaration associates a name with a storage location in memory and specifies the type of data it will store: Type Variable_1, Variable_2, …; For example, to create three integer variables to store the number of baskets, number of eggs per basket, and total number of eggs: int numberOfBaskets, eggsPerBasket, totalEggs;

© 2000 Scott S Albert Variables Variables store data in a program Variables must be declared (or defined) before they are used –Variable declarations Variable type Variable name (identifier) –Example int nPosition, nHeight, nWidth; char cAnswer;

© 2000 Scott S Albert Variable Names Naming rules –Identifiers must start with a letter, may contain letters or numbers or _ –There is essentially no limit to the length of an identifier Variable name should match its use –Use dName rather than a –Use nHeight rather than x

© 2000 Scott S Albert Variable Types Primitive types –Basic building blocks of the C++ language. Match intrinsic hardware capabilities. Class types –Contain: One or more primitive types Plus associated C++ statements to manage the primitive types Possibly even other class types

© 2000 Scott S Albert Primitive Types shortinteger1 byte intinteger2 bytes unsignedinteger4 bytes longinteger8 bytes floatreal4 bytes doublereal8 bytes charcharacter2 bytes boollogical1 bit/1 byte Use the sizeof() operator for a particular system

© 2000 Scott S Albert Primitive Data Types

© 2000 Scott S Albert Primitive Types Integer –Signed whole numbers only (no decimal) Real (Scientific Notation) –Signed numbers with two portions An exponent A mantissa with a fixed precision Char –ASCII character

© 2000 Scott S Albert Integer vs Real Numbers Why two different systems? –Clearly the range of integers fit into the range of real numbers Integers are computationally very efficient –If you can use integers, do so your program will run much faster Integers are precise –By their nature, real numbers often have very small errors that creep into their representation. Integers do not have this issue.

© 2000 Scott S Albert Variable names: Identifiers Rules - these must be obeyed all C++ identifiers must follow the same rules must not start with a digit must contain only numbers, letters, underscore (_) and $ (but avoid using $, it is reserved for special purposes) names are case-sensitive (ThisName and thisName are two different variable names) Good Programming Practice - these should be obeyed always use meaningful names from the problem domain (for example, eggsPerBasket instead of n, which is meaningless, or count, which is not meaningful enough) start variable names with lower case capitalize interior words (use eggsPerBasket instead of eggsperbasket ) avoid using $ since it is reserved for special purposes

© 2000 Scott S Albert Two main kinds of types in C++ primitive data types the simplest types cannot decompose into other types values only, no methods Examples: int - integer double - floating point (real) char - character class types more complex composed of other types (primitive or class types) both data and methods

© 2000 Scott S Albert Syntax Templates Efficient method to communicate the grammar of a computer language –Similar to a sentence diagram –Indicates the required and optional portions of a statement.

© 2000 Scott S Albert Assignment Statements Syntax template Variable = Expression ; Operation –The expression on the right hand side is evaluated and assigned to the memory location named by the variable on the left hand side.

© 2000 Scott S Albert Expressions Simple expression –Variable Arithmetic expressions –Binary operators (require two values) + - * / % –Unary operators (only one value needed)

© 2000 Scott S Albert Expression Examples int nPosition, nHeight, nWidth; nPosition = 0; nHeight = 5; nWidth = nHeight; nHeight = nHeight * 2; nPosition = nHeight + nWidth * 3 + 5; float nXPos, nYPos; nXPos = 2.3; nYPos = nXPos / 0.3 ; nHeight = -nWidth; nHeight = nWidth * -nPosition;

© 2000 Scott S Albert Additional Arithmetic Operators Increment and Decrement –++ will increment a variable by 1 –-- will decrement a variable by 1 –A unary operator that can be prefix or postfix Modulus operator % –Integer division yields an integer result –% Returns the remainder of an integer division

© 2000 Scott S Albert Samples int nSomeNumber, nQuotient, nRemainder; nSomeNumber = 17; nQuotient = nSomeNumber / 3; nRemainder = nSomeNumber % 3; float nAnswer; nAnswer = nSomeNumber / 3.0; nSomeNumber++; nQuotient = 7; nSomeNumber = nQuotient ++; nSomeNumber = ++nQuotient ;

© 2000 Scott S Albert Samples void main() { cout<<"Hello World”<<endl; int nSomeNumber, nQuotient, nRemainder; nSomeNumber = 17; nQuotient = 7; nSomeNumber = nQuotient++; cout<<"nQuotient postfix is " << nQuotient<<endl; cout<<"nSomeNumber postfix is " << nSomeNumber<<endl; nSomeNumber = ++nQuotient ; cout<<"nQuotient prefix is " +<<nQuotient<<endl; cout<<"nSomeNumber prefix is " << nSomeNumber<<endl; }

© 2000 Scott S Albert Examples to run int nCount = 0; int nValue = 0; nValue = nCount++; cout<<"NCount is"+nCount+"nValue is ”<< nValue<<endl; nValue = ++nCount; cout<<"NCount is "+nCount+" nValue is ”<< nValue<<endl; nCount++; ++nCount; nValue = nValue + 1; nCount = nValue--; nCount = nValue - 1;

© 2000 Scott S Albert Prefix and Postfix A prefix ++ or -- is evaluated (executed) first and the new value is available in the current expression A postfix ++ or -- is evaluated (executed) after the expression has been evaluated therefore the new value IS NOT available in the current expression

© 2000 Scott S Albert Precedence Rules How is the following expression evaluated? –nPosition = nHeigth / nWidth + 5 * 3; Why? Precedence rules (introduction) –First unary operators +, -, ++, -- –Next binary operators *, /, % –Then binary operators +, - –Unless there are parenthesis ( )

© 2000 Scott S Albert Precedence Exercise int nCount = 0; int nValue = 0; nValue = – 3 – 4; nValue = 16 * 4 / 2 * 3; nValue = 7 – 3 * 4 + 2; nValue = 6 * -nCount + 5; nValue = (6 * (-nCount)) + 5;

© 2000 Scott S Albert Conversion between types Implicit conversion –C++ will automatically convert some types. This conversion is often call promotion since the conversion always moves to a more expansive type. –short  int  long  float  double –Any type can be promoted to a type on its right byte can be promoted to long float can not be promoted to short

© 2000 Scott S Albert Sample int foo; double foobar; foobar = 2.5; foo = foobar; cout<<"foobar is " << foobar;

© 2000 Scott S Albert Conversion between types Explicit conversion C++ allows the programmer to explicitly specify a conversion though a cast –This allows a conversion that would not be done through promotion, but should be used with caution. Values are truncated (not rounded) without warning. Special issues when casting characters to numbers

© 2000 Scott S Albert Key escape characters Double quote –\” Single quote –\’ New line –\n Carriage return –\r Tab –\t Backslash –\\

© 2000 Scott S Albert Console I/O Input from the keyboard, output to a “console” window. –Not Windows programming per se –Rather a rudimentary input and output mechanism useful for simple programs where we don’t want to spend a great deal of time on the user interface No mouse, graphics, multiple windows etc.

© 2000 Scott S Albert Output Cout<< –Outputs to the console window Use endl or \n for a new line

© 2000 Scott S Albert Input Methods Cin>>

© 2000 Scott S Albert A Matter of Programming Style Using a good programming style is essential –Allows you to communicate your design to team –Makes your program more easy to modify later –Communicates a clean and professional message Most programming organizations have development standards –Variable names, class names, comments etc.

© 2000 Scott S Albert Style Pointers Use meaningful names for variables and all identifiers –I often use a prefix to the variable that communicates its type. This is optional. Use comments to explain the program –Comments are ignored by the compiler, rather they are used to communicate with other programmers (or yourself).

© 2000 Scott S Albert Comments Two types of comments –// -- everything after a // is ignored by the compiler to the end of the line –/* -- everything after a /* is ignored by the compiler until a */ is reached

© 2000 Scott S Albert Style Pointers Indenting –Indenting your source code makes it easier to see the beginning and ending of blocks. Named constants –Rather than using values like 100 as a maximum number, use a named constant of MAXNUMBER. –Benefits – easier to understand, and most importantly, easier to modifier later.

© 2000 Scott S Albert Named Constants Named constants are of type –const Normally there names are all upper case Examples const int MAXNUMBER = 100; const float MINTEMP = -40.0;

© 2000 Scott S Albert Summary We are starting to see the major foundations of the C++ language –Basic types Conversion between types –Assignment statements –Arithmetic expressions –Etc.