CSE202: Lecture 2 The Ohio State University 1
// math example
#include <iostream>
#include <cmath> using namespace std;
{ int main() cout << "The reciprocal of 10 is " << 1.0/10.0 << endl; cout << "The square root of 10 is " << sqrt(10.0) << endl; cout << "e^(10.0) = " << exp(10.0) << endl; cout << "The reciprocal of 15 is " << 1.0/15.0 << endl; cout << "The square root of 15 is " << sqrt(15.0) << endl; cout << "e^(15.0) = " << exp(15.0) << endl;
} return 0; // exit program
CSE202: Lecture 2 The Ohio State University 2
> g++ -o mathExample2.exe mathExample2.cpp
> mathExample2.exe
The reciprocal of 10 is 0.1
The square root of 10 is 3.16228
e^(10.0) = 22026.5
The reciprocal of 15 is 0.0666667
The square root of 15 is 3.87298
e^(15.0) = 3.26902e+06
>
CSE202: Lecture 2 The Ohio State University 3
• Programs like mathExample2.cpp have little use in practice…
• There is no room for change. They simply print the same output onto the screen every time they are executed.
• A more interesting program might have different behavior under different circumstances.
CSE202: Lecture 2 The Ohio State University 4
• For instance, a program that asks for a number and outputs its reciprocal and square root is much more useful than mathExample2.cpp.
– This program needs placeholders for the incoming values.
– These placeholders are called variables
CSE202: Lecture 2 The Ohio State University 5
// math example
#include <iostream>
#include <cmath>
using namespace std;
{ int main() double x; x = 10.0; cout << "The reciprocal of 10 is " << 1.0/x << endl; cout << "The square root of 10 is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl; x = 15.0; cout << "The reciprocal of 15 is " << 1.0/x << endl; cout << "The square root of 15 is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl;
} return 0; // exit program
CSE202: Lecture 2 The Ohio State University 6
• Before you can use a variable, you must declare it.
This allows the computer to set aside the memory that will be needed for that variable.
• Variables consist of a name and its data type. C++ variable declarations are of the form: dataType variableName;
• dataType can be: int, float, char, double, unsigned int, ...
• variableName must be composed of alphanumeric characters or underscore ‘_’.
CSE202: Lecture 2 The Ohio State University 7
#include <iostream> using namespace std; int main()
{ int age; float wage; char initial; double height;
} return 0; // exit program
CSE202: Lecture 2 The Ohio State University 8
• Variable names are composed of the characters: a,b,c,..,z,A,B,C,…,Z,0,1,2,…,9 and _.
• Variables names must begin with: a,b,c,..,z,A,B,C,…,Z or _.
• Capitalized and lower case letters are different.
• Examples: int age; int Age; int age1; int age2; int myAge; int Jacks_age; int age3B; int _age;
CSE202: Lecture 2 The Ohio State University 9
Which of these are valid variable names?
• me2
• Me2
• 2L8
• R-Wenger
• He_who_hesitates_is_lost
• HeWhoHesitatesIsLost
• Gordon.Gee
CSE202: Lecture 2 The Ohio State University 10
• Variables can be assigned values during or after declaration, but never before (why?)
• Assignment is done with the equals sign height = 67.34; initial = ‘E’; //initial = E; will not work as expected double totalPay = salary + overtime;
• Variable assignment is NOT commutative! The variable must always be on the left side of an equals sign. The following is NOT valid:
67 = x;
CSE202: Lecture 2 The Ohio State University 11
// math example
#include <iostream>
#include <cmath> using namespace std;
{ int main() double x; cout << "Enter x: "; // Note: no new line cin >> x; // Note: operator ">>“, not operator "<<" cout << "The reciprocal of " << x << " is " << 1.0/x << endl; cout << "The square root of " << x << " is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl;
} return 0; // exit program
CSE202: Lecture 2 The Ohio State University 12
cin
… double x; cout << "Enter x: "; // Note: no new line cin >> x; // Note: operator ">>“, not operator "<<"
…
• cin (Console INput) can be used to obtain user input .
• Unlike cout, use >> with cin, and not <<
• When the program is run, cin will wait indefinitely for user input.
• cin will input a single value into a variable when it detects a new line from the input:
• Remember that before using inputting values into variables, the variables MUST have already been declared!
CSE202: Lecture 2 The Ohio State University 13
{
… int main() double x; cout << "Enter x: "; // Note: no new line cin >> x; // Note: operator ">>“, not operator "<<" cout << "The reciprocal of " << x << " is " << 1.0/x << endl; cout << "The square root of " << x << " is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl;
…
> mathExample4.exe
Enter x: 10.0
The reciprocal of 10 is 0.1
The square root of 10 is 3.16228
e^(10) = 22026.5
>
CSE202: Lecture 2 The Ohio State University 14
// math example
#include <iostream>
#include <cmath> using namespace std;
{ int main() double x; cout << "Enter x: "; // Note: no new line cin >> x; // Note: operator ">>“, not operator "<<“ cout << "The reciprocal of " << x << " is " << 1.0/x << endl; cout << "The square root of " << x << " is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl;
} return 0; // exit program
Try inputs:
20
-20
1000
-1000
0
-0
CSE202: Lecture 2 The Ohio State University 15
// multiple declarations example
#include <iostream> using namespace std;
{ int main() double x; double y; double z; cout << "Enter x, y and z: "; cin >> x; cin >> y; cin >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl;
} return 0; // exit program
CSE202: Lecture 2 The Ohio State University 16
… cout << "Enter x, y and z: "; cin >> x; cin >> y; cin >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl;
…
> xyz1.exe
Enter x, y and z: 1 2 3 x = 1 y = 2 z = 3
>
CSE202: Lecture 2 The Ohio State University 17
… cout << "Enter x, y and z: "; cin >> x; cin >> y; cin >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl;
…
> xyz1.exe
Enter x, y and z:
1
2
3 x = 1 y = 2 z = 3
>
CSE202: Lecture 2 The Ohio State University 18
• In the previous example we had double x; double y; double z;
• This can be done in one statement like this: double x, y, z;
• This is very useful when creating several variables of the same type.
CSE202: Lecture 2 The Ohio State University 19
// multiple declarations example
#include <iostream> using namespace std;
{ int main() double x, y, z; // multiple declarations cout << "Enter x, y and z: "; cin >> x; cin >> y; cin >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl;
} return 0; // exit program
CSE202: Lecture 2 The Ohio State University 20
// multiple declarations example
#include <iostream> using namespace std;
{ int main() double x, y, z; cout << "Enter x, y and z: "; cin >> x >> y >> z; // read x, then y, then z cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl;
} return 0; // exit program
CSE202: Lecture 2 The Ohio State University 21
cin
• cin can be used to obtain multiple inputs.
• cin knows when to delimit (i.e. start looking for the next input) upon reaching a
“whitespace.”
• Whitespaces are: tabs, spaces, new lines
CSE202: Lecture 2 The Ohio State University 22
… cout << "Enter x, y and z: "; cin >> x >> y >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl;
…
> xyz1.exe
Enter x, y and z: 1 2 3 x = 1 y = 2 z = 3
>
CSE202: Lecture 2 The Ohio State University 23
… cout << "Enter x, y and z: "; cin >> x >> y >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl;
…
> xyz1.exe
Enter x, y and z:
1
2
3 x = 1 y = 2 z = 3
>
CSE202: Lecture 2 The Ohio State University 24
• Sometimes it makes more sense to break up multiple inputs into single inputs, even if they are correlated: int x, y, z; cout << “Enter x: “; cin >> x; cout << “Enter y: “; cin >> y; cout << “Enter z: “; cin >> z;
CSE202: Lecture 2 The Ohio State University 25
Which of the following C++ statements have syntax errors and what are the errors?
• cout >> “Answer = ” >> x+y >> endl;
• cin << x;
• cout << “Yes, ” << “ or ” << “ no “ << “ or ”
<< “ maybe.” << endl;
• cin >> yes >> no >> maybe;
• cout << “x + y = ” (x + y) << endl;
CSE202: Lecture 2 The Ohio State University 26
• Variables are used to hold data within a program (the data is held in your computer’s main memory). A program can read-from and write-to variables. That is, their values can vary .
• Every variable consists of two parts:
1. The name of a variable is tied to a location in memory
2. Its data type (discussed next...)
CSE202: Lecture 2 The Ohio State University 27
• An integer value is any number that has no decimal point.
123 -45000 +1432431 0 are all valid integers
• 1,244 is not a valid integer in C++; no commas are used. Neither is $12 because $ is not a valid part of an integer.
CSE202: Lecture 2 The Ohio State University 28
• The largest and smallest integers supported is dependent of the computer on which the program is compiled. Most of today’s computers use 32-bits to represent an integer, so 2 32 values can be represented.
• Integers can be signed or unsigned.
– What is the maximum value of an unsigned 32 bit integer?
– What is the maximum value of a signed 32 bit integer?
CSE202: Lecture 2 The Ohio State University 29
// example using type int
#include <iostream> using namespace std;
{ int main() int x, y; cout << "Enter x and y: "; cin >> x >> y; // Read in x and then y cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "x+y = " << x+y << endl; cout << "x/y = " << x/y << endl; cout << "Done." << endl;
} return 0; // exit program
Try inputs:
17 3
3 17
0 17
17 0
2000000000 2000000000
CSE202: Lecture 2 The Ohio State University 30
• In C++, (15 / 2) is 7. Why?
• Remember that integers have no fractional parts!
• There is no rounding in integer division. If your program contained: cout << (15 / 16); The output would be 0.
• The fractional part of an integer division is truncated so the result can be an integer. If you need the remainder, use %. If you need a decimal answer, make your operands floating-point numbers: cout << (15.0 / 16.0);
CSE202: Lecture 2 The Ohio State University 31
Data Types: Floating Point Numbers (1)
• Floating-point numbers have a decimal point, and they can also be signed or unsigned.
• There are three basic types: float double long double
– The differences between these are their supported range and precision.
CSE202: Lecture 2 The Ohio State University 32
Data Types: Floating Point Numbers (2)
• To represent a floating point number:
– float uses 32 bits (4 bytes)
– double uses 64 bits (8 bytes)
– long double uses 128 bits (16 bytes)
• The tradeoff is storage vs. precision and range
• What exactly is the precision and range, and how are floating point numbers represented in binary format? IEEE 754 Standard
CSE202: Lecture 2 The Ohio State University 33
Data Types: Floating Point Numbers (3)
CSE202: Lecture 2 The Ohio State University 34
Data Types: Floating Point Numbers (4)
• Floating-point numbers can be written in exponential notation:
134.56 or 1.3456e2
-0.00345 or -3.45e-3
• Here, e is short for “times ten to the power of”, just like in scientific notation.
CSE202: Lecture 2 The Ohio State University 35
// example using type double
#include <iostream> using namespace std;
{ int main() double x, y; cout << "Enter x and y: "; cin >> x >> y; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "x+y = " << x+y << endl; cout << “x*y = " << x*y << endl; cout << “x/y = " << x/y << endl; cout << "Done." << endl;
} return 0; // exit program
Try inputs:
17 3
3 17
17 0
4000000000 4000000000
1e100 1e100
1e200 1e200
CSE202: Lecture 2 The Ohio State University 36
• Characters include:
– All letters of the alphabet (upper and lower case)
– The symbolic representation of digits 0 – 9
– All various symbols such as: + * & ^ % $ | , !
• A character value is simply one of these in single quotes, such as 'A' or '8' or ':' or ' ' (blank space).
CSE202: Lecture 2 The Ohio State University 37
• A character value is a character in single quotes, such as 'A' or '8' or ':' or ' ' (blank space).
• NOTE: '8' and 8 are different.
– '8' is the symbolic representation of the character, ‘8’;
– 8 is the integer 8.
38 CSE202: Lecture 2 The Ohio State University
• Characters usually take up 8 bits (1 byte)
• That means there are 2 8 (or 256) different characters, and every number within the range of [0, 255] is mapped onto some character
• So a character really boils down to a numerical representation known as ASCII encoding.
CSE202: Lecture 2 The Ohio State University 39
39
40
41
…
35
36
37
38
Code Char
32 Space
33
34
!
"
#
$
%
&
(
'
)
…
55
56
57
…
51
52
53
54
Code Char
48 0
49
50
1
2
3
4
5
6
7
8
9
…
72
73
74
…
68
69
70
71
Code Char
65 A
66
67
B
C
D
E
F
G
I
H
J
…
Code Char
97 a
98
99 b c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
… …
CSE202: Lecture 2 The Ohio State University 40
CSE202: Lecture 2 The Ohio State University 41
// example using type char
#include <iostream> using namespace std;
{ int main() char c1, c2, c3; cout << "Enter first initial: "; cin >> c1; cout << "Enter second initial: "; cin >> c2; c3 = 'X'; cout << "Created by: "; cout << c1 << c2 << c3 << endl;
} return 0; // exit program
CSE202: Lecture 2 The Ohio State University 42
… char c1, c2, c3; cout << "Enter first initial: "; cin >> c1; cout << "Enter second initial: "; cin >> c2; c3 = 'X'; cout << "Created by: "; cout << c1 << c2 << c3 << endl;
…
> charExample1.exe
Enter first initial: R
Enter second initial: W
{What is the output?}
CSE202: Lecture 2 The Ohio State University 43
• A character string is an array of characters.
• Examples:
– "Hello"
– "Hello World!" (Note: Blank space is part of the string.)
– "He who hesitates is lost.\nHaste makes waste.\n"
– "" (The empty string.)
CSE202: Lecture 2 The Ohio State University 44
• NOTE: 'A' and "A" are different.
– 'A' is the symbolic representation of the character, ‘A’;
– "A" is a string containing a single character.
• NOTE: '8' and "8" and 8 are different.
– '8' is the symbolic representation of the character, ‘8’;
– "8" is a string containing a single character;
– 8 is the integer 8.
CSE202: Lecture 2 The Ohio State University 45
• The Boolean data type is used for just two values: true and false
• Like characters, they only consume 1 byte of storage.
• It is worth noting that when dealing with
Boolean values in C++, any number other than 0 is always interpreted as true.
CSE202: Lecture 2 The Ohio State University 46
• The basic operations of +, - , *, and / are available, as is % which is used for modulus division (returns the remainder).
• A simple arithmetic expression is of the form: operand operator operand
5 % 3
CSE202: Lecture 2 The Ohio State University 47
// Examples of arithmetic operations
#include <iostream> using namespace std;
{ int main() cout << "5 % 3 = " << (5 % 3) << endl; cout << "4 - 3 = " << (4 - 3) << endl; cout << "5.0 / 2.0 = " << (5.0 / 2.0) << endl; cout << "5 / 2 = " << (5 / 2) << endl;
// Is there any real difference in the last two statements?
} return 0;
CSE202: Lecture 2 The Ohio State University 48
… cout << "5 % 3 = " << (5 % 3) << endl; cout << "4 - 3 = " << (4 - 3) << endl; cout << "5.0 / 2.0 = " << (5.0 / 2.0) << endl; cout << "5 / 2 = " << (5 / 2) << endl;
…
>arithmetic.exe
5 % 3 = 2
4 - 3 = 1
5.0 / 2.0 = 2.5
5 / 2 = 2
>
CSE202: Lecture 2 The Ohio State University 49