2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
Lecture 2 Introduction to C Programming
Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Engineering Problem Solving with C++ An Object Based Approach Chapter 2 Simple C++ Programs.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 3: Input/Output
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Simple C++ Programs.
Due Dates Quiz 1, 2 : Friday September 11 th Quiz 3 : Friday September 18 th Quiz 4 : Monday September 28 th Lab 1, 2 and 3 : Friday Sep 11 th Project.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Instructor - C. BoyleFall Semester
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Basic Elements of C++ Chapter 1.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Simple C++ Programs Program Structure Constants and Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Recap……Last Time [Variables, Data Types and Constants]
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Bill Tucker Austin Community College COSC 1315
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++ Chapter 1.
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs

4 Program Structure 4 Constants and Variables 4 C++ Operators 4 Standard Input and Output 4 Basic Functions from the C++ Standard Library 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 2

2/4/2016Engineering Problem Solving with C++ second edition, J. ingber 3 The C++ Programming Language § A superset of C §C++ compilers can be used to compile C programs § Supports §Programmer Defined Types §Templates §Overloading of functions and operators § Best to think of C++ as its own language

PROGRAM STRUCTURE Comments Compiler Directives Using Directive Blocks of Code 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 4

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 5 /* Simple C++ Program */ /* This program computes the */ /* distance between two points. */ #include // Required for cout, endl. #include // Required for sqrt() using namespace std; int main() { // Declare and initialize objects. double x1(1), y1(5), x2(4), y2(7), side1, side2, distance; // Compute sides of a right triangle. side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1*side1 + side2*side2); // Print distance. cout << "The distance between the two points is " << distance << endl; // Exit program. return 0; }

2/4/2016Engineering Problem Solving with C++ second edition, J. ingber 6 Program structure Comments preprocessor directives using directives Block of code { comments statements }

2/4/2016Engineering Problem Solving with C++ second edition, J. ingber 7 Comments § Comments help people read programs, but are ignored by the compiler. § In C++ there are two types of comments. §Line comments begin with // and continue for the rest of the line. §Delimited comments begin with /* and end with */

2/4/2016Engineering Problem Solving with C++ second edition, J. ingber 8 Preprocessor Directives § Provide instructions to the compiler that are performed before the program is compiled. § Begin with a # § Example: #include The #include directive instructs the compiler to include statements from the file iostream.

using Directive  The using directive instructs the compiler to use files defined a specified namespace. 4 Example: using namespace std; std is the name of the Standard C++ namespace. 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 9

