Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
faheel committed Apr 7, 2018
1 parent 987277b commit eb38165
Show file tree
Hide file tree
Showing 7 changed files with 444 additions and 0 deletions.
66 changes: 66 additions & 0 deletions DataStructures/kruskal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <algorithm>
#include <iostream>
#include <vector>

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 <Node> nodes;
vector <Edge> edges;
vector <Edge> 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;
}
112 changes: 112 additions & 0 deletions DataStructures/prim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <iostream>
#include <vector>

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 <char> nodes;
vector <Edge> edges;
vector <Edge> 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;
}
56 changes: 56 additions & 0 deletions OOP/BasicCalc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
A basic calculator that can add, subtract, multiply, divide and modulus two numbers -
implemented using an inline fuction
*/

#include <iostream>

using namespace std;

template <typename T>
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 : <operand> <operator> <operand>\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;
}
39 changes: 39 additions & 0 deletions OOP/CountConstructorsAndDestructors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>

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;
}
47 changes: 47 additions & 0 deletions OOP/ReverseNumber.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>

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;
}
44 changes: 44 additions & 0 deletions OOP/VowelsInCharArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Find the number of vowels in given char array using pointer arithmetic
*/

#include <iostream>
#include <cstring>

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;
}
Loading

0 comments on commit eb38165

Please sign in to comment.