1 / 32

CSE 100

CSE 100. Common Programming Errors Assignment Statements Incrementing & Decrementing. How many bytes?. #include <iostream.h> void main () { // the columns below are just for showing on one screen char ch; float num4; unsigned int num3; int num1; double num5;

aduke
Download Presentation

CSE 100

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. CSE 100 • Common Programming Errors • Assignment Statements • Incrementing & Decrementing

  2. How many bytes? • #include <iostream.h> • void main () • { // the columns below are just for showing on one screen • char ch; float num4; unsigned int num3; • int num1; double num5; • long int num2; long num6; • cout <<"\nBytes of storage used by a character:\t\t\t " <<sizeof(ch) • <<"\nBytes used by integer:\t\t\t " <<sizeof(num1) • <<"\nBytes used by long integer:\t\t " <<sizeof(num2) • <<"\nBytes used by unsigned integer:\t\t " <<sizeof(num3) • <<"\nBytes used by floating point number:\t " <<sizeof(num4) • <<"\nBytes used by double floating point:\t " <<sizeof(num5) • <<"\nBytes used by long floating point:\t\t " <<sizeof(num6)<<‘\n’; }

  3. How many bytes? • Bytes of storage used by a character: 1 • Bytes used by an integer: 4 • Bytes used by a long integer: 4 • Bytes used by an unsigned integer: 4 • Bytes used by a floating point number: 4 • Bytes used by a double floating point: 8 • Bytes used by a long floating point: 4

  4. Assignment Operator The assignment operator (=)causes the operand on the left to take on the value to the right side of the statement. • This operator assigns from right to left.valid invalid riker = 5.6 5 = riker *

  5. Assignment Statement • Examples: compputer = “dumb”; Al_E_Newman = “dumber”; count = count +1; Syntax:variable = value; *

  6. Assignment Example 1 • #include <iostream.h> • void main(void) • { • int sum; • sum = 25; • cout << “The number stored in sum is " • << sum; • sum = sum + 10; • cout << "\nThe number now stored in sum is " << sum<< ‘\n’; • }

  7. Assignment Example 1 • Output: • The number stored in sum is 25 • The number now stored in sum is 35 • _ Press any key to continue

  8. Assignment Example 2 • int sum; • sum = 0; • cout << "\nThe value of sum is initially set to " << sum; • sum = sum + 96; • cout << "\nsum is now " << sum; • sum = sum + 70; • cout << "\nsum is now " << sum; • sum = sum + 85; • cout << "\nsum is now " << sum; • sum = sum + 60; • cout << "\nThe final sum is " << sum;

  9. Assignment Example 2 • Output: • The value of sum is initially set to 0 • sum is now 96 • sum is now 166 • sum is now 251 • The final sum is 311

  10. Practice Assignment • An aquarium consists of four panels, all of which are metal except for the largest which is glass. All three side panels are rectangles. The bottom panel is a right triangle. There is no top panel. After getting user input on the dimension, calculate the area of the glass panel.

  11. Practice Assignment Assume the user enters positive real numbers expressed in inches. Comments are not required. sample output Height: [10.0] Width: [9.0] Length: [12] The glass is 150.0 square inches. Press any key to continue._

  12. Assignment Operators • A shorthand notation for certain assignments. • They all have right-to-left associativity. • variable op= (expression) • is equivalent to • variable = variable op (expression)

  13. Assignment Operators • += add then assign • -= subtract then assign • *= multiply then assign • /= divide then assign • X -= 3 º X = X - 3 • pay *= 0.35 º pay = pay * 0.35

  14. Assignment Operators+= -= *= /= %= Assignment Operators • 1. i += 2 i = i + 22. r *= 7 r = r *73. j *= (k + 3) j = j * (k + 3) • 4. x /= y - 4 x = x /y - 45. hour %= 12 hour = hour % 126. left -= t_out left = left - t_out * *

  15. Subtotals Syntax:variable = variable + new_value; • Examples year_pay = year_pay + pay; balance = balance - debit; counter = counter + 1; counter += 1; }same *

  16. Increment/Decrement • ++ increment-- decrement • uniary operators take a single operand ex. num++, num--++num, --num

  17. Increment/Decrement • k = k + 1 k = k + 3 • k += 1 k += 3 • k ++ no equivalent

  18. Increment/Decrement num = num - 1num-- i = i - 1i-- • num = num + 1num++ • i = i + 1i++ num += 1 num -=1 i += 1 i -= 1 * * * * *

  19. Increment/Decrement • value after executionk g1. k = 7;2. g = 2;3. k = g;4. g = g + 1; 7 %#@$ 7 2 2 2 2 3 or combine 3 & 4 k = g++ * * * * *

  20. Increment/Decrementpostfix: first use it, then alter value count = 10;k = count++;cout<<k<<‘\t’<<count; k count 10 11 • z = 10;v = z--;cout <<v<<‘\t’<<z; • v z 10 9 * * * *

  21. Increment/Decrement • value after executionk g1. k = 7;2. g = 2;3. g = g + 1;4. k = g; 7 %#@$ 7 2 7 3 3 3 or combine 3 & 4 k = ++g * * * * *

  22. Increment/Decrementprefix: first alter value, then use it count = 10;k = ++count;cout<<k<<‘\t’<<count; k count 11 11 • z = 10;v = --z;cout <<v<<‘\t’<<z; • v z 9 9 * * * *

  23. Increment/Decrement • output • 1cout << cnt++<<'\n'; • 2 cout<<cnt<<'\n'; • 3 cout<<(cnt++==guess)<<'\n'; • 4 cout<<cnt<<'\n'; • 5 cout<<cnt++<<'\n'; • 6 cout<<cnt<< '\n'<<'\n'; int cnt = 10, guess = 11; 10// print then inc 11 1// check then inc 12 12 13 * * * * * *

  24. Increment/Decrement • output • 1 cout << ++cnt<<'\n'; • 2 cout<<cnt<<'\n'; • 3 cout<<(++cnt==guess)<<'\n'; • 4 cout<<cnt<<'\n'; • 5 cout<<++cnt<<'\n'; • 6 cout<<cnt<< '\n'<<'\n'; int cnt = 10, guess = 11; 11// inc then print 11 0// inc then check 12 13 13 * * * * * *

  25. Area of Circle -- 1 double radius = 5, area;const double PI = 3.1416; area = PI * radius * radius;cout << "The area of a circle of radius ” << radius << " is "<< area <<"\n"; * *

  26. Area of Circle -- 1 • Output: • The area of a circle of radius 5 is 78.54

  27. ºF - ºC Conversion -- 1 • double celsius, faren; • faren = 98.6; • celsius = 5.0/9.0 * (faren - 32.0); • cout << faren <<" degrees Fahrenheit equals " • << celsius <<" degrees celsius.\n";

  28. ºF - ºC Conversion -- 1 • Output: • 98.6 degrees Fahrenheit equals 37 degrees celsius.

  29. int j = 5; Increment/Decrement a) 5 b) 6 c) 19 d) 0 e) 50 f) -1 g) 10 h) 6 • a) cout << j++ • b) cout << ++j • c) cout << j += 14 • d) cout << j /= 10 • e) cout << j *= 10 • f) cout << j -= 6 • g) cout << (j = 5) + j • h) cout << (j == 5) + j * * * * * * * *

  30. Common Programming Errors • not declaring all variables • storing data of one type in a variable of a different type. The variable data type is kept. • using a variable before assigning it a value • mixing data types in an operation • in integer division 4/5 = 0

  31. MoreCommon Programming Errors • forgetting << and; • not initializing variables before use • applying ++ or -- to an expression • forgetting >> to separate variables in cin

  32. dummy box for extra sound “Sleeping is not a waste of time.” Deepak Chopra “Except in C++ class” Joseph DeLibero

More Related