1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
-
Upload
jeffry-gibson -
Category
Documents
-
view
212 -
download
0
Transcript of 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
![Page 1: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/1.jpg)
1
COMS 261Computer Science I
Title: C++ Fundamentals
Date: September 9, 2005
Lecture Number: 6
![Page 2: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/2.jpg)
2
Announcements
• I need to miss office hours today– ¾ ton of tile in Richmond is calling me
![Page 3: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/3.jpg)
3
Review
• Program Organization
• Objects
• First C++ Program– Hello, World
• Code Warrior IDE
![Page 4: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/4.jpg)
4
Outline
• Area.cpp
• Standard data types– Integer
– Real (float)
– Character
![Page 5: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/5.jpg)
• Write a program to1) Prompt the user for the width and height
of a rectangle
2) Read and store the entered width and height values
3) Compute the area of the rectangle
4) Display the area, width, and height of the specified rectangle
Area Program - Area.cpp
![Page 6: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/6.jpg)
• Prompt the user– What does this mean?
– Output a message on the display similar to: Enter the width and height of the rectangle:
– Do you know how to output a message to the display?• Insert a string literal into the output object: cout
cout << “Enter the width and height of a rectangle “;
Step 1
Insertion operator
![Page 7: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/7.jpg)
• Read and store the entered width and height values– Read from where?
• The keyboard, which is the standard input device: cin
• Extract two values from the keyboard• Save them in variables (objects whose value
can change)
cin >> width >> height;
Step 2
Extraction operator
![Page 8: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/8.jpg)
• Compute the area of a rectangle:
• rectangleArea = width * height
Step 3
![Page 9: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/9.jpg)
• Display the area, width, and height of the specified rectangle– Output a message on the display similar to:
Area: # width: # height: #
– Insert some text and numbers into the standard output object: coutcout << “Area: “ << rectangleArea
<< “width: “ << width
<< “height: “ << height << endl;
Step 4
![Page 10: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/10.jpg)
10
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 11: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/11.jpg)
11
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 12: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/12.jpg)
12
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 13: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/13.jpg)
13
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 14: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/14.jpg)
14
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 15: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/15.jpg)
15
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 16: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/16.jpg)
16
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 17: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/17.jpg)
17
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 18: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/18.jpg)
18
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 19: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/19.jpg)
19
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 20: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/20.jpg)
20
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width; // define a variable called width
float height; // define a variable called height
cin >> width >>height;// extract values for width and height from standard input
float rectangleArea = width * height; // define and initialize the area
cout << “Area: “ << rectangleArea // insert values into the standard output object
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 21: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/21.jpg)
21
Definition with initialization
Definitions
Extraction
Area.cpp#include <iostream> // since we need the insertion and extraction operators
using namespace std; // since we need cout and cin
int main() { // the main function definition starts here
cout << "Rectangle dimensions: "; // insert the prompt into the output
float width;
float height;
cin >> width >>height;
float rectangleArea = width * height;
cout << “Area: “ << rectangleArea
<< “width: “ << width
<< “height: “ << height << endl;
return 0; // end of the program
} // end of the main function
![Page 22: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/22.jpg)
22
Metroworks IDE with Area.cpp
![Page 23: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/23.jpg)
Area.cpp Output
![Page 24: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/24.jpg)
Fundamental C++ Objects
• C++ has a large number of fundamental or built-in object types
• The fundamental object types fall into one of three categories– Integer objects– Floating-point objects– Character objects
1 1.28345
Z5
P3.14
![Page 25: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/25.jpg)
Integer Object Types
• The basic integer object type is int– The size of an int depends on the machine
and the compiler• On PCs it is normally 32 bits• How many different integer numbers can be
represented with 32 bits?
• Other integers object types–short: typically uses less bits (16 bits)–long: sometimes uses more bits (32 bits)
![Page 26: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/26.jpg)
Integer Object Types
• Different types allow programmers to use resources more efficiently
• Standard arithmetic and relational operations are available for these types
![Page 27: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/27.jpg)
Integer Constants
• Integer constants are positive or negative whole numbers
• Integer constant forms– Decimal
– Octal (base 8)• Digits 0, 1, 2, 3, 4, 5, 6, 7
– Hexadecimal (base 16)• Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a , b, c, d, e, f,
A, B, C, D, E, F
![Page 28: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/28.jpg)
Decimal Constants
• Examples– 97
– 40000L
– 50000
– 23a (illegal)
• The type of the constant depends on its size, unless the type specifier is used
L or l indicates long integer
![Page 29: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/29.jpg)
Character Object Types
• Character type char is related to the integer types
• Characters are encoded as 8-bit integers
• ASCII is the dominant encoding scheme– Examples
• ' ' encoded as 32 '+' encoded as 43• 'A' encoded as 65 'Z' encoded as 90• 'a' encoded as 97 'z' encoded as 122• Complete ASCII character set in App. A
![Page 30: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/30.jpg)
Character Operations
• Arithmetic and relational operations are defined for characters types–'a' < 'b' is true–'4' > '3' is true–'6' <= '2' is false
![Page 31: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/31.jpg)
Character Constants• Explicit (literal) characters within single
quotes–'a','D','*'
• Special characters - delineated by a backslash \– Two character sequences (escape codes)– Some important special escape codes
•\t denotes a tab•\n denotes a new line •\\ denotes a backslash
![Page 32: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/32.jpg)
Character Constants•\' denotes a single quote•\" denotes a double quote
•'\t' is the explicit tab character•'\n' is the explicit new line character• and so on…
![Page 33: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/33.jpg)
Literal String Constants
• A literal string constant is a sequence of zero or more characters enclosed in double quotes–"We are even loonier than you think"
–"Rust never sleeps\n"–"Nilla is a Labrador Retriever“
– Not a fundamental type
![Page 34: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/34.jpg)
Floating-Point Object Types
• Floating-point object types represent real numbers– Integer part
– Fractional part
• The number 108.1517 breaks down into the following parts– 108 - integer part
– 1517 - fractional part
![Page 35: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/35.jpg)
Floating-Point Object Types
• C++ provides three floating-point object types– float
– double
– long double
– In most implementations of C++, long double is equivalent to a double
![Page 36: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/36.jpg)
Floating-Point Constants
• Standard decimal notation134.123
0.15F
• Standard scientific notation1.45E6
0.979e-3L
• When not specified, floating-point constants are of type double
F or f indicates single precision floating point value
L or l indicates long double floating point value
![Page 37: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/37.jpg)
Names• Used to denote program values or
components• A valid name is a sequence of
– Letters (upper and lowercase)– Digits
• A name cannot start with a digit
– Underscores• A name should not normally start with an
underscore
![Page 38: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/38.jpg)
Names• Names are case sensitive
– MyObject is a different name than MYOBJECT
• There are two kinds of names– Keywords– Identifiers
![Page 39: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/39.jpg)
Keywords
• Keywords are words reserved as part of the language–int, return, float, double
• They cannot be used by the programmer to name things
• They consist of lowercase letters only
• They have special meaning to the compiler
![Page 40: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/40.jpg)
Identifiers• Identifiers should be
– Short enough to be reasonable to type (single word is norm)• Standard abbreviations are fine (but only
standard abbreviations)
– Long enough to be understandable• When using multiple word identifiers capitalize
the first letter of each word
– Examples•Min, Temperature, CameraAngle, CurrentNbrPoints
![Page 41: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/41.jpg)
Definitions
• All objects that are used in a program must be defined
• An object definition specifies– Type
– Name
• General definition form – Our convention is one definition per
statement!
Type Id, Id, ..., Id;
Knowntype
List of one ormore identifiers
![Page 42: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/42.jpg)
Examples
char Response;
int MinElement;
float Score;
float Temperature;
int i;
int n;
char c;
float x;
Objects are uninitialized with this definition form
(Value of a object is whatever is in itsassigned memory location)
![Page 43: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/43.jpg)
Arithmetic Operators
• Common– Addition +
– Subtraction -
– Multiplication *
– Division /
– Mod %
Write m*x + bnot mx + b
![Page 44: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/44.jpg)
Arithmetic Operators
• Note– No exponentiation operator
– Single division operator• Integer and float
– Operators are overloaded to work with more than one type of object
![Page 45: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/45.jpg)
Integer Division
• Integer division produces an integer result– Truncates the result
• Examples–3 / 2 evaluates to 1–4 / 6 evaluates to 0–10 / 3 evaluates to 3
![Page 46: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/46.jpg)
Mod
• Produces the remainder of the division
• Examples–5 % 2 evaluates to 1–12 % 4 evaluates to 0–4 % 5 evaluates to 4
![Page 47: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/47.jpg)
Operators and Precedence
• Consider mx + b– which of the following is it equivalent to
• (m * x) + b• m * (x + b)
– Operator precedence tells how to evaluate expressions
![Page 48: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/48.jpg)
Operators and Precedence
• Standard precedence order– () Evaluate first, if nested innermost done first
– * / % Evaluate second. If there are several, then evaluate from left-to-right
– + - Evaluate third. If there are several,then evaluate from left-to-right
![Page 49: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/49.jpg)
Operator Precedence
• Examples 20 - 4 / 5 * 2 + 3 * 5 % 4
(4 / 5)
((4 / 5) * 2)
((4 / 5) * 2) (3 * 5)
((4 / 5) * 2) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) + ((3 * 5) % 4)
![Page 50: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/50.jpg)
Defining and Initializing
• When an object is defined using the basic form, the memory allotted to it contains random information
• Better idea to specify its desired value at the same time– Exception is when the next statement is an
extraction for the object
• Remember our convention of one definition per statement!
![Page 51: 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.](https://reader035.fdocuments.in/reader035/viewer/2022070413/5697bfae1a28abf838c9c644/html5/thumbnails/51.jpg)
Examplesint FahrenheitFreezing = 32;char FinalGrade = 'A';cout << "Slope of line: ";float m;cin >> m;cout << "Intercept: ";float b;cin >> b;cout << "X value of interest: ";float x;cin >> x;float y = (m * x) + b;