CSE 100
description
Transcript of 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; 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
Syntax: variable = value;variable = value;
Examples:compputer = “dumb”;Al_E_Newman = “dumber”;count = count +1;
*
Assignment Example 1
#include <iostream.h>#include <iostream.h>void main(void)void main(void){{ int sum;int sum;
sum = 25;sum = 25; cout << “The number stored in sum is " cout << “The number stored in sum is "
<< sum;<< sum; sum = sum + 10;sum = sum + 10; cout << "\nThe number now stored in sum is " cout << "\nThe number now stored in sum is "
<< sum<< ‘\n’; << 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 2int sum;int sum;
sum = 0;sum = 0;cout << "\nThe value of sum is initially set to " << sum;cout << "\nThe value of sum is initially set to " << sum;sum = sum + 96;sum = sum + 96;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum + 70;sum = sum + 70;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum + 85;sum = sum + 85;cout << "\nsum is now " << sum;cout << "\nsum is now " << sum;sum = sum + 60;sum = sum + 60;cout << "\nThe final sum is " << sum;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.
Assume the user enters positive real numbers expressed in inches. Comments are not required.
sample outputHeight: [10.0]Width: [9.0]Length: [12]The glass is 150.0 square inches.Press any key to continue._
Practice Assignment
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)
+= 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
Assignment Operators
* *
Subtotals
Syntax: variable = variable + variable = variable + new_value;new_value;
Examplesyear_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--
++num++num, --num--num
Increment/Decrement
k = k + 1 k = k + 3
k += 1 k += 3
k ++ no equivalent
Increment/Decrement
num = num + 1num = num + 1
num++num++
i = i + 1i = i + 1
i++i++
num = num - 1num = num - 1
num--num--
i = i - 1i = i - 1
i--i--
* * ** *
num += 1num += 1 num num --=1=1
i += 1 i += 1 i i --= 1= 1
Increment/Decrementvalue after execution
kk g g
1. k = 7;
2. g = 2;
3. k = g;
4. g = g + 1;
7 %#@$
7 2
2 2
2 3
* * ** *
or combineor combine 3 & 4 3 & 4
k = g++k = g++
Increment/Decrement
postfix:postfix: first use it, then alter valuefirst use it, then alter value
z = 10;
v = z--;
cout <<v<<‘\t’<<z;
v z
10 9
count = 10;
k = count++;
cout<<k<<‘\t’<<count;
k count
10 11
* * * *
or combineor combine 3 & 4 3 & 4
k = ++gk = ++g
Increment/Decrementvalue after execution
kk g g
1. k = 7;
2. g = 2;
3. g = g + 1;
4. k = g;
7 %#@$
7 2
7 3
3 3
* * ** *
Increment/Decrement
prefix:prefix: first alter value, then use itfirst alter value, then use it
z = 10;
v = --z;
cout <<v<<‘\t’<<z;
v z
9 9
count = 10;
k = ++count;
cout<<k<<‘\t’<<count;
k count
11 11
* * * *
outputoutput
11 cout << cnt++<<'\n';cout << cnt++<<'\n';
22 cout<<cnt<<'\n';cout<<cnt<<'\n';
33 cout<<(cnt++==guess)<<'\n';cout<<(cnt++==guess)<<'\n';
44 cout<<cnt<<'\n';cout<<cnt<<'\n';
55 cout<<cnt++<<'\n';cout<<cnt++<<'\n';
66 cout<<cnt<< '\n'<<'\n';cout<<cnt<< '\n'<<'\n';
Increment/Decrement
* * ** * *
10 // print then inc// print then inc
11
1 // check then inc// check then inc
12
12
13
int cnt = 10, guess = 11;int cnt = 10, guess = 11;
outputoutput
11 cout << ++cnt<<'\n';cout << ++cnt<<'\n';
22 cout<<cnt<<'\n';cout<<cnt<<'\n';
33 cout<<(++cnt==guess)<<'\n';cout<<(++cnt==guess)<<'\n';
44 cout<<cnt<<'\n';cout<<cnt<<'\n';
55 cout<<++cnt<<'\n';cout<<++cnt<<'\n';
66 cout<<cnt<< '\n'<<'\n';cout<<cnt<< '\n'<<'\n';
Increment/Decrement
* * ** * *
11 // inc then print// inc then print
11
0 // inc then check// inc then check
12
13
13
int cnt = 10, guess = 11;int cnt = 10, guess = 11;
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.
Increment/Decrementa) 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* * * ** * * *
int j = 5;int j = 5;
a)a) 5
b)b) 6
c)c) 19
d)d) 0
e)e) 50
f) f) -1
g) g) 10
h) h) 6
Common Programming ErrorsCommon 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 Sleeping is notnot a waste a waste of time.”of time.”
Deepak Chopra
““Except in Except in C++C++ class” class”Joseph DeLibero