CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.

Slides:



Advertisements
Similar presentations
CSE202: Lecture 3The Ohio State University1 Assignment.
Advertisements

1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
True or false A variable of type char can hold the value 301. ( F )
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Computer Science 1620 Loops.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
CS150 Introduction to Computer Science 1
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Compound Operators 03/07/11. More Operators, First Section 4.4.
CSE202: Lecture 10AThe Ohio State University1 Numerical Error.
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.
Basic Elements of C++ Chapter 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Operaciones y Variables
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Completing the Basic (L06)
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Previously Repetition Structures While, Do-While, For.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
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.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
CSE1222: Lecture 1The Ohio State University1. Computing Basics  Computers CPU, Memory & Input/Output (IO)  Program Sequence of instructions for the.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
The Ohio State University
Introduction to Computer Programming
CSE 220 – C Programming Expressions.
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++ Chapter 1.
Computing and Statistical Data Analysis Lecture 2
Chapter 3 Assignment and Interactive Input.
Computing Fundamentals
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Basic Elements of C++ Chapter 2.
OPERATORS (1) CSC 111.
Chapter 2 Elementary Programming
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
Counting Loops.
CS150 Introduction to Computer Science 1
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

CSE1222: Lecture 3The Ohio State University1

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 3The Ohio State University2

Assignment Operations  Remember our rule: 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) CSE1222: Lecture 3The Ohio State University3

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

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; } CSE1222: Lecture 3The Ohio State University5

> average.exe Enter three numbers : The average is: > CSE1222: Lecture 3The Ohio State University6 … 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 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; } CSE1222: Lecture 3The Ohio State University7

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; } CSE1222: Lecture 3The Ohio State University8

> 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 3The Ohio State University9 … 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 3The Ohio State University10... 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 3The Ohio State University11

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; } CSE1222: Lecture 3The Ohio State University12

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

> 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 3The Ohio State University15 … 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; …

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; } CSE1222: Lecture 3The Ohio State University16 What is the error?

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

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); NOT product = product * ratio + 1; CSE1222: Lecture 3The Ohio State University19

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 6 total += 3; //total is now 9 total += 8; //total is now 17 CSE1222: Lecture 3The Ohio State University20

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 3The Ohio State University21

CSE1222: Lecture 3The Ohio State University22

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 3The Ohio State University23

initExample1.cpp CSE1222: Lecture 3The Ohio State University24 // 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 }

initExample2.cpp CSE1222: Lecture 3The Ohio State University25 // initialization example #include using namespace std; int main() { double x( );// initialize x to double y(5.0), z(6.0);// initialize y and z cout << "The reciprocal of 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 3The Ohio State University26 // C style initialization example #include using namespace std; int main() { double x = ; // C style initialization of x to 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 3The Ohio State University27

noInit1.cpp CSE1222: Lecture 3The Ohio State University28 // 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 }

noInit1.cpp CSE1222: Lecture 3The Ohio State University29 … 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 = >

noInit2.cpp CSE1222: Lecture 3The Ohio State University30 // 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 }

noInit2.cpp CSE1222: Lecture 3The Ohio State University31 … 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 >

undeclaredVariable.cpp 1. // Examples of an undeclared variable #include 4. #include 5. using namespace std; 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. } CSE1222: Lecture 3The Ohio State University32

> 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.) > CSE1222: Lecture 3The Ohio State University33 … 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; …

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; } CSE1222: Lecture 3The Ohio State University34 Try running this program!

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

CSE1222: Lecture 3The Ohio State University36

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 3The Ohio State University37

coercion.cpp 1. // assigning float to integer or integer to float #include 4. using namespace std; 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. } CSE1222: Lecture 3The Ohio State University38

> 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' > CSE1222: Lecture 3The Ohio State University39 … 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 …

> coercion.exe x = 3 y = 3 1/x = 0 1/y = > CSE1222: Lecture 3The Ohio State University40 … 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 …

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 3The Ohio State University41

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; } CSE1222: Lecture 3The Ohio State University42 What is the output?