CSE202: Lecture 3The Ohio State University1 Assignment.

Slides:



Advertisements
Similar presentations
1.1 Line Segments, Distance and Midpoint
Advertisements

Lecture Computer Science I - Martin Hardwick Arithmetic Expressions With Integers rOperators:result is always an integer SymbolNameExampleValue (x.
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
1.4 Linear Equations in Two Variables
Chapter 2: Basic Elements of C++
O A Corpo 1 Cinemática e Cinética de Partículas no Plano e no Espaço Análise Dinâmica dos Corpos O X Y X1X1 Y1Y1 X2X2 Y2Y2 X3X3 Y3Y3 A B P l = 75 mm l.
Graphs & Linear Equations
The Derivative in Graphing and Application
Slope of a Line 11-2 Warm Up Problem of the Day Lesson Presentation
Calculating Slope m = y2 – y1 x2 – x1.
Slope Problems.
Graphing Lines Day 0ne. Cover the concepts: Relation Function
On / By / With The building blocks of the Mplus language.
Gradient of a straight line x y 88 66 44 2 44 4 For the graph of y = 2x  4 rise run  = 8  4 = 2 8 rise = 8 4 run = 4 Gradient = y.
CS 240 Computer Programming 1
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
1 Lecture 5 PRAM Algorithm: Parallel Prefix Parallel Computing Fall 2008.
BINARY/MIXED-INTEGER PROGRAMMING ( A SPECIAL TYPE OF INTEGER PROGRAMMING)
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
CSE202: Lecture 1The Ohio State University1 Introduction to C++
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
CSE202: Lecture 14The Ohio State University1 Arrays.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Completing the Basic (L06)
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Variables and memory addresses
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
CS 240 Computer Programming 1
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
The Ohio State University
Introduction to Computer Programming
Rational Expressions. relational operators. logical operators
Bill Tucker Austin Community College COSC 1315
Computing Fundamentals
LESSON 4 Decision Control Structure
The Ohio State University
Chapter 2 Assignment and Interactive Input
Programming Fundamentals
Chapter 2 Elementary Programming
Lecture 3 Expressions Richard Gesick.
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
3x 2x -5 x + 11 (4x + 7)° 90° (8x - 1)°.
Screen output // Definition and use of variables
הרצאה 03 אבני היסוד של תוכנית ב- C
Counting Loops.
Data Types and Expressions
Combining Like Terms TeacherTwins©2014.
Life is Full of Alternatives
Data Types and Expressions
Data Types and Expressions
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 = x; z = 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 : The average is: > … 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 * / 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 = x^4 = > … 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; y = x^3; // SYNTAX ERROR. 17. cout << "x^3 = " << y << endl; 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( ); // 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 = ; // 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( ); 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( ); 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 = e^(0) = 1 x+y = >

CSE202: Lecture 3The Ohio State University30 noInit2.cpp // example of missing initialization #include using namespace std; int main() { double y( ); 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( ); 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 = e+304 y = e^( e+304) = 0 x+y = e+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 = x; double x(0.0); 13. x = 3.0*4.0; cout << "x = " << x << endl; 16. cout << "y = " << y << endl; 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 = x; double x(0.0); 13. x = 3.0*4.0; 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 = 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); x = 3.4; // assign floating point number to int 12. y = 3; // assign integer to a floating point variable cout << "x = " << x << endl; // x equals cout << "y = " << y << endl; // 3 is output.. but y is still a float cout << "1/x = " << 1/x << endl; // 0 since x is an integer 18.cout << "1/y = " << 1/y << endl; // since y is a float 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); x = 3.4; // assign floating point number to int 12. y = 3; // assign integer to a floating point variable cout << "x = " << x << endl; // x equals cout << "y = " << y << endl; // 3 is output.. but y is still a float cout << "1/x = " << 1/x << endl; // 0 since x is an integer 18. cout << "1/y = " << 1/y << endl; // since y is a float …

CSE202: Lecture 3The Ohio State University40 > coercion.exe x = 3 y = 3 1/x = 0 1/y = > … 8. int x(0); 9. double y(0.0); x = 3.4; // assign floating point number to int 12. y = 3; // assign integer to a floating point variable cout << "x = " << x << endl; // x equals cout << "y = " << y << endl; // 3 is output.. but y is still a float cout << "1/x = " << 1/x << endl; // 0 since x is an integer 18. cout << "1/y = " << 1/y << endl; // 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?