CSE1222: Lecture 3The Ohio State University1. Assignment Operations The C++ assignment operator is:...
-
Upload
robert-cuthbert-tyler -
Category
Documents
-
view
214 -
download
0
Transcript of CSE1222: Lecture 3The Ohio State University1. Assignment Operations The C++ assignment operator is:...
CSE1222: Lecture 3 The Ohio State University 1
Assignment Operations The C++ assignment operator is:
= Examples:
x = 3 * 5;y = x – 7;y = y + 4;
Do not confuse assignment for equality!
CSE1222: Lecture 3 The Ohio State University 2
Assignment Operations Remember our rule:
A variable, and only one variable, must be on the leftAny expression that evaluates to the same data
type as the variable can be on the right
Example:y = x – 7;
Syntax error:x – 7 = y; // syntax error
An expression cannot appear on the left(Left and right are not interchangeable)
CSE1222: Lecture 3 The Ohio State University 3
Expressions An expression is a combination of
constants, variables, and function calls that evaluate to a result
Example: x = 3.0 * 4.0;y = 2.0 + x;z = 5.0 + x/y - sqrt(x * 3.0);
Remember: Variables (i.e., x,) must be defined before they are used
CSE1222: Lecture 3 The Ohio State University 4
average.cpp// Compute the average of three numbers.#include <iostream>using namespace std;
int main(){ double x1, x2, x3, sum, average;
cout << "Enter three numbers : "; cin >> x1 >> x2 >> x3;
sum = x1 + x2 + x3; average = sum / 3.0;
cout << "The average is: " << average << endl;
return 0;}
CSE1222: Lecture 3 The Ohio State University 5
> average.exeEnter three numbers : 2.5 3.1 0.2The average is: 1.93333
>
CSE1222: Lecture 3 The Ohio State University 6
… double x1, x2, x3, sum, average;
cout << "Enter three numbers : "; cin >> x1 >> x2 >> x3;
sum = x1 + x2 + x3; average = sum / 3.0;
cout << "The average is: " << average << endl;…
bmi.cpp// Compute the body mass index (BMI)#include <iostream>using namespace std;
int main(){ double height, weight, height_squared, bmi;
cout << "Enter height (inches): "; cin >> height; cout << "Enter weight (pounds): "; cin >> weight;
height_squared = height * height; bmi = weight * 705.0 / height_squared;
cout << "Body Mass Index = " << bmi << endl;
return 0;}
CSE1222: Lecture 3 The Ohio State University 7
simpleslope.cpp// Compute the slope between two points#include <iostream>using namespace std;
int main(){ double x1, y1, x2, y2, xdiff, ydiff, slope;
cout << "Enter x and y coordinates of first point : "; cin >> x1 >> y1; cout << "Enter x and y coordinates of second point : "; cin >> x2 >> y2;
xdiff = x1 - x2; ydiff = y1 - y2; slope = ydiff / xdiff; cout << "The slope is: " << slope << endl;
return 0;}
CSE1222: Lecture 3 The Ohio State University 8
> simpleslope.exe
Enter x and y coordinates of first point : 0 3
Enter x and y coordinates of second point : 4 1
The slope is: -0.5
>
CSE1222: Lecture 3 The Ohio State University 9
… double x1, y1, x2, y2, xdiff, ydiff, slope;
cout << "Enter x and y coordinates of first point : "; cin >> x1 >> y1; cout << "Enter x and y coordinates of second point : "; cin >> x2 >> y2;
xdiff = x1 - x2; ydiff = y1 - y2; slope = ydiff / xdiff; cout << "The slope is: " << slope << endl;…
simpleslope.cpp
What is the potential problem with this code?
CSE1222: Lecture 3 The Ohio State University 10
... xdiff = x1 - x2; ydiff = y1 - y2; slope = ydiff / xdiff; cout << "The slope is: " << slope << endl;...
Assignment Variations Remember: Only one variable allowed on
the left side of the equals sign
But, that same variable can be on the right side
counter = 3;counter = counter + 5;cout << “counter = “ << counter;
What is the output???
Remember: The right side of the equals is ALWAYS evaluated first
CSE1222: Lecture 3 The Ohio State University 11
power.cpp#include <iostream>using namespace std;
int main(){ double x, y; cout << "Enter x : "; cin >> x;
y = x; y = y * y; // Note: '=' is assignment, NOT equality. cout << "x^2 = " << y << endl;
y = y * x; // Note: '=' is assignment, NOT equality. cout << "x^3 = " << y << endl;
y = y * x; // Note: '=' is assignment, NOT equality. cout << "x^4 = " << y << endl;
return 0;}
CSE1222: Lecture 3 The Ohio State University 12
> power.exe
Enter x : 1.5
x^2 = 2.25
x^3 = 3.375
x^4 = 5.0625
>
CSE1222: Lecture 3 The Ohio State University 13
… y = x; y = y * y; // Note: '=' is assignment, NOT
equality. cout << "x^2 = " << y << endl;
y = y * x; // Note: '=' is assignment, NOT equality.
cout << "x^3 = " << y << endl;
y = y * x; // Note: '=' is assignment, NOT equality.
cout << "x^4 = " << y << endl;…
ERROR!#include <iostream>using namespace std;
int main(){ double x, y;
cout << "Enter x : "; cin >> x;
y = x^2; // SYNTAX ERROR. cout << "x^2 = " << y << endl;
y = x^3; // SYNTAX ERROR. cout << "x^3 = " << y << endl;
y = x^4; // SYNTAX ERROR. cout << "x^4 = " << y << endl;
return 0;}
CSE1222: Lecture 3 The Ohio State University 14
> g++ powerError.cpp
powerError.cpp: In function `int main()':
powerError.cpp:13: invalid operands of types `double' and `int' to binary `operator^'
powerError.cpp:16: invalid operands of types `double' and `int' to binary `operator^'
powerError.cpp:19: invalid operands of types `double' and `int' to binary `operator^'
>
CSE1222: Lecture 3 The Ohio State University 15
…13. y = x^2; // SYNTAX ERROR.14. cout << "x^2 = " << y << endl;15. 16. y = x^3; // SYNTAX ERROR.17. cout << "x^3 = " << y << endl;18. 19. y = x^4; // SYNTAX ERROR.20. cout << "x^4 = " << y << endl;…
ERROR!#include <iostream>using namespace std;
int main(){ double x, y;
cout << "Enter x : "; cin >> x;
x = y; cout << "x^2 = " << y * y << endl; cout << "x^3 = " << y * y * y << endl; cout << "x^4 = " << y * y * y * y << endl;
return 0;}
CSE1222: Lecture 3 The Ohio State University 16
What is the error?
> power2Error.exe
Enter x : 2
x^2 = Inf
x^3 = -Inf
x^4 = Inf
>
CSE1222: Lecture 3 The Ohio State University 17
… double x, y;
cout << "Enter x : "; cin >> x;
x = y; cout << "x^2 = " << y * y << endl; cout << "x^3 = " << y * y * y << endl; cout << "x^4 = " << y * y * y * y << endl;…
CSE1222: Lecture 3 The Ohio State University 18
Useful shortcuts The statement: counter = counter + 5;
Can be shortened to counter += 5;
The following shortcuts work similarly (Memorize for the exam!):+=, -=, *=, /=, and %=
Example:product = product * ratio;product *= ratio;
Caveat (Why?):
product *= ratio + 1;
is the same as
product = product * (ratio + 1);
NOTproduct = product * ratio + 1;
CSE1222: Lecture 3 The Ohio State University 19
Accumulation These types of statements are very useful!
total = total + 5; Start using them!
Trace through this code and track the value of the variable total
total = 0;total += 6; //total is now 6total += 3; //total is now 9total += 8; //total is now 17
CSE1222: Lecture 3 The Ohio State University 20
Counting by 1 Counting by 1 is done a lot by programmers
We have a special shorthand operator (++) for incrementing a variable value by one
All of these are equivalent (which one would you use?):i = i + 1;i += 1;i++;
We also have a special decrement operator (--) for decrementing a variable value by one
i = i - 1;i -= 1;i--;
Programmer prefer using the increment/decrement operators
CSE1222: Lecture 3 The Ohio State University 21
CSE1222: Lecture 3 The Ohio State University 22
Initialization
Initialization is when a variable is assigned a value as it is declared or when assigned to its very first value
There is a shorthand for initializing a variable as it is declared!
CSE1222: Lecture 3 The Ohio State University 23
initExample1.cpp
CSE1222: Lecture 3 The Ohio State University 24
// initialization example
#include <iostream>#include <cmath>
using namespace std;
int main(){ double x(10.0); // initialize x to 10.0
cout << "The reciprocal of 10 is " << 1 / x << endl;
x = 15.0; cout << "The reciprocal of 15 is " << 1 / x << endl;
return 0; // exit program}
initExample2.cpp
CSE1222: Lecture 3 The Ohio State University 25
// initialization example
#include <iostream>#include <cmath>
using namespace std;
int main(){ double x(3.0 + 4.0); // initialize x to 3 + 4 double y(5.0), z(6.0); // initialize y and z
cout << "The reciprocal of 3 + 4 is " << 1 / x << endl; cout << "The reciprocal of " << y << " is " << 1 / y << endl; cout << "The reciprocal of " << z << " is " << 1 / z << endl;
return 0; // exit program}
initExample3.cpp
CSE1222: Lecture 3 The Ohio State University 26
// C style initialization example
#include <iostream>#include <cmath>
using namespace std;
int main(){ double x = 3.0 + 4.0; // C style initialization of x to 3 + 4 double y = 5.0, z = 6.0; // initialize y and z cout << "The reciprocal of 3+4 is " << 1 / x << endl; cout << "The reciprocal of " << y << " is " << 1 / y << endl; cout << "The reciprocal of " << z << " is " << 1 / z << endl;
return 0; // exit program}
Initialization
Rule of thumb: It is good programming practice to initialize all variables
CSE1222: Lecture 3 The Ohio State University 27
noInit1.cpp
CSE1222: Lecture 3 The Ohio State University 28
// example of missing initialization
#include <iostream>#include <cmath>
using namespace std;
int main(){ double x; double y(123.456);
cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "e^(" << x << ") = " << exp(x) << endl; cout << "x + y = " << x + y << endl;
return 0; // exit program}
noInit1.cpp
CSE1222: Lecture 3 The Ohio State University 29
… double x; double y(123.456);
cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "e^(" << x << ") = " << exp(x) << endl; cout << "x + y = " << x + y << endl;…
> noInit1.exex = 0y = 123.456e^(0) = 1x+y = 123.456
>
noInit2.cpp
CSE1222: Lecture 3 The Ohio State University 30
// example of missing initialization
#include <iostream>#include <cmath>
using namespace std;
int main(){ double y(123.456); double x;
cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "e^(" << x << ") = " << exp(x) << endl; cout << "x + y = " << x + y << endl;
return 0; // exit program}
noInit2.cpp
CSE1222: Lecture 3 The Ohio State University 31
… double y(123.456); double x;
cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "e^(" << x << ") = " << exp(x) << endl; cout << "x + y = " << x + y << endl;…
> noInit2.exex = -7.69536e+304y = 123.456e^(-7.69536e+304) = 0x+y = -7.69536e+304
>
undeclaredVariable.cpp1. // Examples of an undeclared variable2. 3. #include <iostream>4. #include <cmath>5. using namespace std;6. 7. int main()8. {9. double y(0.0);10. y = 2.0 + x;11. 12. double x(0.0);13. x = 3.0 * 4.0;14. 15. cout << "x = " << x << endl;16. cout << "y = " << y << endl;17. 18. return 0;19. }
CSE1222: Lecture 3 The Ohio State University 32
> g++ undeclaredVariable.cpp –o undeclaredVariable.exeundeclaredVariable.cpp: In function `int main()':undeclaredVariable.cpp:10: `x' undeclared (first use this function)undeclaredVariable.cpp:10: (Each undeclared identifier is reported only once for each function it appears in.)>
CSE1222: Lecture 3 The Ohio State University 33
…7. int main()8. {9. double y(0.0);10. y = 2.0 + x;11. 12. double x(0.0);13. x = 3.0 * 4.0;14. 15. cout << "x = " << x << endl;16. cout << "y = " << y << endl;…
undefinedVariable.cpp// Examples of an undefined variable
#include <iostream>#include <cmath>using namespace std;
int main(){ double y; double x;
y = 2.0 + x; x = 3.0 * 4.0;
cout << "x = " << x << endl; cout << "y = " << y << endl;
return 0;}
CSE1222: Lecture 3 The Ohio State University 34
Try running this program!
Warning
Remember: Make sure you have already assigned values to variables BEFORE they are used in these computations!
CSE1222: Lecture 3 The Ohio State University 35
CSE1222: Lecture 3 The Ohio State University 36
Coercion Assigning an integer to a floating point variable
converts the integer to a floating point number
Example:
double y = 3;
Assigning a floating point number to an integer truncates the number and converts it to an integer
Example:
int x = 3.4;
CSE1222: Lecture 3 The Ohio State University 37
coercion.cpp1. // assigning float to integer or integer to float2. 3. #include <iostream>4. using namespace std;5. 6. int main()7. {8. int x(0);9. double y(0.0);10. 11. x = 3.4; // assign floating point number to int12. y = 3; // assign integer to a floating point variable13. 14. cout << "x = " << x << endl; // x equals 315. cout << "y = " << y << endl; // 3 is output, but y is still a float16. 17. cout << "1/x = " << 1/x << endl; // 0 since x is an integer18. cout << "1/y = " << 1/y << endl; // 0.333333 since y is a
float19.
20. return 0;21. }
CSE1222: Lecture 3 The Ohio State University 38
> g++ coercion.cpp –o coercion.execoercion.cpp: In function `int main()':coercion.cpp:11: warning: assignment to `int' from `double'coercion.cpp:11: warning: argument to `int' from `double'>
CSE1222: Lecture 3 The Ohio State University 39
…8. int x(0);9. double y(0.0);10. 11. x = 3.4; // assign floating point number to int12. y = 3; // assign integer to a floating point variable13. 14. cout << "x = " << x << endl; // x equals 3.15. cout << "y = " << y << endl; // 3 is output.. but y is still a
float16. 17. cout << "1/x = " << 1/x << endl; // 0 since x is an integer18. cout << "1/y = " << 1/y << endl; // 0.333333 since y is a float…
> coercion.exex = 3y = 31/x = 01/y = 0.333333>
CSE1222: Lecture 3 The Ohio State University 40
…8. int x(0);9. double y(0.0);10. 11. x = 3.4; // assign floating point number to int12. y = 3; // assign integer to a floating point variable13. 14. cout << "x = " << x << endl; // x equals 3.15. cout << "y = " << y << endl; // 3 is output.. but y is still a float16. 17. cout << "1 / x = " << 1 / x << endl; // 0 since x is an integer18. cout << "1 / y = " << 1 / y << endl; // 0.333333 since y is a float…
Coercion Remember the data type character?
A character is represented as an integer with size of a single byte
Assigning an integer to a char variable converts the integer to a character
Example:
char c = 88; // c is now ‘X’// See your ASCII
table
CSE1222: Lecture 3 The Ohio State University 41
coercion2.cpp#include <iostream>using namespace std;int main(){ int x1(71), x2(111), x3(32), x4(66), x5(117), x6(99), x7(107), x8(115),
x9(33); char c1, c2, c3, c4, c5, c6, c7, c8, c9;
c1 = x1; c2 = x2; c3 = x3; c4 = x4; c5 = x5; c6 = x6; c7 = x7; c8 = x8; c9 = x9;
cout << c1 << c2 << c3 << c4 << c5 << c6 << c7 << c8 << c9 << endl;
return 0;}
CSE1222: Lecture 3 The Ohio State University 42
What is the output?