Posts

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

Q3 copy constructor program .

 #include<iostream> using namespace std; class A { int x, y; public : A():x(20),y(40) { } A(const A &ob){ x=ob.x; y=ob.y; } void show() { cout<<x<<endl<<y<<endl; } }; int main() { A obj1; A obj2=obj1; //copy constructor is called . obj2.show(); return 0; }

Q2 program for Assignment operator/unary operator(prefix and postfix )/binary operator overloading .

#include<iostream> using namespace std; class A { int x,y; public : A() { x=10; y=20; } void show(); A operator +(A obj) { A temp; temp.x=x+obj.x; temp.y=y+obj.y; return temp; } A operator -(A obj) { A temp; temp.x=x-obj.x; temp.y=y-obj.y; return temp; } //prefix expression overloaded A operator ++() { x++; ++y; return *this; } //postfix expression overloaded A operator ++(int ) { x++; ++y; return *this; } A operator =(A obj) { this->x=obj.x; this->y=obj.y; return *this; } }; void A::show(){ cout<<x<<endl<<y<<endl; } int main() { A obj1,obj2,obj3; obj1=obj1+obj2;//overloaded assignment operator and binary operator will be called . obj1.show(); ++obj1; //overloaded unary "prefix"  operator will be  called . obj1++; //overloaded unary "post"  operator will be  called . obj1.show(); obj3=obj1 ;//overloaded assignment operator will be called . obj3.show(); return 0; }

Q 1 write pograme in c++ for passbyvalue/passbyrefrence/passbyaddress .

#include<iostream> using namespace std; class passvalue { public:     void passbyvalue(int x, int y); //pass by value     void passbyrefrence(int &x, int &y);//pass by refrence.     void passbyaddress(int *x, int *y); //pass by address. }; void passvalue::passbyvalue(int x, int y) { x=10; y=20; } //actual value will be changed . void passvalue::passbyrefrence(int &x, int &y) { //passbyrefence : cout<<"passbyrefrence called"<<endl; x=60; y=50; } //actual value will be changed . void passvalue::passbyaddress(int *x, int *y) { cout<<"passbyaddress is  called"<<endl; *x=70; *y=80; } int main() { int i=40,j=30; passvalue B; cout<<"actual value"<<endl; cout<<i<<endl<<j<<endl; B.passbyvalue(i,j); cout<<"passbyvalue called"<<endl; cout<<i<<endl<<j<<endl; cout<<"passbyrefrence  called"<<endl; B.passbyrefrence(i,j...