Block of Code 4 A block of code is defined by a set of curly braces {…}. 4 Example: int main() { //Block defines body of main function double x1(1), x2(4), side1; side1 = x2 – x1; cout << side1 << endl; return 0; // main()returns int value of 0 } //end definition of main  Every C++ problem solution contains exactly one function named main().  C++ program solutions always begin execution in main () 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 10

CONSTANTS AND VARIABLES Identifiers Memory Snapshots 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 11

Constants and Variables 4 Constants and variables represent memory locations that we reference in our program solutions. 4 Constants are objects that store specific data that can not be modified. –10 is an integer constant –4.5 is a floating point constant –"Side1" is a string constant –'a' is a character constant 4 Variables are memory locations that store values that can be modified. –double x1(1.0), x2(4.5), side1; –side1 = x2-x1; –x1, x2 and side1 are examples of variables that can be modified. 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 12

Memory Snapshot 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 13 #include using namespace std; int main() { double x1(1.0), x2(4.5), side1; double x1 double x2 double side1 side1 = x2-x1; double x1 double x2 double side1 cout << “side 1 has length: “ side1; ? Output: side 1 has length: 3.5

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 14 Common C++ Data Types KeywordExample of a constant booltrue char'5' int25 double25.0 string"hello" // #include

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 15 C++ Identifiers 4 Identifiers are used to name objects in C++. 4 Rules for selecting valid identifiers: –Identifier must begin with a letter or underscore _ –Identifiers consists of alphanumeric characters and underscore character only. –An identifier can not be a reserved word. –Only the first 31 characters of an identifier are used to distinguish it from other identifiers.  C++ is case sensitive. Thus, Side1 and side1 are unique identifiers that represent different objects.

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 16 Declarations 4 A type declaration statement defines new identifiers and allocates memory. 4 An initial value may be assigned to a memory location at the time an identifier is defined. Syntax: [modifier] data_type identifier_list; Examples: double length( 20.75), width(11.5), volume; int numberOfFeetInYard(3); const int MIN_SIZE = 0;

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 17 Symbolic Constants  A symbolic constant is defined in a declaration statement using the modifier const. 4 A symbolic constant allocates memory for an object that can not be modified during execution of the program. Any attempt to modify a constant will be flagged as a syntax error by the compiler. 4 A symbolic constant must be initialized in the declaration statement.

C++ OPERATORS Assignment Operators Arithmetic Operators Precedence of Operators 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 18

Assignment Operator 4 The assignment operator (=) is used in C++ to assign a value to a memory location. 4 The assignment statement: x1 = 1.0; –assigns the value 1.0 to the variable x1. –Thus, the value 1.0 is stored in the memory location associated with the identifier x1. 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 19

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 20 4 Example 1 - initialization double sum = 0; sum 4 Example 2 int x; x=5; x Assignment Operators - Examples 0 5 a 4 Example 3 char ch; ch = 'a'; ch

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 21 Assignment Statements - continued 4 Example 3 int x, y, z; x=y=0; z=2; x y z 4 How is the memory map affected by the following statement? y = z; 0 0 2

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 22 Arithmetic Operators 4 Addition+ 4 Subtraction- 4 Multiplication* 4 Division/ 4 Modulus% –Modulus returns remainder of division between two integers –Example 5%2 returns a value of 1

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 23 Integer Division 4 Division between two integers results in an integer. 4 The result is truncated, not rounded 4 Example: The expression 5/3 evaluates to 1 The expression 3/6 evaluates to 0

C++ Operators 4 The cast operator. –The cast operator is a unary operator that requests that the value of the operand be cast, or changed, to a new type for the next computation. The type of the operand is not affected. 4 Example: int count(10), sum(55); double average; average = (double)sum/count; 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 24 Memory snapshot: int count int sum double average

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 25 Priority of Operators 1.ParenthesesInner most first 2.Unary operatorsRight to left (+ -) 3.Binary operatorsLeft to right (* / %) 4.Binary operatorsLeft to right (+ -)

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 26 Practice! – Evaluate Expressions * 5 – / % 3 * 6 4 (7 + 3) * 5 - 2

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 27 Overflow and Underflow 4 Overflow –answer too large to store Example: using 16 bits for integers result = ; 4 Exponent overflow –answer’s exponent is too large Example: using float, with exponent range –38 to 38 result = 3.25e28 * 1.0e15; 4 Exponent underflow –answer’s exponent too small Example: using float, with exponent range –38 to 38 result = 3.25e-28 *1.0e-15;

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 28 Increment and Decrement Operators 4 Increment Operator ++ post incrementx++; pre increment++x; 4 Decrement Operator - - post decrementx- -; pre decrement- -x; 4 For examples assume k=5 prior to executing the statement. m= ++k;both m and k become 6 n = k- -;n becomes 5 and k becomes 4

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 29 Abbreviated Assignment Operators operatorexampleequivalent statement +=x+=2; x=x+2; -=x-=2;x=x-2; *=x*=y;x=x*y; /=x/=y;x=x/y; %=x%=y;x=x%y;

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 30

STANDARD INPUT AND OUTPUT The cout Object The cin Object 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 31

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 32 Simple Input - cin  cin is an istream object defined in the header file iostream  cin is defined to stream data from standard input (the keyboard)  We use the input operator >> with cin to assign values to variables General Form: cin >> identifier >> identifier; Note: Data entered from the keyboard must be compatible with the data type of the variable.

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 33 Simple Output - cout  cout is an ostream object, defined in the header file iostream  cout is defined to stream data to standard output (the display)  We use the output operator << with cout to output the value of an expression. General Form: cout << expression << expression; Note: An expression is a C++ constant, identifier, formula, or function call.

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 34 output 1,2 4.5 cm _ //Example1: Determine the output #include using namespace std; int main() { int i, j; double x; string units = " cm"; cin >> i >> j; cin >> x; cout << "output \n"; cout << i << ',' << j << endl << x << units << endl; return 0; } //Input stream:

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 35 //Example 2: Determine the output #include using namespace std; int main() { int i, j; double x, y; cin >> i >> j >> x >> y; cout << "First output " << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; cin >> x >> y >> i >> j; cout << "Second output" << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; return 0; } //Input stream is: First output 1,2,3.4,5 Second output 3,2,2,3 _

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 36 Characters and input 4 The input operator >> skips all whitespace characters. 4 The get() method gets the next character. 4 Example: int x; char ch; cin >> x >> ch; cin >> x; cin.get(ch); Input stream: 45 c 39 b Memory Snapshot

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 37 Characters and input 4 >> discards leading whitespace 4 get() method used to input whitespace characters 4 Example: int x; char ch; cin >> x >> ch; cin >> x; cin.get(ch); xch xch Input stream: 45 c 39 b 45‘c’ 39‘\n ’

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 38 Manipulators and methods 4 endl – places a newline character in the output buffer and flushes the buffer. 4 setf() and unsetf() FlagMeaning ios::showpointdisplay the decimal point ios::fixedfixed decimal notation ios::scientificscientific notation ios::rightright justification ios::leftleft justification 4 Manipulators in –setprecision(n) –setw(n)

C++ STANDARD LIBRARYS cmath cctype 2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 39

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 40 Common Functions in abs(x)computes absolute value of x sqrt(x)computes square root of x, where x >=0 pow(x,y)computes x y ceil(x)nearest integer larger than x floor(x)nearest integer smaller than x exp(x)computes e x log(x)computes ln x, where x >0 log10(x)computes log 10 x, where x>0 sin(x)sine of x, where x is in radians cos(x)cosine of x, where x is in radians tan(x)tangent of x, where x is in radians

2/4/2016Engineering Problem Solving with C++, Etter,Ingber Second Edition 41 Common Functions in isalpha(ch)Returns true if ch is an upper or lower case letter. isdigit(ch)Returns true if ch is a decimal digit isspace(ch)Returns true if ch is a whitespace character. islower(ch)Returns true if ch is an lower case letter. isupper(ch)Returns true if ch is an upper case letter. tolower(ch)Returns the lowercase version of ch if ch is an uppercase character, returns ch otherwise. toupper(ch)Returns the uppercase version of ch if ch is a lowercase character, returns ch otherwise.