Posts

Print even and odd using two thread in synchronized way

 Print even and odd using two thread in synchronized way : Example :  ODD Thread will print  1                    EVEN Thread will print  2                  ----------------------------------------------                   ODD Thread will print till pre-define range                   EVEN Thread will print till pre-define range Solutions : #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #define MAXSIZE 1000 using namespace std; mutex mu; condition_variable con; int mycount = 1; void printodd() {        cout << "i am in ODD thread" << endl;                 while (mycount < MAXSIZE) {         unique_lock<mutex> mylock(mu); ...

Implementation of Queue using Array

 _________________________Main.cpp_____________________________________ #include"OwnQueue.h" int main() {     std::cout << "MY.. QUEUE!\n" << endl;     OwnQueue queue;     queue.dequeue();     queue.enqueue(10);     queue.enqueue(20);     queue.enqueue(0);     queue.enqueue(40);     cout << queue.peek() << endl;     queue.enqueue(50);     queue.enqueue(70);     cout << queue.isempty() << endl;     cout << queue.isfull() << endl;     queue.dequeue();     queue.dequeue();     queue.dequeue();     queue.dequeue();     cout << queue.peek() << endl;     queue.dequeue();     queue.dequeue();     cout << queue.isempty() << endl;     cout << queue.isfull() << endl;      queue.printqueue...

Print Left View of Binary tree

// Print Left view of Binary tree #include <iostream> #include <vector> using namespace std; class tree {        public:      int data ;   tree * left, *right;      tree(int data)   {       this->data = data;       left = right = nullptr;   } }; void printleftview(tree *node, int level) {     static int maxlevel = 0;          if(node == nullptr)     {         return;     }          if(maxlevel < level)     {       maxlevel = level;       cout << node->data << endl;     }          printleftview(node->left, level+1);     printleftview(node->right, level+1);      } int main() {        cout<<"left view of binary tree" << endl;...

optimized bubble sort

#include <iostream> #include <vector> using namespace std; int main() {     vector<int> vec = {10,20,30};          //booble sort     bool  check = false;          for(int i = 0; i<= vec.size(); i++)     {                   for (int j= i+1; j<vec.size(); j++)         {                          if(vec[j] < vec[i])             {                 int temp = vec[i];                 vec[i] = vec[j];                 vec[j] = temp;                 check = true;             }                   ...

Creating Dynamic Array using C++

 #include <iostream> using namespace std; template <typename T> class Array { private: T* base; int size; public : Array(T arr[], int length) { base = new T[sizeof(arr)]; size = length; for (int i = 0; i < length; i++) base[i] = arr[i]; } void print(); }; template <typename T> void Array<T>::print() { for (int i = 0; i < size; i++) cout << " " << *(base + i) << endl; } int main() { int arr1[] = {10, 20, 30}; Array<int> obj(arr1, 3); obj.print(); char arr2[] = { 'v', 'a', 'r','u','n' }; Array<char> obj1(arr2, (sizeof(arr2) / sizeof(arr2[0]))); obj1.print(); return 0; }

Dynamic Memory allocation in C++ for 1d, 2d and 3d array

#include <iostream> #define N 10 // Dynamically Allocate Memory for 1D Array in C++ int main() { // dynamically allocate memory of size N int* A = new int[N]; // assign values to allocated memory for (int i = 0; i < N; i++) A[i] = i + 1; // print the 1D array for (int i = 0; i < N; i++) std::cout << A[i] << " "; // or *(A + i) // deallocate memory delete[] A; return 0; } //2-Dimentional Array. // using single pointer; #include <iostream> // M x N matrix #define M 4 #define N 5 // Dynamically Allocate Memory for 2D Array in C++ int main() { // dynamically allocate memory of size M*N int* A = new int[M * N]; // assign values to allocated memory for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) *(A + i*N + j) = rand() % 100; // print the 2D array for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) std::cout << *(A + i*N + j) << " ...

Write Your Own String Class:

class OwnString { private : char *string1 = NULL; int size; public: OwnString(void) { string1 = NULL; } OwnString(const char * strData) { size = strlen(strData); string1  = new char[size + 1]; int i = 0; while (*strData) { string1[i] = *strData; strData++; i++; } } OwnString & operator=(OwnString & obj) { if (this != &obj) { delete[]string1; size = obj.size; string1 = new char[size+1]; int i = 0; while (i<size+1) { string1[i] = obj.string1[i]; //strData++; i++; } } return *this; } OwnString (const OwnString & obj) { if (this != &obj) { delete[]string1; size = obj.size; string1 = new char[size + 1]; int i = 0; while (i < size + 1) { string1[i] = obj.string1[i]; //strData++; i++; } } } void print() { for (int i = 0; i < size; i++) { cout << string1[i]; ...