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

Slides:



Advertisements
Similar presentations
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
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.
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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
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.
1 Engineering Problem Solving with C++ An Object Based Approach Chapter 2 Simple C++ Programs.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
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.
Introduction to C++ Programming
 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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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?
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.
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++
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
Chapter 2 Variables.
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.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
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.
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.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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.
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
Chapter 2: Introduction to C++
Basic Elements of C++.
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++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
Presentation transcript:

CS Jan 2007 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 “Enter a number: “ C++ cout << “Enter a number: “; Pseudocode Print square C++ cout << square;

Input… Pseudocode Get value from user 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;// easier 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”;

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

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

Mathmatical Operators Addition and subtraction: +, - Division and multiplication:/, * Modulus (remainder):% Precedence: *, /, % are higher than +, -

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 }