From eb38165f32c25b26668145a33d6f9b2e8f729fa4 Mon Sep 17 00:00:00 2001 From: Faheel Ahmad Date: Tue, 7 Feb 2017 15:50:10 +0530 Subject: [PATCH] Add files --- DataStructures/kruskal.cpp | 66 ++++++++++++++ DataStructures/prim.cpp | 112 ++++++++++++++++++++++++ OOP/BasicCalc.cpp | 56 ++++++++++++ OOP/CountConstructorsAndDestructors.cpp | 39 +++++++++ OOP/ReverseNumber.cpp | 47 ++++++++++ OOP/VowelsInCharArray.cpp | 44 ++++++++++ OS/banker.cpp | 80 +++++++++++++++++ 7 files changed, 444 insertions(+) create mode 100644 DataStructures/kruskal.cpp create mode 100644 DataStructures/prim.cpp create mode 100644 OOP/BasicCalc.cpp create mode 100644 OOP/CountConstructorsAndDestructors.cpp create mode 100644 OOP/ReverseNumber.cpp create mode 100644 OOP/VowelsInCharArray.cpp create mode 100644 OS/banker.cpp diff --git a/DataStructures/kruskal.cpp b/DataStructures/kruskal.cpp new file mode 100644 index 0000000..d6b3b3d --- /dev/null +++ b/DataStructures/kruskal.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +using namespace std; + +const int MaxNodes = 10; + +struct Node +{ + char parent; + int rank; +}; + +struct Edge +{ + char node1, node2; + int weight; +}; + +bool byWeight(Edge e1, Edge e2) +{ + return e1.weight <= e2.weight; +} + +int main() +{ + Node node; + Edge edge; + + vector nodes; + vector edges; + vector MST; + + int numNodes, numEdges, i; + + cout << "Enter the number of nodes (max " << MaxNodes << ") : "; + cin >> numNodes; + + for (i = 0; i < numNodes; i++) + { + node.parent = char('a' + i); + node.rank = 0; + + nodes.push_back(node); + } + + cout << "Enter the number of edges : "; + cin >> numEdges; + + cout << "Enter the edges (node1, node2, weight) :\n"; + for (i = 0; i < numEdges; i++) + { + cin >> edge.node1; + cin >> edge.node2; + cin >> edge.weight; + + edges.push_back(edge); + } + + sort(edges.begin(), edges.end(), byWeight); + + // TODO: complete it! + + return 0; +} diff --git a/DataStructures/prim.cpp b/DataStructures/prim.cpp new file mode 100644 index 0000000..cee2689 --- /dev/null +++ b/DataStructures/prim.cpp @@ -0,0 +1,112 @@ +#include +#include + +using namespace std; + +const int MaxNodes = 10; + +struct Edge +{ + char source, dest; + int weight; +}; + +void printAdjMatrix(int adjMatrix[][MaxNodes], int numNodes) +{ + int i, j; + + cout << "Adjacency matrix:\n "; + for (i = 0; i < numNodes; i++) + cout << char('a' + i) << " "; + + for (i = 0; i < numNodes; i++) + { + cout << "\n" << char('a' + i) << " "; + for (j = 0; j < numNodes; j++) + cout << adjMatrix[i][j] << " "; + } + cout << "\n"; +} + +int main() +{ + Edge edge; + + vector nodes; + vector edges; + vector MST; + + int adjMatrix[MaxNodes][MaxNodes]; + int numNodes, numEdges, i, j, source, dest, weight; + + cout << "Enter the number of nodes (max " << MaxNodes << ") : "; + cin >> numNodes; + + for (i = 0; i < numNodes; i++) + nodes.push_back(char('a' + i)); + + cout << "Enter the number of edges : "; + cin >> numEdges; + + cout << "Enter the edges (source, dest, weight) :\n"; + for (i = 0; i < numEdges; i++) + { + cin >> edge.source; + cin >> edge.dest; + cin >> edge.weight; + + edges.push_back(edge); + } + + for (i = 0; i < numNodes; i++) + for (j = 0; j < numNodes; j++) + adjMatrix[i][j] = 0; + + for (i = 0; i < numEdges; i++) + { + source = edges[i].source - 'a'; + dest = edges[i].dest - 'a'; + weight = edges[i].weight; + adjMatrix[source][dest] = adjMatrix[dest][source] = weight; + } + + printAdjMatrix(adjMatrix, numNodes); + + int cost, minWeight, minIndex; + + cost = minWeight = 0; + for (i = 0; i < numNodes-1; i++) + { + minWeight = 1E9; + minIndex = -1; + for (j = 0; j < numNodes; j++) + if (adjMatrix[i][j] != 0) + { + if (adjMatrix[i][j] < minWeight) + { + minWeight = adjMatrix[i][j]; + minIndex = j; + } + } + if (minIndex != -1) + { + adjMatrix[minIndex][i] = 0; + edge.source = char('a' + i); + edge.dest = char('a' + minIndex); + edge.weight = minWeight; + + MST.push_back(edge); + cost += minWeight; + } + } + + cout << "\nMinimum spanning tree:\n"; + for (i = 0; i < MST.size(); i++) + { + cout << MST[i].source << " - " << MST[i].weight << " - " << MST[i].dest << "\n"; + } + + cout << "\nCost of MST : " << cost << "\n"; + + return 0; +} \ No newline at end of file diff --git a/OOP/BasicCalc.cpp b/OOP/BasicCalc.cpp new file mode 100644 index 0000000..6cc5579 --- /dev/null +++ b/OOP/BasicCalc.cpp @@ -0,0 +1,56 @@ +/* + A basic calculator that can add, subtract, multiply, divide and modulus two numbers - + implemented using an inline fuction +*/ + +#include + +using namespace std; + +template +inline T calculate(T a, char op, T b) { + T result; + + switch (op) { + case '+': + result = a + b; + break; + case '-': + result = a - b; + break; + case '*': + result = a * b; + break; + case '/': + result = a / b; + break; + case '%': + result = a % b; + break; + default: + cout << "Invalid operator!\n"; + } + + return result; +} + +int main() { + int a, b, result; + char op; + + cout << "Basic calculator\n"; + cout << "----------------\n"; + cout << "Available operations : [+, -, *, /, %]\n"; + cout << "Operation format : \n\n"; + cout << "Press Ctrl+Z to exit.\n\n"; + + do { + cout << ">>> "; + cin >> a >> op >> b; + + result = calculate(a, op, b); + cout << " = " << result << "\n"; + } while (true); + + return 0; +} \ No newline at end of file diff --git a/OOP/CountConstructorsAndDestructors.cpp b/OOP/CountConstructorsAndDestructors.cpp new file mode 100644 index 0000000..09bea3b --- /dev/null +++ b/OOP/CountConstructorsAndDestructors.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +class Some { + string name; + static int object_count; +public: + void show_obj_count() { + cout << "Objects alive : " << object_count << "\n"; + } + + Some(string objName) { + name = objName; + cout << "Object with name \'" << name << "\' created.\n"; + object_count++; + show_obj_count(); + } + + ~Some() { + cout << "Object with name \'" << name << "\' destructed.\n"; + object_count--; + show_obj_count(); + } +}; + +int Some::object_count = 0; + +int main() { + Some obj1("First"); + + { + Some obj1("First"), obj2("Second"); + } + + Some obj3("Third"); + + return 0; +} \ No newline at end of file diff --git a/OOP/ReverseNumber.cpp b/OOP/ReverseNumber.cpp new file mode 100644 index 0000000..7ec71c8 --- /dev/null +++ b/OOP/ReverseNumber.cpp @@ -0,0 +1,47 @@ +/* + Reverse the given number using a function with a return type, + and also using a function without a return type +*/ + +#include + +using namespace std; + +void reverse_number_no_return(int &num) { + int reverse = 0; + + while (num) { + reverse *= 10; + reverse += num % 10; + num /= 10; + } + + num = reverse; +} + +int reverse_number_return(int num) { + int reverse = 0; + + while (num) { + reverse *= 10; + reverse += num % 10; + num /= 10; + } + + return reverse; +} + +int main() { + int num; + cout << "Enter a number : "; + cin >> num; + + cout << "Using a function with a return type:\n"; + cout << "The reverse of the number is : " << reverse_number_return(num) << "\n"; + + cout << "Using a function without a return type:\n"; + reverse_number_no_return(num); + cout << "The reverse of the number is : " << num << "\n"; + + return 0; +} \ No newline at end of file diff --git a/OOP/VowelsInCharArray.cpp b/OOP/VowelsInCharArray.cpp new file mode 100644 index 0000000..227977c --- /dev/null +++ b/OOP/VowelsInCharArray.cpp @@ -0,0 +1,44 @@ +/* + Find the number of vowels in given char array using pointer arithmetic +*/ + +#include +#include + +using namespace std; + +int count_vowels(char *str) { + unsigned int length = strlen(str); + int num_vowels = 0; + + for (size_t i = 0; i < length; i++) { + switch (*(str + i)) { + case 'a': + case 'A': + case 'e': + case 'E': + case 'i': + case 'I': + case 'o': + case 'O': + case 'u': + case 'U': + num_vowels++; + } + } + + return num_vowels; +} + +const unsigned int MAX_LENGTH = 128; + +int main() { + char str[MAX_LENGTH]; + cout << "Enter a string : "; + cin.getline(str, MAX_LENGTH); + + int num_vowels = count_vowels(str); + cout << "\nNumber of vowels in the string : " << num_vowels << "\n"; + + return 0; +} \ No newline at end of file diff --git a/OS/banker.cpp b/OS/banker.cpp new file mode 100644 index 0000000..ebba1df --- /dev/null +++ b/OS/banker.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +const int Max_Res = 5; +const int Max_Proc = 10; + +void print_need(int need[Max_Proc][Max_Res], int num_proc, int num_res) +{ + cout << " "; + for (j = 0; j < num_res; j++) + cout << " " << char('A' + j) << " "; + for (i = 0; i < num_proc; i++) + { + cout << "\nP" << i+1 << " "; + for (j = 0; j < num_res; j++) + cout << " " << need[i][j] << " "; + } + cout << "\n"; +} + +void safety_test() +{ + +} + +int main() +{ + int total[Max_Res], + avail[Max_Res], + max_req[Max_Proc][Max_Res], + alloc[Max_Proc][Max_Res], + need[Max_Proc][Max_Res], + num_res, + num_proc, + i, j; + + cout << "Enter the number of resources (max " << Max_Res << ") : "; + cin >> num_res; + + cout << "Enter total number of each resource:\n"; + for (i = 0; i < num_res; i++) + { + cout << char('A' + i) << " : "; + cin >> total[i]; + } + + cout << "Enter the number of processes (max " << Max_Proc << ") : "; + cin >> num_proc; + + cout << "Enter max request and allocated resources for each process:"; + for (i = 0; i < num_proc; i++) + { + cout << "\nP" << i+1 << ":\n"; + + cout << "Max request for resources:\n"; + for (j = 0; j < num_res; j++) + { + cout << char('A' + i) << " : "; + cin >> max_req[i][j]; + } + + cout << "Allocated resources:\n"; + for (j = 0; j < num_res; j++) + { + cout << char('A' + i) << " : "; + cin >> alloc[i][j]; + + avail[j] = total[j] - alloc[i][j]; + need[i][j] = max_req[i][j] - alloc[i][j]; + } + } + + cout << "Need matrix:\n"; + printNeed(need, num_proc, num_res); + + + + return 0; +} \ No newline at end of file