Download presentation
Presentation is loading. Please wait.
Published bySteven Charley Modified over 9 years ago
1
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE 20232 Lecture 3 – Data Types & Algorithms
2
2 Demo Create symbolic link to your dropbox ln –s /afs/nd.edu/coursefa.06/cse/cse20232.01/dropbox/your_afs id/hw1 mydropbox Submit your assignment Using cp command cp hw1_1.cpp mydropbox/hw1 Using WebFile “Check” hw1_1.cpp and click copy follow links opening folder AFS Home, then mydropbox and then hw1 “click” copy again and you are done
3
3 Reading Assignments Continue reading in Deitel … Week 2 Sections 2.5-2.7, 6.1-6.6 Appendices B, C, E.1-E.2 Week 3 Sections 4.1-4.12, 15.1-15.8 Appendices F.1-F.3
4
4 Important Terms & Concepts Declaration Definition of the type and name of some program element int sum; Statement Line of code causing some action to occur cout << “hello world!” << endl; Literal A value, literally 237 “Sum = “
5
5 Important Terms & Concepts Variable (or object) A named program element containing a value that can be changed int x, y, sum; Symbolic Constant A named value that remains constant throughout its existence const int MAX = 32; #define PI 3.14159 Keyword Predefined names in C/C++ having a meaning that cannot be changed int, if, while
6
6 Important Terms & Concepts Data Types Types of values that may be used in C++ programs (some predefined, some user-defined) int, float, char, string, complex, … Assignment Setting of the value of a named object/variable x = 25.5; Expression Sequence of code that computes a value a*x*x + b*x +c;
7
7 Important Terms & Concepts Cast Explicit change in data type (int) x Coercion Implicit/forced change in data type Precedence Relative ordering of operators, determining the order in which the operations are performed
8
8 Fundamental Data Types Boolean type bool true, false Character types char 0..255 : single byte interpreted as ASCII character wchar_t Multiple bytes for representing Unicode characters
9
9 Fundamental Data Types Integer types signed char, short int, int, long int In order by increasing range of values and memory size -128.. 127 -32,768.. 32,767 -2,147,483,648.. 2,147,483,648 unsigned char, unsigned short int, unsigned int, unsigned long int 0.. 255, 0.. 65,535, 0.. 4,294,967,295
10
10 Fundamental Data Types Floating Point types (real numbers) float double long double In order by increasing magnitude, precision and memory size Float +/- x.xxxxx * 10 +/-37 Double +/- x.xxxxxxxxx * 10 +/-37
11
11 Identifier Names Identifiers are names of variables, objects, classes, functions, constants, … A valid C/C++ identifier May contain letters, numerical digits, and underscores Must begin with a letter or underscore Examples Good: firstName, sum, vector1, last_name Bad: 12th_digit, half-way, %interest
12
12 Assignments Assigns the value of the right hand side to the object on the left hand side of the operator int x; double y, z; x = 1; // assign x the value 1 y = 5.75; // assign y the value 5.75 z = x + y; // assign z the sum x+y // add the product x*y to the current value of // z and sets z’s new value to the result z = z + x * y;
13
13 Integer Arithmetic Given:int x = 23; Addition: x + 5 equals 28 Subtraction: x – 5 equals 18 Multiplication: x * 5 equals 115 Division (quotient): x / 5 equals 4 Division (remainder): x % 5 equals 3 (* / %) have higher precedence than (+ -) Evaluation is left to right in absence of parentheses
14
14 Floating Point Arithmetic Givendouble x = 23.5; Addition: x + 5.0 equals 28.5 Subtraction: x - 5.0 equals 18.5 Multiplication: x * 5.0 equals 117.5 Division: x / 5.0 equals 4.7 (* /) have higher precedence than (+ -) Evaluation is left to right in absence of parentheses
15
15 Order of Evaluation Basic order is left to right Precedence of operators is set High to low is generally Multiplicative operators (* / %) Additive operators (+ -) Comparisons ( = == !=) Logical operators (&& ||) Complete list is in Appendix A of textbook Parentheses ( ) can be used to alter order of evaluation
16
16 Sample Program – circle.cpp // circle.cpp – JHS - compute circumference & area // of circle, given its radius #include #define PI 3.14159 using namespace std; int main () { double radius, area, circ; cout << “Enter radius of circle: “; cin >> radius; circ = 2.0 * PI * radius; area = PI * radius * radius; cout << “Circumference = “ << circ << “ Area = “ << area << endl; return 0; }
17
17 Sample Program – circle2.cpp // circle2.cpp – JHS - compute circumference & area // of circle, given its radius (formatted output) #include #include // for I/O manipulators #define PI 3.14159 using namespace std; int main () { double radius, area, circ; cout << “Enter radius of circle: “; cin >> radius; circ = 2.0 * PI * radius; area = PI * radius * radius; cout << fixed << setprecision(3) << “Circumference = “ << circ << “ Area = “ << area << endl; return 0; }
18
18 Sample Program – disk.cpp // disk.cpp – JHS - compute surface area and volume of disk, given its radius and thickness #include #include // for I/O manipulators const double PI = 3.14159; // PI value defined differently using namespace std; int main () { double radius, thickness, area, volume; cout << “Enter radius and thickness of disk: “; cin >> radius >> thickness; area = PI * radius * radius; volume = (2.0 * area) + (2.0 * PI * radius * thickness); cout << fixed << setprecision(3) << “Area = “ << area << “ Volume = “ << volume << endl; return 0; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.