C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.

Slides:



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

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
C++ Basics Part I Part II. Slide 2 Part I: basics.
C++ Basics. COMP104 C++ Basics / Slide 2 Introduction to C++ * C++ is a programming language for manipulating numbers and user-defined objects. * C++
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall A. Using Class Type Objects.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Exposure C++ Chapter VII Program Input and Output.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
CPS120: Introduction to Computer Science Formatted I/O.
COMP102 Lab 021 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
CSCI-1411 F UNDAMENTALS O F C OMPUTING L AB Shane Transue Summer
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Chapter 05 (Part II) Control Statements: Part II.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
Introduction to programming using C++ Dr. Mohamed Khafagy.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
What Actions Do We Have Part 1
Programming Fundamentals
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3. Expressions and Interactivity
Chapter 3 L7.
Manipulators CSCE 121 J. Michael Moore
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
C++ Basics.
Chapter 3: Input/Output
Introduction to cout / cin
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Formatted Input, Output & File Input, Output
Fundamental Programming
Introduction to cout / cin
Presentation transcript:

C++ Numerical Data Input/Output Programming

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers differently from decimal numbers. 100 is an int type , , and 100. are double type. The general rule for division of int and double types is: double/double -> double (normal) double/int -> double (normal) int/double -> double (normal) int/int -> int (note: the decimal part is discarded)

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 3 Rules for Division l Examples: 220. / double/double -> double result is / 100 double/int -> double result is / int/double -> double result is / 100 int/int -> int result is 2 Summary: division is normal unless both the numerator and denominator are int, then the result is an int (the decimal part is discarded).

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 4 Assignment Conversions A decimal number assigned to an int type variable is truncated. An integer assigned to a double type variable is converted to a decimal number. l Example 1: double yy = 2.7; int i = 15; int j = 10; i = yy; // i is now 2 yy = j; // yy is now 10.0

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 5 Assignment Conversions l Example 2: int m, n; double xx; m = 7; n = 2.5; xx = m / n; n = xx + m / 2; // What is the value of n?

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 6 Assignment Conversions l Example 2: int m, n; double xx; m = 7; n = 2.5; // 2.5 converted to 2 and assigned to n xx = m/n; // 7/2=3 converted to 3.0 and assigned to xx n = xx+m/2; // m/2=3 : integer division // xx+m/2 : double addition because xx is double // convert result of m/2 to double (i.e. 3.0) // xx+m/2=6.0 // convert result of xx+m/2 to int (i.e. 6) //because n is int

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 7 Forcing a Type Change l You can change the type of an expression with a cast operation. l Syntax: variable1 = type(variable2); variable1 = type(expression); l Example: int x=1, y=2; double result1 = x/y;// result1 is 0.0 double result2 = double(x)/y;// result2 is 0.5 double result3 = x/double(y);// result3 is 0.5 double result4 = double(x)/double(y);// result4 is 0.5 double result5 = double(x/y);// result5 is 0.0 int cents = int(result4*100); // cents is 50

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 8 Standard Input/Output cin - the standard input stream Input operator “ >> ” n Extracts data from input “stream” (the keyboard by default). n Skips over white spaces. n Extracts only characters of the right form and performs automatic conversion to the type specified.

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 9 Standard Input/Output cout - the standard output stream Output operator “ << ” n Inserts data into the output “stream” (the screen by default). n Example: int id, score; cout << "Enter student ID and score: "; cin >> id >> score; cout << "Student ID: " << id << " score: " << score << endl;

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 10 Standard Input/Output Some special output characters: \a bell \t tab \n new line \' single quote \" double quote

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 11 Format Manipulation #include l setw(int size) Specifies the number of characters to use in displaying the next value, which is right-justified. Ex: cout << setw(5) << 12; //output 3 spaces and then 12 l setprecision(int digit) Specifies the number of significant digits for all subsequent output. l cout << fixed << setprecision(2); the precision is: two digits after the decimal point. l Fixed: uses fixed-point notation in the float field

COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 12 Format Manipulation #include using namespace std; int main() { cout << setprecision(2) << << endl; // output cout << setprecision(2) << << endl; //output 1.2e+002 return 0; }

// Demonstrate the features of output format manipulators // Input: cost of lunch, number of people attending lunch // Output: lunch cost per person #include // Use IO manipulators #include using namespace std; int main() { double cost_of_lunch, cost_per_person; int number_of_people; cout << "Press return after entering a number.\n"; cout << "Enter the cost of lunch:\n"; cin >> cost_of_lunch; cout << "Enter the number of people attending lunch:\n"; cin >> number_of_people; cost_per_person = cost_of_lunch / number_of_people; // cout << setiosflags(ios::fixed) << setprecision(2); cout << "If the lunch cost $"; cout << cost_of_lunch; cout << ", and you have " << number_of_people << " persons attending, then \n";

cout << "the cost per person is $" << cost_per_person << ".\n"; /* cout << "the cost per person is $"; cout << setprecision(4) << cost_per_person << ".\n"; */ return 0; } Output of example program: Press return after entering a number. Enter the cost of lunch: Enter the number of people attending lunch: 9 If the lunch cost $800.75, and you have 9 attending, then the cost is $ Using setprecision (4) in the last cout statement can change the final result to $88.97.