320 likes | 333 Views
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;
E N D
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; • 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’; }
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
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 *
Assignment Statement • Examples: compputer = “dumb”; Al_E_Newman = “dumber”; count = count +1; Syntax:variable = value; *
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’; • }
Assignment Example 1 • Output: • The number stored in sum is 25 • The number now stored in sum is 35 • _ Press any key to continue
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;
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
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.
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._
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)
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
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 * *
Subtotals Syntax:variable = variable + new_value; • Examples year_pay = year_pay + pay; balance = balance - debit; counter = counter + 1; counter += 1; }same *
Increment/Decrement • ++ increment-- decrement • uniary operators take a single operand ex. num++, num--++num, --num
Increment/Decrement • k = k + 1 k = k + 3 • k += 1 k += 3 • k ++ no equivalent
Increment/Decrement num = num - 1num-- i = i - 1i-- • num = num + 1num++ • i = i + 1i++ num += 1 num -=1 i += 1 i -= 1 * * * * *
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++ * * * * *
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 * * * *
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 * * * * *
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 * * * *
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 * * * * * *
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 * * * * * *
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"; * *
Area of Circle -- 1 • Output: • The area of a circle of radius 5 is 78.54
º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";
ºF - ºC Conversion -- 1 • Output: • 98.6 degrees Fahrenheit equals 37 degrees celsius.
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 * * * * * * * *
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
MoreCommon Programming Errors • forgetting << and; • not initializing variables before use • applying ++ or -- to an expression • forgetting >> to separate variables in cin
dummy box for extra sound “Sleeping is not a waste of time.” Deepak Chopra “Except in C++ class” Joseph DeLibero