1 / 28

C++ Programming: Arithmetic Operations and Data Types

Learn about arithmetic operations and data types in C++ programming, including integers, characters, and floating-point numbers. Explore examples and exercises to practice using these concepts.

dkemp
Download Presentation

C++ Programming: Arithmetic Operations and Data Types

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. Overview • Order of presentation different than published handouts • Run a program on ccc • Finish Arithmetic operations • Data types integer char floating point • Write a program

  2. Rules of precedence for Arithmetic Operations

  3. Example - precedence for arithmetic operations algebra: m = a + b + c + d + e -------------------- 5 C++: m = (a + b + c + d + e) / 5; what if parentheses missing? m = a + b + c + d + e -- 5 algebra: m = mx + b C++: m = m * x + b;

  4. Exercises 1) int a, x, b, c, y; a = 2; x = 5; b = 3; c = 7; y = a * x * x + b * x + c; What is y after this program executes? 2) Evaluate the following c++ expressions: x = 7 + 3 * 6 / 2 - 1; y = 2 % 2 + 2 * 2 - 2/2; z = (3 * 9 * (3 + (9 * 3/ (3))));

  5. Integer Data types • Size and range differ from system to system. Typical: short (2 bytes) unsigned short 0 to 65535 signed short -32768 to 32765 int (4 bytes) unsigned int 0 to 4294967295 signed int -2147483648 to 2147483647 long (4 bytes) unsigned long 0 to 4294967295 signed long -2147483648 to 2147483647 • int is shorthand for signed int • same operations valid for all • why so many? conserve memory. Database of all US citizens, age, social security #.

  6. Determining Size and Range // Determining the size and range of the the signed long integer type. #include <iostream> using namespace std; int main() { cout << "signed long type is " << sizeof (signed long) << "bytes\n"; cout << "largest value of signed long type is " << LONG_MAX << endl; cout << "smallest value of signed long type is " << LONG_MIN << endl; return 0; } page 45 of book gives other constants. Find out size and range for WPI.

  7. Floating point Data Type Algebraically: number with fractional part 3.14 3 1.2 x 10 mantissa (1.2) exponent (3) -2 1.5 x 10 mantissa (1.5) exponent (-2) Implementation: Varies from system to system. Typical: sign bit 8 bits exponent 23 bits mantissa

  8. floating point data type • C++: Use when: fractions too big for integer (1 x 10 to the 100th) • Precision - number of places to right of decimal point. Limited by size of mantissa

  9. Floating Point Data Types

  10. floating point data type • implementations differ. Typical double always >= float long double always >= double • Constants 3.14 (double) 3.14L (long double) 3.14f (float) float f1 = 3.14; //compiler error

  11. Mixing float and integer types #include <iostream> using namespace std; int main() { float average; int numberOfGrades = 2; int totalOfGrades = 9; average = totalOfGrades / numberOfGrades; cout << "The average is " << average << endl; return 0; }

  12. mixing float and integer • could change totalOfGrades to float to fix. Compiler will promote numberOfGrades • Could use cast operator to change totalOfGrades temporarily average = (float) totalOfGrades/ numberOfGrades;

  13. Things to remember about floating point data types • Mantissa is limited in size so value may not be exact (1/3 = .3333333333...) • Arithmetic is faster using integer types than floating point types • Don’t use equality operators • Modulus operator not valid for floating point • Don’t use floating point #’s for loop control

  14. Exercises 1) create a floating point variable and initialize it to the value 21.654 2) create a floating point variable and initialize it to the value 1.6 times 10 to the 12th power 3) Why would the compiler complain about this statement? float f = 32.56; 4) Suppose you have a floating point number and you want to separate the whole part from the fractional part so that you have two integer values. One contains the whole part and one contains the fractional part ie. 5.77 -> 5, 77. How would you do it?

  15. Homework • Use cin to read in a value double total; cin >> total; • To print floating point using cout cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout << setprecision (2);

  16. char data type • char ch1; ch1 • ASCII set see book 623 A is 65 B is 66 • regularity. A + 1 = B A < B is true • initialize with ‘ char ch1 = ‘A’; // stores whole number 65 in storage cell named ch1 one byte

  17. char data type • routines treat char differently than int char ch1 = ‘A’; int int1 = 65; cout << “ch1 is << ch1 << endl; //displays “ch1 is A” cout << “int1 is “ << int1 << endl;//displays “ch1 is 65” • char is shorthand for either unsigned char (0 to 255) or signed char (-127 to 127) varies from system. What is it on ours?

  18. Data Types Exercises Given the following declarations, determine the value of the variable on the left-hand-side of each assignment statement. int int1, int2, int3; float f1=1.0f, f2=2.5f, f3=5.0f; double d1, d2; char ch1, ch2; int1 = 5; d2 = 5.0; ch1 = '5'; int3 = f2; int2 = int1 / int3; d1 = f3 - -f1 * 6 / int3 + 8.0 * (int1 - d2); ch2 = ch1 - 2; int3 = 'a' - 'A'; ch1 = 'W' + int3;

  19. Operations • Arithmetic • Relational (a < b) • Logical (a && b)

  20. Relational operators • Used to compare values • result of relational expression is true or false a = 1, b = 2 a == b false a < b true a > b false • Used to change execution order of statements

  21. Relational Operators

  22. Slide relational operators (cont) • precedence < <= >= > higher than == and != a <= b == x > y is the same as (a <= b) == (x > y) • arithmetic operators higher precedence than relational if x = 2, y = 3 y == 2 * x + 3 evaluated as follows: 2 * x (4) 4 + 3 (7) 3 == 7? (false)

  23. Common programming errors • Compiler will generate an error if the operators ==, !=, >=, or <= have whitespace • Cannot reverse => not same as >= • using = instead of == common mistake m== 4 desired. m = 4 done. do 4 == m

  24. relational operators exercises Identify which of the following expressions are true: Assume a = 0, b = 1, x = 2, y = 3 1) a == b + x 2) b + x == y 3) x - b != y 4) a * b + x * y != x * y 5) a <= b != x <= y

  25. Logical Operators

  26. Logical Operators • Precedence ! && || !a && b same as (!a) && b • Relational operators have higher precedence than the && and || operators • a < b && c > d same as (a < b) && (c > d)

  27. Precedence chart

  28. Precedence Exercises Exercises 1) Assume a = 1, b = 2, c = 4, d =3 is following expression true or false? a < b && c >d 2) Create C++ expression a < b and > c, or x > y and > z

More Related