Professional Documents
Culture Documents
ECCS Lecture Notes
ECCS Lecture Notes
Lecture 2
2.1 Pseudocode and Programming Languages:
-C++ and code describes the steps in computer language in order for the computer to
compute commands.
-Ex: cuot<<y;
-Ex: 2+3
-Ex:
Int x = 3; x = 3
Int y = x; y = 3 33
x = 3;
cout << x;
cout << y;
Return 0;
1
fi
Wednesday, September 9, 2020
-Like a grammar error where the computer doesn’t understand what is being said
-A bug is when the computer is told what to do but does the wrong thing
Lecture 3
3.1 Types
3.2 Operators
- Integer division:
- 7%3 = 1
- Nothing to note
2
Wednesday, September 9, 2020
-Implicit Conversions
-Explicit Conversion
-static_cast<int>(x);
-Rounding Functions:
-Ex: d=3.7
- round (d) = 4
- oor (d) = 3
- Ceil (d) = 4
Lecture 4
4.1 Functions
3
fl
fl
Wednesday, September 9, 2020
Parts of a function:
_____________ nameOfFunction(_____________________){
return _____________;
(output value)
double square(double d) {
return d*d;
int main() {
4
Wednesday, September 9, 2020
double z = 3.0; Declares a new variable z = 3.0
s = square (z); function call (square), z = 3.0, the value of z become d, runs
return 0;
Input Rules:
Output Rules:
Function calls:
Lecture 5
5.1 Scope
Example: Example:
int x = 3; y = x;
y = x; return 0;
return 0; }
} Doesn't Run
Runs
5
fi
Wednesday, September 9, 2020
5.2 Pass by Value
Example:
Void addOne(int x) {
x = x + 1
cout << x;
int main () {
int x = 3;
addOne(x);
cout << x;
Example:
int x;
cin >> x;
6
Wednesday, September 9, 2020
5.4 Cin Fail State, getline(), other inputs
input eecs
input x; <---- This is the fail state, because the letters aren't integers, so all other cins
do nothing
cin >>x;
string s;
cin >> s;
use getlin()
Example:
getline(cin, s);
Lecture 6
6.1 RME Comments
-Example:
e ects: Returns average of count and the elements that end up in sum
7
ff
fi
ff
fi
fi
ff
Wednesday, September 9, 2020
double average(double sum, double count) {
return sum/count;
-example
if (windChill < 0) {
} else {
-compare numbers
-compare numbers
8
Wednesday, September 9, 2020
6.5 Completments
Lecture 7
7.1 While Loops
Example:
int i = 0 variables
i = i + 1 console
} hihihibye
-count control loop: executes a certain number of times and made up of three parts
example:
i = i + 1
9
Wednesday, September 9, 2020
7.2 Event Controlled Loops (Cin Loops)
-Event loop also known as cin loops use cin in a while loop
- Evaluates to true when something successfully reads into x and false when it fails to
read into x
Example:
int x; 0123D
return 0;
Example;
int main() {
int x;
return 0;
10
Wednesday, September 9, 2020
Example:
int main() {
int x;
return 0;
i = i + 1 i = i - 1
i +=1 i -=1
i++ i--
++i --i
-Some others
Lecture 8
8.2 For Loops:
11
Wednesday, September 9, 2020
Ex:
int i = 0;
cout << i;
Ex:
cout << "i = " << i << " nished" << endl;
12
fi
Wednesday, September 9, 2020
- Scope with loops:
Ex: Ex:
int x = 7; -
int y; - -
if (x < 10) { - -
int z = x - 10; - - -
x += 2; - - -
} - - -
y = x + z; - -
Lecture 9
9.1 Expressions with Strings
-string comparison:
13
Wednesday, September 9, 2020
9.2 String Indexes
Ex:
string s = "Hello";
index of string s:
h e l l o
0 1 2 3 4
-index of s = 4
Ex:
string.s = "hello";
- .at()
Ex:
string.s = "hello";
Ex:
string s = 'sup';
s.at(1) = 'c';
Ex:
string s = 'hello';
14
Wednesday, September 9, 2020
Lecture 10
10.1 Pass by Value and Pass by Reference
Ex:
void f(int x) {
x = 3;
int main() {
int x = 2;
f(x);
-pass by reference
- uses an &
Ex:
x = 3;
int main () {
int x = 2;
f(x);
15
Wednesday, September 9, 2020
Ex:
x = x * 2;
int main () {
int a = 3;
multiply (a);
return 0;
Ex: ERROR
x = x + 1;
int main() {
addOne(3.1);
double a = 3.1;
addOne(a + 1);
int y = 3;
addOne(y);
return 0;
16
Wednesday, September 9, 2020
Ex: FIXED
x = x + 1;
int main() {
double d = 3.1;
addOne(d);
double a = 3.1;
double d2 = a + 1;
addOne (d2);
int y = 3;
addOne(y);
return 0;
Lecture 11
11.1 Source Code (.cpp) and header (.h) les
- With an array, you can store large amounts of variables instead of declaring all the
variables by assigning a number to the variable
- Example:
int main() {
int arr[3]; <---- type int, name arr, 3 spots (starting from 0).
17
fi
Wednesday, September 9, 2020
-Array sizes must be known at compile time
- number
-stored in constant
-Example:
good
-int arr[2];
int arr[SIZE];
not good
-int size;
int arr[size];
-Ex; int arr[4] = {-1}; puts 0's in the rest of the spots
-Ex: arr[4];
18
ff
Wednesday, September 9, 2020
-Things you cannot do with arrays
-Example:
int main() {
return 0;
-Example:
arr[0] = 4;
int main( ) {
f(arr, 3);
return 0;
19
fi
Wednesday, September 9, 2020
Lecture 12
12.1 Review Arrays
- index 0 1 2
- variable 4 5 6
for (int i = 0; i < arr.size( ); i++) { <---- cannot use .size with arrays
count ++;
return count;
-Common Errors
- the variables stored are score[0] score [1] score [2] up to score[5].
20
fi
Wednesday, September 9, 2020
- Arrays as function parameters
- 2D arrays
-arr[1]
- int arr[2][3];
-Initializing 2D arrays
- int arr[4][3] = {
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3}
};
- Example:
int arr[2][3];
- 2D arrays as Parameters\
-Example:
21
Wednesday, September 9, 2020
Lecture 13
13.1 Reading from Files: ifstreams
-Streams
- Types of streams
- #include <fstream>
-Istream syntax
#include <fstream>
int main( ) {
int x;
n >> x;
n.close( );
return0;
22
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Wednesday, September 9, 2020
- Four steps for reading from a le
- ofstream syntax
- Example:
#include <fstream>
int main ( ) {
fout.close( );
return 0;
-Types:
- .eof -- checks to see if there is nothing more in the le, reached the end
23
fi
fi
fi
fi
fi
fi
ff
fi
fi
fi
fi
fi
fi
fi
fi
Wednesday, September 9, 2020
- .clear( ) -- clears a fail state
-clearing states:
int x;
cin >> x;
if (cin.fail( )) {
cin.clear( );
string s;
cin >> s;
Lecture 14
14.1 De ning Classes
- Example:
class Student {
string name; <-- member variables, variables that are part of a class
-Example cont.
int main ( ) {
24
fi
fi
Wednesday, September 9, 2020
- Member functions
public:
};
cout << name; <== name comes from current instance of the class
.
(current means variable of class type)
int main ( ) {
Student s1;
s1.umid = 99999901;
14.3 Constructors
- Constructors
-Constructor syntax
- no return type
25
Wednesday, September 9, 2020
-same name as class
- Example:
class Student {
public:
private:
string name;
int umid;
};
name = name_in;
umid = umis_in;
- Non-default constuctor
-has parameters
- Example:
Student::Student(__________)
- Default contructor
- has no parameters
- Example:
Student::Student( ) {
name = "";
umid = 0;
26
Wednesday, September 9, 2020
-Using Constructors
int main ( ) {
Student s2; <== creates instance called s2 and calls default constructor
Lecture 15
15.1 Header Files
-Example:
student s1 = {"Jiaying"};
student s2;
s2 = s1;
- Example:
s_in.name = newName;
s_in.printName( );
int main ( ) {
Student s = {"Dean"};
s.printName( );
changeName(s, "niels");
- Header les
- .h les tell c++ what to expect, contain functions declarations and prototypes,
. RMEs, and class de nitions
27
fi
fi
fi
fi
fi
Wednesday, September 9, 2020
- Helps prevent errors by stopping bad input from getting into a class
- private will give class control over what goes into member variables
- member functions:
- private otherwise
- Example:
setter
umid = umid_in;
getter
- int getUmid( ) {
return umid;
28
Wednesday, September 9, 2020
15.4 Constructors
-Constructors
- no return type
- Example:
class Student {
public:
private:
string name;
int umid;
};
in the .cpp le
name = name_in;
umid = umid_in;
- Using constructors
- Example:
int main ( ) {
29
fi
Wednesday, September 9, 2020
}
Lecture 16
16.1 Overloaded Constructors
-Default: no parameters
- Overloaded Constructors
- Example:
classStudent {
public:
Student( );
private:
string name;
int umid;
};
int main ( ) {
- If you write any constructor, then it takes away this default constructor
30
ff
fi
Wednesday, September 9, 2020
16.2 Memebr variables of class type
- Example:
Class Student {
public:
string name;
int umid;
};
class DormRoom {
public:
Student resident;
string dorm;
int room;
};
int main( ) {
DormRoom dr;
dr.resident.name = "emily";
Lecture 17
17.1 Read and Write e Functions
31
Wednesday, September 9, 2020
32