# CSE202: Lecture 3The Ohio State University1 Assignment.

## Presentation on theme: "CSE202: Lecture 3The Ohio State University1 Assignment."— Presentation transcript:

CSE202: Lecture 3The Ohio State University1 Assignment

CSE202: Lecture 3The Ohio State University2 Assignment Operations The C++ assignment operator is: = Examples: x = 3*5; y = x – 7; y = y + 4; Do not confuse assignment for equality!

CSE202: Lecture 3The Ohio State University3 Assignment Operations A variable, and only one variable, must be on the left. Any 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.)

CSE202: Lecture 3The Ohio State University4 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); Variables (i.e., x,) must be defined before they are used.

CSE202: Lecture 3The Ohio State University5 average.cpp // Compute the average of three numbers. #include 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; }

CSE202: Lecture 3The Ohio State University6 > average.exe Enter three numbers : 2.5 3.1 0.2 The average is: 1.93333 > … 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; …

CSE202: Lecture 3The Ohio State University7 bmi.cpp // Compute the body mass index (BMI) #include 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; }

CSE202: Lecture 3The Ohio State University8 simpleslope.cpp // Compute the slope between two points #include 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; }

CSE202: Lecture 3The Ohio State University9 > 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 > … 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? CSE202: Lecture 3The Ohio State University10... xdiff = x1 - x2; ydiff = y1 - y2; slope = ydiff / xdiff; cout << "The slope is: " << slope << endl;...

CSE202: Lecture 3The Ohio State University11 Assignment Variations 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.

CSE202: Lecture 3The Ohio State University12 power.cpp #include 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; }

CSE202: Lecture 3The Ohio State University13 > power.exe Enter x : 1.5 x^2 = 2.25 x^3 = 3.375 x^4 = 5.0625 > … 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; …

CSE202: Lecture 3The Ohio State University14 ERROR! #include 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; }

CSE202: Lecture 3The Ohio State University15 > 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^' > … 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; …

CSE202: Lecture 3The Ohio State University16 ERROR! #include 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; } What is the error?

CSE202: Lecture 3The Ohio State University17 > power2Error.exe Enter x : 2 x^2 = Inf x^3 = -Inf x^4 = Inf > … 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; …

Assignment Variations CSE202: Lecture 3The Ohio State University18

CSE202: Lecture 3The Ohio State University19 Useful shortcuts counter = counter + 5; can be shortened to counter += 5; The following shortcuts work in the same way: +=, -=, *=, /=, and %= Example: product = product * ratio; product *= ratio; Caveat: product *= ratio + 1; is the same as product = product * (ratio + 1); NOT product = product * ratio + 1;

CSE202: Lecture 3The Ohio State University20 Accumulation Assignments like total = total + 5; are quite useful in programming. For instance: total = 0; total += 6; //total is now 6 total += 3; //total is now 9 total += 8; //total is now 17

CSE202: Lecture 3The Ohio State University21 Counting by 1 Counting by 1 is so common and useful that there is a special operator for it called the increment operator (++). All of these are equivalent: i = i + 1; or i += 1; or i++; Similarly, the decrement operator (--) can be used to decrease a variable by 1. i = i - 1; or i -= 1; or i--;

Variable Initialization CSE202: Lecture 3The Ohio State University22

CSE202: Lecture 3The Ohio State University23 Initialization Variables can be assigned a value as they are declared. This is called initializing.

CSE202: Lecture 3The Ohio State University24 initExample1.cpp // initialization example #include 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 }

CSE202: Lecture 3The Ohio State University25 initExample2.cpp // initialization example #include 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 }

CSE202: Lecture 3The Ohio State University26 initExample3.cpp // C style initialization example #include 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 }

CSE202: Lecture 3The Ohio State University27 Initialization It is good programming practice to initialize all variables.

CSE202: Lecture 3The Ohio State University28 noInit1.cpp // example of missing initialization #include 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 }

CSE202: Lecture 3The Ohio State University29 noInit1.cpp … 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.exe x = 0 y = 123.456 e^(0) = 1 x+y = 123.456 >

CSE202: Lecture 3The Ohio State University30 noInit2.cpp // example of missing initialization #include 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 }

CSE202: Lecture 3The Ohio State University31 noInit2.cpp … 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.exe x = -7.69536e+304 y = 123.456 e^(-7.69536e+304) = 0 x+y = -7.69536e+304 >

CSE202: Lecture 3The Ohio State University32 undeclaredVariable.cpp 1.// Examples of an undeclared variable 2. 3.#include 4.#include 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.}

CSE202: Lecture 3The Ohio State University33 > g++ undeclaredVariable.cpp –o undeclaredVariable.exe undeclaredVariable.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.) > … 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; …

CSE202: Lecture 3The Ohio State University34 undefinedVariable.cpp // Examples of an undefined variable #include 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; } Try running this program!

CSE202: Lecture 3The Ohio State University35 Warning Make sure you have already assigned values to variables before they are used in these computations!

Coercion CSE202: Lecture 3The Ohio State University36

CSE202: Lecture 3The Ohio State University37 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;

CSE202: Lecture 3The Ohio State University38 coercion.cpp 1.// assigning float to integer or integer to float 2. 3.#include 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 int 12. y = 3; // assign integer to a floating point variable 13. 14. cout << "x = " << x << endl; // x equals 3. 15.cout << "y = " << y << endl; // 3 is output.. but y is still a float 16. 17.cout << "1/x = " << 1/x << endl; // 0 since x is an integer 18.cout << "1/y = " << 1/y << endl; // 0.333333 since y is a float 19. 20.return 0; 21.}

CSE202: Lecture 3The Ohio State University39 > g++ coercion.cpp –o coercion.exe coercion.cpp: In function `int main()': coercion.cpp:11: warning: assignment to `int' from `double' coercion.cpp:11: warning: argument to `int' from `double' > … 8. int x(0); 9. double y(0.0); 10. 11. x = 3.4; // assign floating point number to int 12. y = 3; // assign integer to a floating point variable 13. 14. cout << "x = " << x << endl; // x equals 3. 15. cout << "y = " << y << endl; // 3 is output.. but y is still a float 16. 17. cout << "1/x = " << 1/x << endl; // 0 since x is an integer 18. cout << "1/y = " << 1/y << endl; // 0.333333 since y is a float …

CSE202: Lecture 3The Ohio State University40 > coercion.exe x = 3 y = 3 1/x = 0 1/y = 0.333333 > … 8. int x(0); 9. double y(0.0); 10. 11. x = 3.4; // assign floating point number to int 12. y = 3; // assign integer to a floating point variable 13. 14. cout << "x = " << x << endl; // x equals 3. 15. cout << "y = " << y << endl; // 3 is output.. but y is still a float 16. 17. cout << "1/x = " << 1/x << endl; // 0 since x is an integer 18. cout << "1/y = " << 1/y << endl; // 0.333333 since y is a float …

CSE202: Lecture 3The Ohio State University41 Coercion Assigning an integer to a char variable converts the integer to a character. Example: char c = 88; // c is now ‘X’

CSE202: Lecture 3The Ohio State University42 coercion2.cpp #include 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; } What is the output?