├── .gitignore ├── CMakeLists.txt ├── README.md ├── algorithms └── min-heap.cpp ├── convert.c ├── convert.cpp ├── dijkstra.cpp ├── main.cpp └── print_array.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | ### C++ template 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | 22 | # Compiled Static libraries 23 | *.lai 24 | *.la 25 | *.a 26 | *.lib 27 | 28 | # Executables 29 | *.exe 30 | *.out 31 | *.app 32 | 33 | # Created by .ignore support plugin (hsz.mobi) 34 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | project(cpp_experiments) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | 6 | set(SOURCE_FILES main.cpp) 7 | add_executable(cpp_experiments ${SOURCE_FILES}) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ Experiments and Sandbox Code 2 | -------------------------------------------------------------------------------- /algorithms/min-heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void minHeapify(int *a, int i, int n) { 6 | int j, temp; 7 | temp = a[i]; 8 | j = 2 * i; 9 | while (j <= n) { 10 | if (j < n && a[j + 1] < a[j]) 11 | j = j + 1; 12 | if (temp < a[j]) 13 | break; 14 | else if (temp >= a[j]) { 15 | a[j / 2] = a[j]; 16 | j = 2 * j; 17 | } 18 | } 19 | a[j / 2] = temp; 20 | return; 21 | } 22 | 23 | void buildMinHeap(int *a, int n) { 24 | int i; 25 | for (i = n / 2; i >= 1; i--) { 26 | minHeapify(a, i, n); 27 | } 28 | } 29 | 30 | int main() { 31 | int n, i, x; 32 | cout << "enter no of elements of array\n"; 33 | cin >> n; 34 | int a[20]; 35 | for (i = 1; i <= n; i++) { 36 | cout << "enter element" << (i) << endl; 37 | cin >> a[i]; 38 | } 39 | buildMinHeap(a, n); 40 | cout << "Min Heap\n"; 41 | for (i = 1; i <= n; i++) { 42 | cout << a[i] << endl; 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /convert.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Convert this program to C++ 3 | * change to C++ io 4 | * change to one line comments 5 | * change defines of constants to const 6 | * change array to vector<> 7 | * inline any short function 8 | */ 9 | 10 | #include 11 | 12 | #define N 40 13 | 14 | void sum(int *p, int n, int d[]) { 15 | int i; 16 | *p = 0; 17 | 18 | for (i = 0; i < n; ++i) 19 | *p = *p + d[i]; 20 | 21 | } 22 | 23 | int main() { 24 | 25 | int i; 26 | 27 | int accum = 0; 28 | 29 | int data[N]; 30 | 31 | for (i = 0; i < N; ++i) 32 | 33 | data[i] = i; 34 | 35 | sum(&accum, N, data); 36 | 37 | printf("sum is %d\n", accum); 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /convert.cpp: -------------------------------------------------------------------------------- 1 | // Sums the indices of an vector of length N 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const int N = 40; 9 | 10 | // stores result in p: the sum of the values of vector d of length n 11 | inline void sum(int& p, int n, vector d) { p = 0; for (int i = 0; i < n; i++) { p += d.at(i); } } 12 | 13 | int main() 14 | { 15 | int accum = 0; 16 | vector data(N); 17 | 18 | for (int i = 0; i < N; i++) { 19 | data.at(i) = i; 20 | } 21 | 22 | sum(accum, N, data); 23 | 24 | cout << "sum is " << accum << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int NODES = 50; 7 | inline double prob() { return rand() / double(RAND_MAX); } 8 | 9 | class Node { 10 | public: 11 | double weight; 12 | vector neighbors; 13 | }; 14 | 15 | class Graph 16 | { 17 | public: 18 | Graph(); 19 | void generateGraph(double density, double minDist, double maxDist); 20 | 21 | private: 22 | int nodeCount; 23 | vector nodes; 24 | }; 25 | 26 | Graph::Graph() { 27 | this->nodeCount = NODES; 28 | } 29 | 30 | void Graph::generateGraph(double density, double minDist, double maxDist) 31 | { 32 | for (int i = 0; i < this->nodeCount; i++) { 33 | // this->nodes.push_back(i) 34 | 35 | for (int j = 0; j < this->nodeCount; j++){ 36 | if (i != j) { 37 | if (prob() < density) { 38 | Node* newNode = new Node(); 39 | newNode->weight = prob() * (maxDist - minDist); 40 | // this->nodes[i].neighbors.push_back(newNode); 41 | // cout << this->nodes[i].weight << endl; 42 | } 43 | } 44 | } 45 | } 46 | } 47 | 48 | /** 49 | * Runs a number of trials in a Monte Carlo simulation 50 | * to determine the average shortest path in random undirected graphs 51 | */ 52 | int main(){ 53 | 54 | cout << "Running trials..." << endl; 55 | 56 | int numTrials = 10; 57 | double sum = 0.0; 58 | double minDistance = 1.0; 59 | double maxDistance = 10.0; 60 | 61 | for (int i = 0; i < numTrials; i++) { 62 | 63 | // graph with 20% density 64 | Graph* graph1 = new Graph(); 65 | graph1->generateGraph(0.2, minDistance, maxDistance); 66 | 67 | // graph with 40% density 68 | Graph* graph2 = new Graph(); 69 | graph2->generateGraph(0.4, minDistance, maxDistance); 70 | } 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | cout << "Hello, World!" << endl; 7 | return 0; 8 | } -------------------------------------------------------------------------------- /print_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void display(const T arr[], int size) 7 | { 8 | int i; 9 | 10 | for (i = 0; i < size; i++) { 11 | cout << arr[i] << "\t"; 12 | } 13 | 14 | cout << "\n"; 15 | } 16 | 17 | int main() 18 | { 19 | int intArray[] = {1, 3, 6, 12}; 20 | double dblArray[] = {1.1, 2.4, 6.24, 0.4}; 21 | 22 | cout << "Array elements: " << endl; 23 | 24 | display(intArray, 4); 25 | display(dblArray, 4); 26 | 27 | return 0; 28 | } --------------------------------------------------------------------------------