CS 1400 15 Sept 2006 Pick ups from chapters 2 and 3.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity Csc 125 Introduction to C++
Advertisements

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
CS 117 Spring 2002 Basic Program Elements Chapter 2.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS 1400 Pick ups from chapters 2 and 3. #include directive This pre-processing directive causes the textual contents of a named file to be inserted into.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
CS150 Introduction to Computer Science 1
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
CS 1400 Feb 2007 Pick ups from chapters 2 and 3. Example; A person 15 years of age may acquire a driver’s lic. with permission from parents. A person.
CS 1400 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
1 Chapter 3 Expressions and Interactivity. 2 Topics 3.1 The cin Object 3.2 Mathematical Expressions 3.3 When You Mix Apples and Oranges: Type Conversion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for CMPS 1043 Computer Science I at MSU.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Simple C++ Programs Program Structure Constants and Variables
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Expressions and Interactivity. 3.1 The cin Object.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lecture 3 Expressions, Type Conversion, Math and String
Today Variable declaration Mathematical Operators Input and Output Lab
Introduction to C++ (Extensions to C)
Chapter 3: Expressions and Interactivity
Programming Fundamentals
Chapter 3. Expressions and Interactivity
Chapter 3: Expressions and Interactivity.
Chapter 3: Expressions and Interactivity
Expressions and Interactivity
Counting Loops.
Expressions and Interactivity
Standard Version of Starting Out with C++, 4th Edition
An Introduction to Programming with C++ Fifth Edition
C++ Programming Basics
C++ for Engineers and Scientists Second Edition
Lecture 3 Expressions, Type Conversion, and string
Presentation transcript:

CS Sept 2006 Pick ups from chapters 2 and 3

Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int sum = 0; float interest_rate = 0.56;

Boolean constants Example: bool flag; flag = true; flag = false; What would this do? while (true) {cout << “hello! “; }

Type conversions Rankings: –long double(highest) –double –float –unsigned long –long –unsigned int –int(lowest)

Rules Rule #1 –char, short, unsigned short are all considered as int Rule #2 –When a mixed-type arithmetic operation is encountered, the lower ranked value is promoted to the higher ranking value. Rule #3 –When a value is assigned to a variable, it is converted to that variable type.

Overflow / Underflow Assigning a too large or too small value to a variable results in incorrect values. An error message may not be generated!

Manual type conversions… Type casting allows you to manually force a value to be converted to a type –Form: static_cast (value ) –Example: int sum, count; float average; cin >> sum >> count; average = static_cast (sum) / count; cout >> “average is: “ << average << endl;

Named constants Constants may be given symbolic names to represent them in a program; –Form: const type name = value; –Example: const float PI = ; const int SIZE = 24; –Alternative Form:#define name value #define PI #define SIZE 24 Defines should be place above main()

Combining assignments Multiple assignment statements may be combined –Example: a = b = c = 25; Shorthand notation for combining arithmetic operations and assignment –Example: a = a * 2;same asa *= 2; b = b – 5;same asb -= 5; c = c / 7;same asc /= 7;

Formatting output Requires: #include cout manipulators: setw(n) (set the minimum spaces for the next value output) setprecision(n) (set the precision or rounding for a float value) fixed (force output of float values in fixed-point notation) showpoint (show a decimal point with trailing zeros)

Controlling input Requires #include cin manipulators setw(n) do not input more than n-1 characters for the next variable textbook error, pg 128 char word[5]; cin >> setw(5) >> word;// user enters “Eureka” cout << word;// program outputs “Eure”

Special input examples… char sentence[20], ch; cin.getline(sentence, 20); –reads all characters on a line up to 19 in count from the keyboard and stores them in array sentence cin.get(ch); –reads the next input character (including white spaces) cin >> ch; –reads the next printable character (ignoring white spaces) cin.ignore (); –ignore (discard) the next input buffer character

More math library functions… Requires: #include functionsexamples absy = abs(x); sqrty = sqrt(a*a + b*b); logy = log(x); siny = sin(x+z); cosy = cos(x); tany = tan(x); etc…

File Output Requires:#include ofstream fout; fout.open (“report.txt”); fout << “hello from a file!\n”; fout.close(); To write to report.txt on the a: drive; fout.open (“a:\\report.txt”);

File Input Requires:#include int a, b; ofstream fin; fin.open (“report.txt”); fin >> a >> b; cout << “sum is: “ << a+b << endl; fin.close();

Example… Write a program to generate a report of concert hall business for a touring show. –assume:reserved seat tickets$80 stadium seat tickets$50 standing tickets$35 production company gets 80%

Input… line 1:tour name line 2:number of shows line 3 (first show)reserved stadium standing line 4 (second show)reserved stadium standing etc…