Skip to content

Commit fe9db51

Browse files
committed
Added code file for the first homework
1 parent b8e68d8 commit fe9db51

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

HW1/AoA_hw1.pdf

167 KB
Binary file not shown.

HW1/main.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
#include <vector>
4+
#include <chrono>
5+
#include <random>
6+
7+
using namespace std;
8+
9+
void find_duplications(unsigned int *arr, unsigned int arr_length, vector<unsigned int> &dup_vec) {
10+
for (unsigned int i = 0; i < arr_length; i++) {
11+
for (unsigned int j = i + 1; j < arr_length; j++) {
12+
if (arr[i] == arr[j]) {
13+
dup_vec.push_back(i);
14+
dup_vec.push_back(j);
15+
}
16+
}
17+
}
18+
}
19+
20+
int main(int argc, char *argv[]) {
21+
// (a)
22+
unsigned int arr_length; // User will provide the array length and maximum number we can generate
23+
arr_length = stoul(argv[1]);
24+
unsigned int *random_arr = new unsigned int[arr_length];
25+
26+
// (b)
27+
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
28+
default_random_engine generator(seed);
29+
uniform_int_distribution<int> distribution(1, arr_length);
30+
31+
for (unsigned int i = 0; i < arr_length; i++) {
32+
random_arr[i] = distribution(generator); // Element i is between 1 and arr_length(included)
33+
}
34+
35+
// (c)
36+
37+
cout << "ARRAY:";
38+
for (unsigned int i = 0; i < arr_length; i++) {
39+
if (i % 10 == 0) cout << "\n"; // 10 elements per line
40+
cout << random_arr[i] << "\t";
41+
}
42+
cout << endl;
43+
44+
// (d)
45+
cout << "\nEXPECTED NUMBER OF DUPLICATIONS = " << (arr_length - 1) / 2.0 << endl;
46+
47+
// (e)
48+
vector<unsigned int> dup_vec;
49+
find_duplications(random_arr, arr_length, dup_vec);
50+
51+
int c = 0;
52+
for (auto i = dup_vec.begin(); i != dup_vec.end(); advance(i, 2)) {
53+
if (c % 10 == 0) cout << "\n"; // 10 elements per line
54+
cout << "(" << *i + 1 << ", " << *(i + 1) + 1 << ") ";
55+
c++;
56+
}
57+
58+
// (f)
59+
cout << endl << "\nENCOUNTERED NUMBER OF DUPLICATIONS = " << dup_vec.size() / 2 << endl;
60+
61+
delete[] random_arr;
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)