Created
February 13, 2019 18:31
-
-
Save Hsankesara/195dfaf8fb7d2ef646b3c5977d915290 to your computer and use it in GitHub Desktop.
playing with cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
using namespace std; | |
double get_sum(int m, int n, double **array) | |
{ | |
double sum; | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < m; j++) | |
{ | |
sum += array[i][j]; | |
} | |
} | |
return sum; | |
} | |
int main() | |
{ | |
int n, m; | |
scanf("%d %d", &n, &m); | |
double **array; | |
array = new double *[n]; | |
for (int i = 0; i < n; i++) | |
{ | |
array[i] = new double[m]; | |
} | |
double temp; | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < m; j++) | |
{ | |
scanf("%lf", &temp); | |
array[i][j] = temp; | |
} | |
} | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < m; j++) | |
{ | |
printf("%lf ", array[i][j]); | |
} | |
printf("\n"); | |
} | |
printf("%lf\n", get_sum(m, n, array)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Node | |
{ | |
Node *lchild = NULL; | |
Node *rchild = NULL; | |
int value; | |
public: | |
Node(int value) | |
{ | |
this->value = value; | |
} | |
void add_child(Node *child, bool is_left) | |
{ | |
if (is_left) | |
this->lchild = child; | |
else | |
{ | |
this->rchild = child; | |
} | |
} | |
Node *get_child(bool is_left) | |
{ | |
if (is_left) | |
return lchild; | |
else | |
return rchild; | |
} | |
int get_value() | |
{ | |
return value; | |
} | |
void set_value(int new_value) | |
{ | |
value = new_value; | |
} | |
}; | |
class BinaryTree | |
{ | |
Node *root; | |
public: | |
BinaryTree(int value) | |
{ | |
root = new Node(value); | |
} | |
void add_child(int value) | |
{ | |
pair<Node *, bool> parent = get_parent_node(value); | |
bool is_left = parent.second; | |
Node *parent_node = parent.first; | |
Node *new_node = new Node(value); | |
parent_node->add_child(new_node, is_left); | |
} | |
Node *get_root() | |
{ | |
return root; | |
} | |
void print_inorder() | |
{ | |
print_inorder(root); | |
cout << endl; | |
} | |
bool search(int element) | |
{ | |
return search(root, element); | |
} | |
void delete_node(int element) | |
{ | |
delete_node(root, NULL, element, false); | |
} | |
private: | |
pair<Node *, bool> get_parent_node(int value) | |
{ | |
Node *current_pointer = root; | |
while (current_pointer != NULL) | |
{ | |
bool go_left; | |
if (current_pointer->get_value() < value) | |
{ | |
go_left = false; | |
} | |
else | |
{ | |
go_left = true; | |
} | |
if (current_pointer->get_child(go_left) == NULL) | |
{ | |
pair<Node *, bool> ret_value; | |
ret_value.first = current_pointer; | |
ret_value.second = go_left; | |
return ret_value; | |
} | |
else | |
{ | |
current_pointer = current_pointer->get_child(go_left); | |
} | |
} | |
} | |
void print_inorder(Node *node) | |
{ | |
if (node == NULL) | |
{ | |
return; | |
} | |
cout << node->get_value() << " "; | |
print_inorder(node->get_child(true)); | |
print_inorder(node->get_child(false)); | |
} | |
bool search(Node *node, int element) | |
{ | |
if (node == NULL) | |
return false; | |
if (node->get_value() == element) | |
return true; | |
bool go_left; | |
if (node->get_value() < element) | |
{ | |
go_left = false; | |
} | |
else | |
{ | |
go_left = true; | |
} | |
return search(node->get_child(go_left), element); | |
} | |
Node *get_leftmost_node(Node *node) | |
{ | |
Node *temp = node; | |
while (temp->get_child(true) != NULL) | |
{ | |
temp = temp->get_child(true); | |
} | |
return temp; | |
} | |
void delete_node(Node *node, Node *parent, int element, bool left_child) | |
{ | |
if (node == NULL) | |
return; | |
if (node->get_value() == element) | |
{ | |
Node *node_rchild = node->get_child(false); | |
Node *node_lchild = node->get_child(true); | |
if (node_lchild == NULL && node_rchild == NULL) | |
{ | |
parent->add_child(NULL, left_child); | |
} | |
else if (node_rchild == NULL) | |
{ | |
parent->add_child(node_lchild, left_child); | |
} | |
else | |
{ | |
Node *leftmost_rnode = get_leftmost_node(node_rchild); | |
leftmost_rnode->add_child(node_lchild, true); | |
parent->add_child(node_rchild, left_child); | |
} | |
return; | |
} | |
bool go_left; | |
if (node->get_value() < element) | |
{ | |
go_left = false; | |
} | |
else | |
{ | |
go_left = true; | |
} | |
delete_node(node->get_child(go_left), node, element, go_left); | |
return; | |
} | |
}; | |
int main() | |
{ | |
BinaryTree tree(3); | |
tree.add_child(2); | |
tree.add_child(5); | |
tree.add_child(7); | |
tree.add_child(-3); | |
tree.add_child(-1); | |
tree.add_child(1); | |
tree.add_child(4); | |
tree.add_child(6); | |
tree.add_child(-5); | |
tree.print_inorder(); | |
cout << tree.search(3) << " " << tree.search(1231) << endl; | |
tree.delete_node(-3); | |
tree.delete_node(5); | |
tree.delete_node(7); | |
tree.print_inorder(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct Node | |
{ | |
int data; | |
Node *next; | |
}; | |
class LinkedList | |
{ | |
Node *head; | |
int length; | |
public: | |
LinkedList(int value) | |
{ | |
head->data = value; | |
head->next = NULL; | |
length = 1; | |
} | |
Node *get_head() | |
{ | |
return head; | |
} | |
void insert(int value) | |
{ | |
Node *temp = head; | |
Node *new_node = new Node; | |
new_node->data = value; | |
new_node->next = NULL; | |
while (temp->next != NULL) | |
temp = temp->next; | |
temp->next = new_node; | |
length += 1; | |
} | |
void insert(int value, int pos) | |
{ | |
if (pos >= length) | |
{ | |
insert(value); | |
} | |
else if (pos == 0) | |
{ | |
Node *new_node = new Node; | |
new_node->data = value; | |
new_node->next = head; | |
} | |
else | |
{ | |
Node *temp = head; | |
Node *new_node = new Node; | |
new_node->data = value; | |
for (int i = 0; i < pos - 1; i++) | |
temp = temp->next; | |
new_node->next = temp->next; | |
temp->next = new_node; | |
} | |
length += 1; | |
} | |
void modify(int new_value, int pos) | |
{ | |
if (pos >= length) | |
return; | |
Node *temp = head; | |
for (int i = 0; i < pos; i++) | |
temp = temp->next; | |
temp->data = new_value; | |
} | |
void delete_node(int pos) | |
{ | |
Node *temp = head; | |
if (pos == 0) | |
{ | |
head = head->next; | |
} | |
else | |
{ | |
for (int i = 0; i < pos - 1; i++) | |
temp = temp->next; | |
temp->next = temp->next->next; | |
} | |
length -= 1; | |
} | |
void print_list() | |
{ | |
print_list(head); | |
cout << endl; | |
} | |
private: | |
void print_list(Node *node) | |
{ | |
if (node == NULL) | |
return; | |
cout << node->data << " "; | |
print_list(node->next); | |
} | |
}; | |
int main() | |
{ | |
LinkedList list(2); | |
list.insert(3); | |
list.insert(4, 3); | |
list.print_list(); | |
list.insert(2, 1); | |
list.print_list(); | |
list.modify(12, 1); | |
list.print_list(); | |
list.delete_node(0); | |
list.print_list(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <queue> | |
#include <stack> | |
using namespace std; | |
int main() | |
{ | |
queue<int> q; | |
q.push(2); | |
q.push(4); | |
q.push(6); | |
q.push(8); | |
while (!q.empty()) | |
{ | |
cout << q.front() << endl; | |
q.pop(); | |
} | |
cout << "Stacking\n"; | |
stack<int> s; | |
s.push(2); | |
s.push(4); | |
s.push(6); | |
s.push(8); | |
while (!s.empty()) | |
{ | |
cout << s.top() << endl; | |
s.pop(); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <set> | |
#include <iterator> | |
using namespace std; | |
int main() | |
{ | |
// empty set container | |
set<int, greater<int>> gquiz1; | |
// insert elements in random order | |
gquiz1.insert(40); | |
gquiz1.insert(30); | |
gquiz1.insert(60); | |
gquiz1.insert(20); | |
gquiz1.insert(50); | |
gquiz1.insert(50); // only one 50 will be added to the set | |
gquiz1.insert(10); | |
cout << "\nThe set gquiz1 is : "; | |
for (auto itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) | |
{ | |
cout << '\t' << *itr; | |
} | |
cout << endl; | |
// assigning the elements from gquiz1 to gquiz2 | |
set<int> gquiz2(gquiz1.begin(), gquiz1.end()); | |
// print all elements of the set gquiz2 | |
cout << "\nThe set gquiz2 after assign from gquiz1 is : "; | |
for (auto itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) | |
{ | |
cout << '\t' << *itr; | |
} | |
cout << endl; | |
// remove all elements up to 30 in gquiz2 | |
cout << "\ngquiz2 after removal of elements less than 30 : "; | |
gquiz2.erase(gquiz2.begin(), gquiz2.find(30)); | |
for (auto itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) | |
{ | |
cout << '\t' << *itr; | |
} | |
// remove element with value 50 in gquiz2 | |
int num; | |
num = gquiz2.erase(50); | |
cout << "\ngquiz2.erase(50) : "; | |
cout << num << " removed \t"; | |
for (auto itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) | |
{ | |
cout << '\t' << *itr; | |
} | |
cout << endl; | |
//lower bound and upper bound for set gquiz1 | |
cout << "gquiz1.lower_bound(40) : " | |
<< *gquiz1.lower_bound(40) << endl; | |
cout << "gquiz1.upper_bound(40) : " | |
<< *gquiz1.upper_bound(40) << endl; | |
//lower bound and upper bound for set gquiz2 | |
cout << "gquiz2.lower_bound(40) : " | |
<< *gquiz2.lower_bound(40) << endl; | |
cout << "gquiz2.upper_bound(40) : " | |
<< *gquiz2.upper_bound(40) << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
#include <string.h> | |
using namespace std; | |
int main() | |
{ | |
string str; | |
string s2 = "Bhsa"; | |
cin >> str; | |
cout << str << endl; | |
string s3 = str + s2; | |
cout << s3 << endl | |
<< s3[3] << endl; | |
s3[1] = 'a'; | |
cout << s3 << endl; | |
cout << s3.substr(2, 3) << endl; | |
cout << s3.append(str, 1, 2) << endl; | |
printf("%d", str.length()); | |
cout << str.compare(s2) << endl | |
<< str.compare(str) << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <unordered_map> | |
#include <string> | |
using namespace std; | |
class Pet | |
{ | |
public: | |
string name; | |
string species; | |
int price; | |
bool is_sold = false; | |
Pet(string name, string species, int cost) | |
{ | |
this->price = cost; | |
this->name = name; | |
this->species = species; | |
} | |
void pet_bought(int final_price) | |
{ | |
is_sold = true; | |
price = final_price; | |
} | |
bool is_pet_available() | |
{ | |
return ~is_sold; | |
} | |
}; | |
int main() | |
{ | |
unordered_map<string, Pet *> pet_list; | |
pet_list["Alice"] = new Pet("Alice", "Dog", 12); | |
pet_list["Lila"] = new Pet("Lila", "Dog", 10); | |
pet_list["Ila"] = new Pet("Ila", "Cat", 33); | |
string key = "Lila"; | |
if (pet_list.find(key) == pet_list.end()) | |
cout << key << " not found\n"; | |
else | |
{ | |
cout << "Found " << key << "\n"; | |
cout << pet_list[key]->species << endl; | |
} | |
key = "sd"; | |
if (pet_list.find(key) == pet_list.end()) | |
cout << key << " not found\n"; | |
else | |
{ | |
cout << "Found " << key << "\n"; | |
cout << pet_list[key]->species << endl; | |
} | |
for (auto i = pet_list.begin(); i != pet_list.end(); i++) | |
{ | |
cout << i->first << endl; | |
cout << i->second->species << " " << i->second->price << endl; | |
} | |
cout << pet_list.bucket_count() << " " << pet_list.bucket_size(0) << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <typeinfo> | |
using namespace std; | |
int main() | |
{ | |
vector<int> list; | |
int n; | |
cin >> n; | |
int temp; | |
for (int i = 0; i < n; i++) | |
{ | |
cin >> temp; | |
list.push_back(temp); | |
} | |
cout << list.front() << endl; | |
cout << "printing elements" << endl; | |
for (auto i = list.begin(); i != list.end(); i++) | |
{ | |
cout << *i << endl; | |
} | |
cout << "printing elements in Reverse" << endl; | |
for (auto i = list.rbegin(); i != list.rend(); i++) | |
{ | |
cout << *i << endl; | |
} | |
cout << "Size of the list is " << list.size() << endl; | |
list.resize(3); | |
cout << "Size of the list after resizing is " << list.size() << endl; | |
cout << "Max size of the list is " << list.max_size() << endl; | |
cout << "printing elements after resizing" << endl; | |
for (auto i = list.begin(); i != list.end(); i++) | |
{ | |
cout << *i << endl; | |
} | |
cout << list.at(2) << endl; | |
list.insert(list.begin() + 1, 12); | |
cout << "printing elements after Inserting" << endl; | |
for (auto i = list.begin(); i != list.end(); i++) | |
{ | |
cout << *i << endl; | |
} | |
cout << typeid(list).name() << endl; | |
list.erase(list.end() - 1); | |
cout << "printing elements after Inserting" << endl; | |
for (auto i = list.begin(); i != list.end(); i++) | |
{ | |
cout << *i << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment