Skip to content

Commit 2c19d0f

Browse files
committed
first commit
1 parent 34b7700 commit 2c19d0f

File tree

18 files changed

+274
-0
lines changed

18 files changed

+274
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# algs4cplusplus
22
Algorithms, 4th edition textbook code (using c++)
3+

ch1/1_BinarySearch/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(1_BinarySearch)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(1_BinarySearch ${SOURCE_FILES})

ch1/1_BinarySearch/main.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <fstream>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
/**
9+
* Returns the index of the specified key in the specified array.
10+
*
11+
* @param a the array of integers, must be sorted in ascending order
12+
* @param key the search key
13+
* @return index of key in array {@code a} if present; {@code -1} otherwise
14+
*/
15+
int indexOf(vector<int> &a, int key) {
16+
int lo = 0, hi = a.size() - 1;
17+
while (lo <= hi) {
18+
// Key is in a[lo..hi] or not present.
19+
int mid = lo + (hi - lo) / 2;
20+
if (key < a[mid]) hi = mid - 1;
21+
else if (key > a[mid]) lo = mid + 1;
22+
else return mid;
23+
}
24+
return -1;
25+
}
26+
27+
int main() {
28+
// TODO: change to relative path
29+
ifstream input("/home/ace/AceDev/C++/algorithm/ch1/1_BinarySearch/tinyW.txt");
30+
vector<int> whitelist;
31+
int t;
32+
while (input >> t) {
33+
whitelist.push_back(t);
34+
}
35+
sort(whitelist.begin(), whitelist.end());
36+
ifstream check("/home/ace/AceDev/C++/algorithm/ch1/1_BinarySearch/tinyT.txt");
37+
while (check >> t) {
38+
int idx = indexOf(whitelist, t);
39+
if (idx == -1)
40+
cout << t << endl;
41+
}
42+
}

ch1/1_BinarySearch/tinyT.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
23
2+
50
3+
10
4+
99
5+
18
6+
23
7+
98
8+
84
9+
11
10+
10
11+
48
12+
77
13+
13
14+
54
15+
98
16+
77
17+
77
18+
68

ch1/1_BinarySearch/tinyW.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
84
2+
48
3+
68
4+
10
5+
18
6+
98
7+
12
8+
23
9+
54
10+
57
11+
33
12+
16
13+
77
14+
11
15+
29

ch1/2_RandomSeq/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(2_RandomSeq)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(2_RandomSeq ${SOURCE_FILES})

ch1/2_RandomSeq/main.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <iomanip>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
const int seed = 100;
9+
10+
/**
11+
* Reads in two command-line arguments lo and hi and prints n uniformly
12+
* random real numbers in [lo, hi) to standard output.
13+
*
14+
* @param args the command-line arguments
15+
*/
16+
int main() {
17+
int n = 10;
18+
double lo = 0.0, hi = 1.0;
19+
// uniform[0.0, 1.0)
20+
default_random_engine e(seed);
21+
uniform_real_distribution<double> dis(0.0, 1.0);
22+
cout << fixed;
23+
for (int i = 0; i < n; ++i)
24+
cout << setprecision(4) << dis(e) << " ";
25+
cout << endl;
26+
// uniform[lo, high)
27+
lo = 2.5, hi = 3.0;
28+
uniform_real_distribution<double> dis2(lo, hi);
29+
for (int i = 0; i < n; ++i)
30+
cout << setprecision(4) << dis2(e) << " ";
31+
cout << endl;
32+
}

ch1/3_Average/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(3_Average)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(3_Average ${SOURCE_FILES})

ch1/3_Average/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <iomanip>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
const int seed = 100;
9+
10+
/**
11+
* Reads in a sequence of real numbers from standard input and prints
12+
* out their average to standard output.
13+
*
14+
* @param args the command-line arguments
15+
*/
16+
int main() {
17+
int count = 0;
18+
double sum = 0.0;
19+
vector<double> arr{10.0, 5.0, 6.0, 3.0, 7.0, 32.0};
20+
for (auto a: arr) {
21+
sum += a;
22+
count++;
23+
}
24+
double avg = sum / count;
25+
cout << "Average is " << avg << endl;
26+
}

ch1/4_Cat/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
project(4_Cat)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(4_Cat ${SOURCE_FILES})

0 commit comments

Comments
 (0)