Skip to content

Commit 73efa8c

Browse files
committed
Added references examples
1 parent f9fbe1e commit 73efa8c

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

absoluteDifference.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main() {
5+
//declare the two variables
6+
int n, m;
7+
//read them in from standard input
8+
cin >> n >> m;
9+
//calculate the difference
10+
int diff = n - m;
11+
12+
if (diff < 0) {
13+
diff = diff*(-1);
14+
}
15+
16+
cout << diff << endl;
17+
18+
return 0;
19+
}

references/1.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int main() {
6+
int x = 3;
7+
cout << "x is: " << x << "; &x is: " << &x << endl;
8+
int &xRef = x;
9+
cout << "xRef is: " << xRef << endl;
10+
int *hell = &xRef;
11+
cout << "hell is: " << hell << "; *hell is: " << *hell << "; &hell is: " << &hell << endl;
12+
xRef = 10;
13+
cout << "x is: " << x << "; xRef is: " << xRef << endl;
14+
cout << "&xRef is: " << &xRef << endl;
15+
16+
const int &unnamedRef2 = 5;
17+
cout << "unnamedRef2 is: " << unnamedRef2 << endl;
18+
19+
int *intP;
20+
cout << "intP is: " << intP << "; *intP is: " << *intP << endl;
21+
22+
int *&ptrRef = intP;
23+
cout << "intP is: " << intP << "; *intP is: " << *intP << "; &intP is: " << &intP << "; ptrRef is: " << ptrRef << "; *ptrRef is: " << *ptrRef << "; &ptrRef is: " << &ptrRef << endl;
24+
25+
ptrRef = new int;
26+
cout << "intP is: " << intP << "; *intP is: " << *intP << "; &intP is: " << &intP << "; ptrRef is: " << ptrRef << "; *ptrRef is: " << *ptrRef << "; &ptrRef is: " << &ptrRef << endl;
27+
28+
*ptrRef = 5;
29+
cout << "intP is: " << intP << "; *intP is: " << *intP << "; &intP is: " << &intP << "; ptrRef is: " << ptrRef << "; *ptrRef is: " << *ptrRef << "; &ptrRef is: " << &ptrRef << endl;
30+
}

references/separateOddsAndEvens.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
void separateOddsAndEvens(const vector<int>& arr, vector<int>& odds, vector<int>& evens);
7+
void printVector(const vector<int>& vec);
8+
9+
int main() {
10+
vector<int> arrUnsplit = {1,2,3,4,5,6,7,8,9,10};
11+
vector<int> odds, evens;
12+
cout << "main array is: " << endl;
13+
printVector(arrUnsplit);
14+
separateOddsAndEvens(arrUnsplit, odds, evens);
15+
cout << "odds is: " << endl;
16+
printVector(odds);
17+
cout << "evens is: " << endl;
18+
printVector(evens);
19+
20+
return 0;
21+
}
22+
23+
void separateOddsAndEvens(const vector<int>& arr, vector<int>& odds, vector<int>& evens) {
24+
int numodds = 0, numevens = 0;
25+
for(auto& i : arr) {
26+
if(i % 2 == 1) {
27+
numodds++;
28+
} else {
29+
numevens++;
30+
}
31+
}
32+
33+
odds.reserve(numodds);
34+
evens.reserve(numevens);
35+
for(auto& i : arr) {
36+
if(i % 2 == 1) {
37+
odds.push_back(i);
38+
} else {
39+
evens.push_back(i);
40+
}
41+
}
42+
}
43+
44+
void printVector(const vector<int>& vec) {
45+
cout << "[";
46+
for(auto& i : vec) {
47+
cout << i << ", ";
48+
}
49+
cout << "]" << endl;
50+
}

references/swap.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
void swap(int first, int second);
5+
void swapRef(int &firstRef, int &secondRef);
6+
7+
int main() {
8+
int first = 5, second = 15;
9+
swap(first, second);
10+
cout << "first is now: " << first << "; and second is now: " << second << endl;
11+
swapRef(first, second);
12+
cout << "first is now: " << first << "; and second is now: " << second << endl;
13+
14+
return 0;
15+
}
16+
17+
void swap(int first, int second) {
18+
int temp = first;
19+
first = second;
20+
second = temp;
21+
cout << "in 'swap' function, first is: " << first << "; and second is: " << second << endl;
22+
}
23+
24+
void swapRef(int &firstRef, int &secondRef) {
25+
int temp = firstRef;
26+
firstRef = secondRef;
27+
secondRef = temp;
28+
cout << "in 'swapRef' function, first is: " << firstRef << "; and second is: " << secondRef << endl;
29+
}

0 commit comments

Comments
 (0)