Lecture Notes for Lecture 1

Download Report

Transcript Lecture Notes for Lecture 1

// A simple C++ program
# include <iostream>
using namespace std;
int main ()
{
cout << “Welcome to CS1005! \n”;
return 0;
}
General comments
•
•
•
•
Use whitespace to make it more readable
Common programming errors
Don’t reinvent the wheel
Maximum warnings on compiler
What not to do with Whitespace
#include <iostream>
using namespace std;int main(){cout<<"Welcome to CS1005!\n";return 0;}
// This program adds two numbers and displays the result
// on the screen
# include <iostream>
using namespace std;
int main ()
{
int result;
result = 2 + 3;
cout << “The sum of 2 + 3 is “ << result << endl;
return 0;
}
Data Type
• Determines how much memory is needed to
store this information
• Determines what sort of information the
data type represents
• Determines which operations are legal on
this information
Rules for Identifiers
•
•
•
•
•
•
Must start with a letter
May only contain letters, digits and underscore character
Must not be a keyword
C++ is case sensitive.
C++ allows any length
Our standard - start with small letter, capitalize first letter
of each word (avgTemp)
• Should be meaningful. Reflect it’s purpose (avgGrade,
worstGrade, bestGrade)
Arithmetic Operators
C++ Operation
Arithmetic
Operator
Algebraic
Expression
C++ Expression
Addition
+
a+b
a+b
Subtraction
-
a-b
a-b
Multiplication
*
ab or a x b
a*b
Division
/
a/b or a or a
Modulus
%
a mod b
b a/b
a%b
Rules of precedence for
Arithmetic Operations
Operator
Operation
Order of evaluation
()
Parentheses
Evaluated first. If the
parentheses are nested the
innermost pair is evaluated
first. If there are several pairs
on the same level they are
evaluated left to right.
*/%
Division, mulitiplication,
modulus
Evaluated second. If there
are several they are
evaluated left to right.
+-
Addtition, subtraction
Evaluated last. If there are
several, they are evaluated
left to right
Exercise 1
int num1, num2, num3;
num1 = 3;
num2 = num1;
num1 = num2 + 1;
num3 = num2 * num1 - 6;
What are the values for num1, num2 and num3 after this program executes?
Exercise 2
int a, x, b, c, y;
a = 2;
x = 5;
b = 3;
c = 7;
y = a * x * x + b * x + c;
What is y after this program executes?
Relational Operators
Standard algebraic
expression
C++ operator
Example of C++
expression
Meaning
>
>
a>b
a is greater than b
<
<
a<b
a is less than b
>=
a >=b
a is greater than or
equal to b
<=
a <= b
a is less than or equal
to b
==
a == b
a is equal to b
!=
a != b
a is not equal to b
=
Logical Operators
Operator
Name
Example
True if
!
Not
!x
x is false
&&
And
x && y
x and y are both
true
||
Or
x || y
x or y is true
Precedence chart
Operator
Type
()
!
*/%
parentheses
not
multiplication, division,
modulus
addition, subtraction
relationals
equality
and
or
+< > <= >=
== !=
&&
||
Data Type
• Determines how much memory is needed to
store this information
• Determines what sort of information the
data type represents
• Determines which operations are legal on
this information
Determining Size and Range
// Determining the size and range of the the signed long integer type.
#include <iostream>
using namespace std;
int main()
{
cout << "signed long type is " << sizeof (signed long) << "bytes\n";
cout << "largest value of signed long type is " << LONG_MAX << endl;
cout << "smallest value of signed long type is " << LONG_MIN << endl;
return 0;
}
Floating Point Data Types
Type
Typical range
Typical
precision
float
10 e -38 to
10 e 38
10 e -308 to
10 e 308
10 e -4932 to
10 e 4932
6 digits
double
long double
15 digits
19 digits
Things to remember about
floating point data types
• Mantissa is limited in size so value may not
be exact
• Arithmetic is faster using integer types than
floating point types
• Don’t use equality operators
• Modulus operator not valid for floating
point
• Don’t use floating point #’s for loop control
Mixing float and integer types
#include <iostream>
using namespace std;
int main()
{
float average;
int numberOfGrades = 2, totalOfGrades = 9;
average = totalOfGrades / numberOfGrades;
cout << "The average is " << average << endl;
return 0;
}
Data Types Summary
Given the following declarations, determine the value of the variable on the left-handside of each assignment statement.
int int1, int2, int3;
float f1=1.0, f2=2.5, f3=5.0;
double d1, d2;
char ch1, ch2;
int1 = 5; d2 = 5.0;
ch1 = '5';
int3 = f2;
int2 = int1 / int3;
d1 = f3 - -f1 * 6 / int3 + 8.0 * (int1 - d2);
ch2 = ch1 - 2;
int3 = 'a' - 'A';
ch1 = 'W' + int3;