1 Engineering Problem Solving with C++ An Object Based Approach 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
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
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/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 2: Basic Elements of C++
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
COMPUTER SCIENCE I C++ INTRODUCTION
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?
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Simple C++ Programs Program Structure Constants and Variables
CSC 107 – Programming For Science. Announcements.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
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 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Bill Tucker Austin Community College COSC 1315
Simple C Programs.
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1: Introduction to computers and C++ Programming
Chapter 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.
Lecture 2B Expressions Richard Gesick
Chapter 2 Elementary Programming
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

1 Engineering Problem Solving with C++ An Object Based Approach Chapter 2 Simple C++ Programs

2 First Program – volume of a box /************************************************************/ /* Program chapter1 */ /* */ /* This program computes the volume of a box */ /************************************************************/ #include using namespace std; int main() { // Declare and initialize objects double length( 20.75), width(11.5),height(9.5), volume; // Calculate volume. volume = length * width * height; // Print the volume. cout << “The volume is “ << volume << endl; // Exit program. return 0; } /************************************************************/

3 Program structure preprocessor directives int main() { declarations statements }

4 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 */

5 #include Preprocessor Command Copies source code into the program from the specified file. #include –Contains class information for input and output.

6 C++ Data Types KeywordExample of a constant booltrue char‘5’ int25 double25.0 string“hello” //must include

7 Naming entities in C++ Identifiers are used to name entities in C++. Rules for construction of identifiers –Start with a letter or underscore _ –Consist of letters digits and underscore –Can not be a reserved word. –Only first 31 characters used to distinguish it from other identifiers. –Case sensitive

8 Variable Declarations Declarations define memory locations, including type of data to be stored, identifer, and possibly an initial value. General Form: data_type identifier_list; Examples: double length( 20.75), width(11.5), volume; int numberOfFeetInYard(3);

9 Symbolic Constants Used to name values which do not change during the execution of the program. Are always initialized at declaration. Used wherever an expression is allowed. General Form: const data_type identifier = value;

10 Example 1 - initialization double sum = 0; sum 4 Example 2 int x; x=5;x Assignment Statements Used to assign a value to a variable General Form: identifier = expression; 0 5 a 4 Example 3 char ch; ch = ‘a’; ch

11 Assignment Statements - continued Example 3 int x, y, z; x=y=0; z=2;x y z Example 4 y=z;y

12 Arithmetic Operators Addition+ Subtraction- Multiplication* Division/ Modulus% –Modulus returns remainder of division between two integers –Example 5%2 returns a value of 1

13 Integer Division Division between two integers results in an integer. The result is truncated, not rounded Example: 5/3 is equal to 1 3/6 is equal to 0

14 Priority of Operators 1.ParenthesesInner most first 2.Unary operatorsRight to left (+ -) 3.Binary operatorsLeft to right (* / %) 4.Binary operatorsLeft to right (+ -)

15 Self-test - Evaluate * 5 – / 3 8 % 3 * 6 (7 + 3) * 5 - 2

16 Overflow and Underflow Overflow –answer to large to store –Example: using 16 bits for integers –result = ; Exponent overflow –answer’s exponent too large –Example: using float, with exponent range –38 to 38 –result = 3.25e28 * 1.0e15; Exponent underflow –answer’s exponent to small –Example: using float, with exponent range –38 to 38 –Result = 3.25e-28 *1.0e-15;

17 Increment and Decrement Operators Increment Operator ++ post incrementx++; pre increment++x; Decrement Operator - - post decrementx- -; pre decrement- -x; 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

18 Abbreviated Assignment Operator 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;

19

20 Simple I/O - cin 4 cin is an istream object streams input from standard input uses the >> (input operator) General Form: cin >> identifier >> identifier; Note: Data entered from the keyboard must be compatible with the data type of the variable.

21 Simple Output - cout cout –is an ostream object –streams output to standard output –uses the << (output) operator General Form: cout << expression << expression; Note: An expression is any C++ expression (string constant, identifier, formula or function call)

22 output 1,2, 4.5 cm _ //Example1 for input and 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: 1,2,3,4

23 //Example 2 of input and 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 _

24 Manipulators and methods endl – places newline character in stream and flushes the buffer. 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)

25 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

26 Characters and input >> discards leading whitespace get() method used to input whitespace characters Example: int x; char y; cin >> x >> y; cin >> x; cin.get(y);xy xy 45 c 39 b 45‘c’ 39‘\n ’

27 Wind-Tunnel Data Flight-Path Angle Coefficient of Lift