Engineering Problem Solving with C++ An Object Based Approach

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.
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 Engineering Problem Solving with C++ An Object Based Approach Chapter 2 Simple C++ Programs.
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.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
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.
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?
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
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.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Bill Tucker Austin Community College COSC 1315
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++ Chapter 1.
Computer Programming BCT 1113
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
Chapter 2: Introduction to C++
Computing and Statistical Data Analysis Lecture 2
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Copyright © 2012 Pearson Education, Inc.
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Chapter 2 - Introduction to C Programming
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
Programming Funamental slides
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Programming Funamental slides
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Chapter 2 - Introduction to C Programming
Presentation transcript:

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

Topics for Discussion Program Structure Data representation: Variables Data types Operators: addition, subtraction Operator precedence Input/output statements Problem solving: from problem statement to complete (tested and verified) C++ solution.

First Program – volume of a box /************************************************************/ /* Program chapter1 */ /* */ /* This program computes the volume of a box */ #include <iostream.h> 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; }

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

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

#include Preprocessor Command Copies source code into the program from the specified file. #include <iostream> Contains class information for input and output. In the version of the compiler you have at the lab: #include <iostream.h>

C++ Data Types Keyword Example of a constant bool true char ‘5’ int 25 double 25.0 string “hello” //must include <string>

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

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);

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;

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

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

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

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

Priority of Operators Parentheses Inner most first Unary operators Right to left (+ -) Binary operators Left to right (* / %)

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

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

Precedence of Arithmetic and Assignment Operators Precedence Operator Associativity 1 Parentheses: () Innermost first 2 Unary operators + - ++ -- (type) Right to left 3 Binary operators * / % Left ot right 4 5 Assignment operator Right to left =

Simple I/O - cin 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.

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)

//Example1 for input and output #include <iostream> #include <string> 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 output 1,2, 4.5 cm _

//Example 2 of input and output #include <iostream> using nmaespace 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; return 0; } //Input stream is: 1 2 3.4 5 2 3 3 7 First output 1,2,3.4,5 Second output 3,7,2,3 _

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); x y x y 45 c 39 b 45 ‘c’ 39 ‘\n ’

Problem: Distance between two points Compute the distance between two points. Method for solving it: Input? Output? Walk-through an example Stepwise solution (pseudo code) Code Test Verify