Posts

Showing posts from 2019

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

Calculating Daylight saving and UTC offset in QT

void getTimeZoneOffset() {     QString UTC_offset;     time_t current_time;     time(&current_time);     QString Daylighsaving ;     struct tm *timeinfo = localtime(&current_time);     double offset = (timeinfo->tm_gmtoff)/60;     if (timeinfo->tm_isdst)     {         offset += 60;         Daylighsaving = "1";     }     if (offset > 0)     {         offset = -offset;     }     else{         offset = -(offset);     }     UTC_offset = QString::number(offset); }

# smart pointer implementation in c++

#include <iostream> using namespace std; template <class T> class smart { protected : T *ptr; public : explicit smart(T *data = NULL) { ptr = data; } ~smart() { delete ptr; } T & operator *() { return *ptr; } }; class test { public : test() { cout << "test - constructor " << endl; } ~test() { cout << "test - destroctor " << endl; } }; int main() { smart<int> obj(new int(20)); smart<char> obj2(new char('A')); smart<test> obj3(new test()); cout << *obj << endl; cout << endl; cout << *obj2 << endl; }

Printing the Prime no (As per the user Input )

#include <iostream> using namespace std; void  primeno(int x) { int k ; for (int i = 2; i < x; i++) { k = 0; for (int j = 1; j <= i; j++) { if (i%j == 0) { k++; } } if (k == 2) { cout << i << " " <<endl; } } } int main() { int primenosize;         std::cout << "prime no \n" <<endl; cout << "input range to print the prime no" <<endl; cin >> primenosize; primeno(primenosize); return 0; }

Printing the odd and even no using Pthread

#include<iostream> #include <pthread.h> using namespace std; void *even(void * size) {             int x = reinterpret_cast<int> (size);         cout << "even no" <<endl;     for(int i = 0; i<x; i++)     {         if(i%2 == 0){                         cout << i << endl;         }             }     cout <<endl;     cout <<"even thread is terminited ";     cout <<endl; } void *odd(void *size ) {     pthread_t tid2;         int x = reinterpret_cast<int> (size);         cout << "odd no" <<endl ;     for(int i = 0; i<x; i++)     {         if(i%2 != 0){                         cout << i <<endl ;         }             }             pthread_create(&tid2, NULL, even, (void*)x);         cout <<endl;     pthread_join(tid2, NULL);         cout <<"odd thread is terminited ";     cout <<endl; } int main() {     int size;         cout <<

Program for Page Replacement Algorithms (LRU)

// LRU.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include "pch.h" #include <iostream> #include <unordered_map> #include <unordered_set> using namespace std; int pageFaults(int pages[], int n, int capacity) { unordered_set<int> s; unordered_map<int, int> indexes; // Start from initial page int page_faults = 0; for (int i = 0; i < n; i++) { // Check if the set can hold more pages if (s.size() < capacity) { // Insert it into set if not present // already which represents page fault if (s.find(pages[i]) == s.end()) { s.insert(pages[i]); // increment page fault page_faults++; } // Store the recently used index of // each page indexes[pages[i]] = i; } // If the set is full then need to perform lru // i.e. remove the least recently used page // and insert the current page else { // Che

STACK IMPLEMENTATION USING LINKLIST

// stack_implemention_using_linked_list.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdlib.h> #include <iostream> using namespace std ; struct stack { int data ; struct stack *next ; }; struct stack *top = NULL; struct stack *ptop= NULL; struct stack *last= NULL; class stack_List { protected : struct stack * node; stack_List *stack_obj; /*stack_List() { node = NULL; }*/ /*~stack_List() { delete stack_obj; }*/ public : void push(int data); void pop (); void printData(); }; void stack_List::push(int data) { struct stack *temp  = NULL; temp = new (nothrow) stack ; // notthorw if memroy not availble in heap area then it won't throe badallocation insated of it will return null if(temp == NULL) { cout << "Memory Allocation Fialed"; } if(top == NULL ) { temp->data = data; temp->next = NULL; top = temp; ptop =

how to find the smallest and 2nd smallest number in an array

#include <iostream> using namespace std; int min(int arr[], int size,  int &secound_min) { int min = arr[0]; int sec_min = arr[1]; for (auto i = 0; i < size; i++) { if (arr[i] < min) { sec_min = min; min = arr[i]; } } secound_min = sec_min; return min; } int main() { int arr[10] = {5,9,2,1,10,0,-1,-2,0,3}; int secound_min; int min_vaue = min(arr,10, secound_min); cout << min_vaue << endl << secound_min << endl; }

How to Install GoogleTest Frame Work On Linux(Ubuntu) PC .

Step 1: open  Terminal (ctrl+alt+t) and type sudo apt-get install libgtest-dev (wait for few seconds) if you have already camke in your pc then below command is not required else you can use below command to install cmake : sudo apt-get install cmake step 2 : go to below directory cd /usr/src/gtest step 3 : run the below commands : sudo cmake CMakeLists.txt sudo make step   4 :  sudo cp *.a /usr/lib