Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 C++ Numerical Data Input/Output Programming

2 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. 100.0, 100.0000, 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)

3 COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 3 Rules for Division l Examples: 220. / 100.0 double/double -> double result is 2.2 220. / 100 double/int -> double result is 2.2 220 / 100.0 int/double -> double result is 2.2 220 / 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).

4 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

5 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?

6 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

7 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

8 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.

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

10 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

11 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

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

13 // 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";

14 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: 800.75 Enter the number of people attending lunch: 9 If the lunch cost $800.75, and you have 9 attending, then the cost is $88.9722. Using setprecision (4) in the last cout statement can change the final result to $88.97.


Download ppt "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."

Similar presentations


Ads by Google