Upload
katarina-wilds
View
220
Download
0
Tags:
Embed Size (px)
Citation preview
CSE202: Lecture 3 The Ohio State University 2
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 3 The Ohio State University 3
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 3 The Ohio State University 4
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 3 The Ohio State University 5
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;}
CSE202: Lecture 3 The Ohio State University 6
> average.exeEnter three numbers : 2.5 3.1 0.2The 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 3 The Ohio State University 7
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;}
CSE202: Lecture 3 The Ohio State University 8
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;}
CSE202: Lecture 3 The Ohio State University 9
> 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 3 The Ohio State University 10
... xdiff = x1 - x2; ydiff = y1 - y2; slope = ydiff / xdiff; cout << "The slope is: " << slope << endl;...
CSE202: Lecture 3 The Ohio State University 11
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 3 The Ohio State University 12
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;}
CSE202: Lecture 3 The Ohio State University 13
> 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 3 The Ohio State University 14
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;}
CSE202: Lecture 3 The Ohio State University 15
> 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 3 The Ohio State University 16
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;}
What is the error?
CSE202: Lecture 3 The Ohio State University 17
> 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;…
CSE202: Lecture 3 The Ohio State University 19
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 asproduct = product * (ratio + 1);
NOTproduct = product * ratio + 1;
CSE202: Lecture 3 The Ohio State University 20
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 3 The Ohio State University 21
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--;
CSE202: Lecture 3 The Ohio State University 23
Initialization
• Variables can be assigned a value as they are declared. This is called initializing.
CSE202: Lecture 3 The Ohio State University 24
initExample1.cpp// 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}
CSE202: Lecture 3 The Ohio State University 25
initExample2.cpp// 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}
CSE202: Lecture 3 The Ohio State University 26
initExample3.cpp// 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}
CSE202: Lecture 3 The Ohio State University 27
Initialization
• It is good programming practice to initialize all variables.
CSE202: Lecture 3 The Ohio State University 28
noInit1.cpp// 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}
CSE202: Lecture 3 The Ohio State University 29
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.exex = 0y = 123.456e^(0) = 1x+y = 123.456
>
CSE202: Lecture 3 The Ohio State University 30
noInit2.cpp// 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}
CSE202: Lecture 3 The Ohio State University 31
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.exex = -7.69536e+304y = 123.456e^(-7.69536e+304) = 0x+y = -7.69536e+304
>
CSE202: Lecture 3 The Ohio State University 32
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. }
CSE202: Lecture 3 The Ohio State University 33
> 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.)>
…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 3 The Ohio State University 34
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;}
Try running this program!
CSE202: Lecture 3 The Ohio State University 35
Warning
Make sure you have already assigned values to variables before they are used in these computations!
CSE202: Lecture 3 The Ohio State University 37
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 3 The Ohio State University 38
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 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 float19.20. return 0;21. }
CSE202: Lecture 3 The Ohio State University 39
> 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'>
…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…
CSE202: Lecture 3 The Ohio State University 40
> coercion.exex = 3y = 31/x = 01/y = 0.333333>
…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…
CSE202: Lecture 3 The Ohio State University 41
Coercion
• Assigning an integer to a char variable converts the integer to a character.
• Example:
char c = 88;
// c is now ‘X’
CSE202: Lecture 3 The Ohio State University 42
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;}
What is the output?