CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8, 11 - 15.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
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.
© 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.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.
Input & Output: Console
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CSC 107 – Programming For Science. Announcements.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
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.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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.
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
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Chapter 2: Introduction to C++
Basic Elements of C++.
Variables, Expressions, and IO
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
Topics Designing a Program Input, Processing, and Output
Presentation transcript:

CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,

Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return 0; }

Output… Pseudocode Print to screen “Enter a number: “ C++ cout << “Enter a number: “; Pseudocode Print value of variable square C++ cout << square;

Input… Pseudocode Get number from user and store in variable value C++ cin >> value;

Assignments… Pseudocode Square = n * n C++ square = n * n; Pseudocode Pay = hours * 40 + (hours-40) * Overtime_pay C++ pay = hours * 40 + (hours-40) * overtime_pay;

Declarations… All variables must be established or declared prior to use – usually at the top of the program Integers are declared using int. For example; int value; int width, height, depth; A variable name must begin with a letter, followed by letters, digits, or underscores.

Pseudocode Example: “Write a program to calculate the volume of a box, using dimensions provided by the user.” Print “Enter height, width, and depth of box:” Get height from user Get width from user Get depth from user volume = height * width * depth Print “Volume is: “ Print volume

C++ boilerplate… // Cannon, demo program #include using namespace std; int main() { return 0; }

Adding C++ instructions… // Cannon, demo program #include using namespace std; int main() {int height, width, depth, volume; cout << “Enter height, width, and depth: “; cin >> height >> width >> depth; volume = height * width * depth; cout << “Volume is: “ << volume; return 0; }

C++ is case sensitive! Use lower case for simplicity Upper case or underscore may be used to separate multi-word variable names int phone_number;// easiest to read int phoneNumber;// easy to read int phonenumber;// not as easy to read

Extended syntax… Input and output statements can be cascaded; cin >> height >> width >> depth; cout << “the value of width is: “ << width; Declarations can be a list; int height, width, depth, volume; Output messages can include control characters such as \n (new line) and \a (beep) cout << “This appears on line 1 \n This is on line 2”; Output messages may output endl to force a new line cout << “this is line1 “ << endl << “This is line 2”;

Assignment statements A formula may be used to store a calculation into one variable volume = height * width * height; Operator symbols –Addition and subtraction: +, - –Division and multiplication:/, * –Modulus (remainder):% Precedence: *, /, % are higher than +, -

Integer variables Declaring variables as type int – –Only allows them to hold an integer (+ or -) –Range -2,147,483,648 to 2,147,483,647 Variations – –unsigned int –long –short… etcetera

Floating point variables… Declaring variables to be type float – –Allows them to hold an approximate floating- point value (precision to ~6 digits) –Range +/- 3.4 x to +/- 3.4 x –Constants may be expressed in floating-point notation or E-notation –E-notation is like scientific notation 3.14 x 10 5 is the same as 3.14E5

Summary of variable types… int –exact whole numbers –limited range float –approximate decimal numbers –vast range

Literals vs. numbers A literal or string is enclosed in quotes cout << “hello” << “my name is Fred”; Any letter or character may be part of a literal Literals are for humans to read – you cannot do math with a literal or store a literal in a numeric variable! int x; x = “5”;// ouch!!

Keywords Certain words are reserved in C++ and cannot be used for variable names Fig 2.4

Truncation An int variable can only hold integers int whole; float decimal; decimal = ; whole = 25; Integer truncation will occur on; –assignment:whole = decimal; –division:decimal = 1 / 3; An integer divided by an integer is always an integer!!

Scope A variable may only be used after it is declared: {// age may not be used here … int age; …// age may be used here }

Examples… Write a program to calculate the area of a circle. Write a program to calculate the total amount of a sale by adding 7.5% sales tax Write a program to convert Fahrenheit temperatures to Centigrade. Write a program to calculate the sale amount for a company that sells three types of bags (small, medium, and large). They sell for $2.50, $3.25, and $4.00 respectively. The user should enter three values for the count of each bag type. formulas: A =  R 2 total = (1.075)sale C = (9/5)(F-32)

Tracing tables… A traciing table shows the values of variables after each program statement Stmtbag1bag2bag3costsale …

Summary Topics covered –boilerplate –input and output –assignment statements –declarations for int and float –case sensitivity –literals –keywords –truncation –scope