1 / 16

Math Operators and Output Formatting

Math Operators and Output Formatting. Incrementing and Decrementing. If J holds a value of 10. K. J. 10. K = J++; // post-fix. Then,. K. J. 11. 10. K. J. 10. K = ++J; // pre-fix. K. J. 11. 11. Assignment Operator. i = 25;. More Assignment Operator. Dividing by Zero.

arav
Download Presentation

Math Operators and Output Formatting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Math OperatorsandOutput Formatting

  2. Incrementing and Decrementing

  3. If J holds a value of 10 K J 10 K = J++; // post-fix Then, K J 11 10 K J 10 K = ++J; // pre-fix K J 11 11

  4. Assignment Operator i = 25; More Assignment Operator

  5. Dividing by Zero In mathematics, division by zero is infinitive. In Computer Science, it generate an error. Overflow Overflow is the condition when a value becomes too large for its data type. Underflow Underflow is the condition when a number is too small for the data type Precision When a number’s significant decimal place is important to the accuracy of the calculation, it is necessary to promote to the memory of another data type. For instance, use double instead of float to represent your number

  6. Mixing Data Type C++ allow you to mix data types in calculation. In case of mixed data types, the compiler makes adjustments so as to produce the most accurate answer. For example, the integer value (number_of_people) is temporarily converted to a float so that the fractional part of the variable (money) can be used in the calculation. The is called promotion. int number_of_people; float money, share; share = money / number_of_people;

  7. 5/2 yield 2 2/4 yield 0 5/2.0 or 5.0/2.0 or 5.0/2 yield 2.5 Implicit Promotion float a=10, b=5, x; int m=3.0, n=2.5, y; y = m/n; yield 1 x = m/n; yield 1.0 x = 5.5; y = x + m/n; ==> y = x + 1 ==> y = 5.5 + 1.0 ==> y = 6.5 ==> y = 6

  8. Typecasting • C++ allows you to explicity change one data type to another using operators called typecast operators • using cast operator to create a temporary copy of the variable. float payRate = (float) 0.0; int Num; Num = (int) payRate;

  9. Input • Skip all the leading delimiter ( space, tab, <enter>) • Char read 1 at a time (1 non blank) • Data will be read until space • >> extraction operator Form: cin >> dataVariable; cin >> age >> firstInitial;

  10. Output • Output stream cout • << Output operator (insertion operator) • cout << “my height in inches is: “ << height; • Blank lines • endl; or “\n”; • Form: cout << dataVariable;

  11. Using setf and unsetf to format output The cout object has format options that can be changed. To change these option, you send a message to the object using serf and unsetf. Examples are illustrated in knowlton p94

  12. // coutsetf.cpp - Knowlton p 99 #include <iostream> using namespace std; int main() { float x = 24.0; cout << x << endl; // display 24 cout.setf(ios::showpoint); cout << x << endl; // display 24.0000 cout.setf(ios::showpos); cout << x << endl; // display +24.0000 cout.setf(ios::scientific); cout << x << endl; // display +2.400000e+001 cout.setf(ios::uppercase); cout << x << endl; // display +2.400000E+001 cout.unsetf(ios::showpoint); cout << x << endl; // display +2.400000E+001 cout.unsetf(ios::showpos); cout << x << endl; // display 2.400000E+001 cout.unsetf(ios::uppercase); cout << x << endl; // display 2.400000e+001 cout.unsetf(ios::scientific); cout << x << endl; // display 24 return 0; }

  13. I/O Manipulators #include <iomanip> It required to include a library <iomanip> in the section for compiler directives. setprecision - to set the number of digits that are to appear to the right of the decimal point. cout << setprecision(2) << price << ‘\n’; setw - to set the minimum number of spaces for the number to be display on output. The amount of space used to display a number is called the field width. setw can only the format for the next output. cout << setw(2) <<‘$’ << total << “\n\n”;

  14. // This program demonstrates the setw manipulator being // used with values of various data types. #include <iostream> #include <iomanip> using namespace std; int main() { int IntValue = 3928; float FloatValue = 91.5; char StringValue[14] = "John J. Smith"; cout << "(" << setw(5) << IntValue << ")" << endl; cout << "(" << setw(8) << FloatValue << ")" << endl; cout << "(" << setw(16) << StringValue << ")" << endl; return 0; } Program Output ( 3928) ( 91.5) ( John J. Smith)

  15. getline - could be used to read in a string including space getline does not skip leading white space string name; getline (cin, name); ignore - skips up to n characters in the specified stream (cin) until the character argument (delimiter) is encountered. cin.ignore(100,’\n’); cin.ignore(80,’*’);

  16. // instring.cpp #include <iostream> #include <string> using namespace std; int main() { string FirstName; string LastName; cout << "Enter your first name: "; getline(cin, FirstName); cout << "Enter your last name: "; getline(cin, LastName); cout << "Your name is " << FirstName << " " << LastName << ".\n"; return 0; }

More Related