RSS

Author Archives: dikshamodi

PROGRAM TO OVERLOAD MULTIPLICATION OPERATOR TO WORK WITH FRACTIONS

//THIS PROGRAM OVERLOADS MULTIPLICATION OPERATOR TO WORK WITH FRACTIONS AS WELL AS INTEGERS

#include <iostream>
#include <cstdlib>

using namespace std;

class fraction
{
	int numerator,denominator;
	public:
		fraction(int num = 0, int den = 1):numerator(num), denominator(den)
		{}
		void print()
		{
			cout<<numerator<<"/"<<denominator<<endl;
		}
		friend fraction operator*(fraction, fraction);
		friend fraction operator*(fraction, int);
		friend fraction operator*(int, fraction);
};

fraction operator*(fraction a, fraction b)
{
	int num = a.numerator * b.numerator;
	int den = a.denominator * b.denominator;
	return fraction(num,den);
}

fraction operator*(fraction a, int b)
{
	int num = a.numerator * b;
	int den = a.denominator;
	return fraction(num,den);
}

fraction operator *(int a, fraction b)
{
	return operator*(b,a);
}

int main()
{
	int choice;
	char wish;
	do{
		system("clear"); //change to system("cls") if on a windows machine
		cout<<"\nChoose an Option\n";
		cout<<"\n1. MUltiply two fractions\n";
		cout<<"\n2. Multiply a fraction and an integer\n";
		cin>>choice;
		while(choice != 1 && choice != 2)
		{
			cout<<"\nIncorrect choice\nEnter a valid one\n";
			cin>>choice;
		}
		if(choice == 1)
		{
			int num,den;
			cout<<"\nEnter the numerator and denominator of first fraction respectively\n";
			cin>>num>>den;
			while(den == 0)
			{
				cout<<"\nDenominator cannot be zero. Enter once again\n";
				cin>>den;
			}
			fraction f1(num,den);
			cout<<"\nEnter the numerator and denominator of second fraction respectively\n";
			cin>>num>>den;
			while(den == 0)
			{
				cout<<"\nDenominator cannot be zero. Enter once again\n";
				cin>>den;
			}
			fraction f2(num,den);
			fraction product = f1*f2;
			cout<<"\nThe product is: \t";
			product.print();
		}
		else if(choice == 2)
		{
			int num,den;
			cout<<"\nEnter the numerator and denominator of fraction\n";
			cin>>num>>den;
			while(den == 0)
			{
				cout<<"\nDenominator cannot be zero. Enter once again\n";
				cin>>den;
			}
			fraction frac(num,den);
			cout<<"\nEnter the integer\n";
			cin>>num;
			fraction product = frac*num;
			cout<<"\nTHe product is: \t";
			product.print();
		}
		cout<<"\nDo once again? (Y/n)\t";
		cin>>wish;
		while(wish != 'Y' && wish != 'N' && wish != 'y' && wish != 'n')
		{
			cout<<"\nInvalid input\nEnter valid one\n";
			cin>>wish;
		}
	}
	while(wish == 'Y' || wish == 'y');
	
	return 0;
}

Advertisements
 
Leave a comment

Posted by on May 23, 2016 in Miscellaneous Programs

 

PROGRAM TO FIND THE NUMBER OF TRAILING ZEROES IN THE FACTORIAL OF A NUMBER

#include <iostream>
#include <cmath>
 
using namespace std;
 
int main()
{
  long long int i = 0,num;
     cin>>num;
     long long power = 1,facts = 0;
     while(floor(num/pow(5,power)) != 0)
     {
       facts += floor(num/pow(5,power));
       power++;
     }
     cout<<facts<<'\n';
    return 0;
} 
 
Leave a comment

Posted by on April 21, 2016 in Miscellaneous Programs

 

PROGRAM TO DETECT THE PRESENCE OF A LOOP IN A LINKED LIST (FLOYD’S CYCLE FINDING ALGORITHM)

// THIS PROGRAM CHECKS FOR THE PRESENCE OF A LOOP IN A LINKED LIST (FLOYD'S CYCLE FINDING ALGORITHM)

#include <iostream>

using namespace std;

struct node
{
    int info;
    node *next;
};

bool looppresent(node *start);

int main()
{
    node *start;
    int i = 2;
    char wish = 'Y';
    start = new node;
    node *ptr;

    cout<<"Node 1: ";
    cin>>start->info;
    start->next = NULL;

    ptr = start;

    do
    {
        cout<<"\nNode "<<i<<": ";
        node *temp = new node;
        ptr->next = temp;
        ptr = temp;
        cin>>ptr->info;
        cout<<"\nContinue ??\n";
        cin>>wish;
        i++;
    }
    while(wish == 'Y' || wish == 'y');
    ptr->next = NULL;

    //uncomment the below line if you want to add a loop
    //ptr->next = start->next;

    if(looppresent(start) == true)
    {
        cout<<"\n Loop is present in the linked list\n\n";
    }
    else
    {
        cout<<"\n The linked list is loop free\n";
    }

    return 0;
}

bool looppresent(node *start)
{
    node *first, *second;
    first = second = start;
    second = first->next->next;

    while(first != NULL && second != NULL && first != second)
    {
        first = first->next;
        if(second->next == NULL || second ->next ->next == NULL)
            return false;
        second = second->next->next;
    }

    if(first == second)
        return true;
    else
        return false;
}

 

PROGRAM TO DELETE ALL THE ELEMENTS EQUAL TO AN ENTERED ELEMENT FROM THE STACK

// PROGRAM TO IMPLEMENT THE DELETE FUNCTIONALITY IN A STACK

#include <iostream>
#include <cstdlib>
#include <process.h>
#define size 10

using namespace std;

class stack{
	int data[size];
	int top;
	
	public:
		stack()
		{
			top = -1;
		}
		void push();
		void pop();
		void display();
		void del(int);
};

int main()
{
	stack s;
	char wish = 'Y';
	int choice;
	
	do{
		system("cls");
		cout<<"\nImplementation of Delete functionality in Stack....\n\n";
		cout<<"\nPlease enter a choice\n\n";
		cout<<"\n1. PUSH AN ELEMENT\n\n";
		cout<<"\n2. POP AN ELEMENT\n\n";
		cout<<"\n3. DISPLAY THE ELEMENTS OF STACK\n\n";
		cout<<"\n4. DELETE ALL THE ELEMENTS EQUAL TO A CERTAIN ELEMENT FROM THE STACK\n\n";
		cout<<"\n5. EXIT THE PROGRAM\n\n";
		cout<<"\n\nEnter your choice\t";
		cin>>choice;
		
		while(choice<1 || choice >5)
		{
			cout<<"\nInvalid choice.... Enter a valid one below\n\n";
			cout<<"Choice : \t";
			cin>>choice;
		}
		system("cls");
		int var;
		switch(choice)
		{
			case 1: s.push();
					cout<<"\n\nThe stack staus is \n\n";
					s.display();
					break;
			
			case 2: s.pop();
					cout<<"\n\nThe stack staus is \n\n";
					s.display();
					break;
			
			case 3: cout<<"\n\nThe stack staus is \n\n";
					s.display();		
					break;
					
			case 4: cout<<"\n\nEnter the element all elements equal to which should be deleted from stack\n\n";
					cin>>var;
					s.del(var);
					cout<<"\n\nThe stack staus is \n\n";
					s.display();
					break;
					
			case 5: cout<<"\nEXITING FROM THE PROGRAM \n\n";
					exit(0);
					break;	
		}
		
		cout<<"\n\nDo any more operation? (Y/N)";
		cin>>wish;
		
		while(wish != 'y' && wish !='Y' && wish !='n' && wish != 'N')
		{
			cout<<"\n\nInvalid choice \n\n Please enter correct one below...\n";
			cout<<"\nChoice (Y/N): \t";
			cin>>wish;
		}
		
	}while(wish == 'y' || wish == 'Y');
	
	cout<<"\n\nThanks for coming.... Do come again. \n\n";
	cout<<"\nHave a nice day ahead :)";
	
	return 0;
}

void stack::push() 
{
	int element;
	if(top == size-1)
	{
		cout<<"\n\nERROR !!!!!   STACK OVERFLOW !!!!!!\n\n";
		exit(1);
	}
	else
	{
		top++;
		cout<<"\n\nEnter the element to be pushed:\t";
		cin>>element;
		data[top] = element;
	}
	
	cout<<"\n\nINSERTION SUCCESSFUL .....\n\n";
}

void stack::pop()
{
	if(top == -1)
	{
		cout<<"\n\nERROR !!!!!! STACK UNDERFLOW !!!!!!\n\n";
		exit(1);
	}
	else
	{
		int item = data[top];
		top--;
		cout<<"\n\nDeleted element is:\t "<<item;
	}
}

void stack::display()
{
	if(top == -1)
	cout<<"\n\nEMPTY !!!!!\n\n";
	else
	{
		for(int i = top; i >=0; i--)
		{
			if(i == top)
			{
				cout<<"Top -->  "<<data[i]<<'\n';
			}
			else
				cout<<"         "<<data[i]<<'\n';
		}                           
	}
}

void stack::del(int val)
{
	stack tempstack;
	
	for(;top>=0;)
	{
		if(data[top] == val)
		{
			top--;
		}
		else
		{
			//push into temp stack
			tempstack.top++;
			tempstack.data[tempstack.top] = data[top]; 
			top--;
		}
	}
	
	while(tempstack.top>=0)
	{
		top++;
		data[top] = tempstack.data[tempstack.top];
		tempstack.top--;
	}
	
	cout<<"\n\nDeletion Successful !!!!!\n\n";
}
 
5 Comments

Posted by on March 29, 2016 in Stacks, Queues and Linked Lists

 

PROGRAM TO CHECK THE TYPE OF SORTING IN AN ARRAY (2ND LOGIC)

//THIS PROGRAM CHECKS WHETHER AN ARRAY IS SORTED OR NOT. IF SORTED THEN IT CHECKS THE TYPE OF SORT - ASCENDING OR DESCENDING

#include <iostream>

using namespace std;

char checksort(int arr[], int size);

int main()
{
 int size;
 cout<<"\nEnter the size of the array \n";
 cin>>size;

 int *arr = new int[size];

 cout<<"\nInput the array\n";

  for(int i = 0; i<size; i++)
  {
   cin>>arr[i];
  }

char s = checksort(arr,size);

if(s == 'A')
 cout<<"\nAscending...\n";
else if(s == 'D')
 cout<<"\nDescending...\n";
else
 cout<<"\nNot ordered...\n";

return 0;
}

 


char checksort(int arr[], int size)
 {
 char sort = 'N';
 int flag = 0;
//check ascending
 for(int i = 0; i<size-1; i++)
 {
 if(arr[i]>arr[i+1])
 {
 flag = 1;
 break;
 }
 }

if(flag == 0)
 {
 return ('A');
 }

//check descending

flag = 0;

for(int j = 0; j<size-1; j++)
 {
 if(arr[j]<arr[j+1])
 {
 flag = 1;
 break;
 }
 }

if(flag == 0)
 {
 return ('D');
 }

return 'N';
 }

 

INSERTION SORT: DESCENDING ORDER (BETTER LOGIC)

// THIS PROGRAM SORTS AN ARRAY IN DESCENDING ORDER USING INSERTION SORT

#include <iostream>

using namespace std;

void insertionsort(int arr[], int size);

int main()
{
 int arr[100], size;
 cout<<"Enter the size of the array \n";
 cin>>size;

 cout<<"\nInput the array \n";

 for(int i = 1;i<=size; i++)
 {
  cin>>arr[i];
 }

 arr[0] = 32767;

 insertionsort(arr,size);

 cout<<"\nThe sorted array is \n";

 for(int i = 1; i<=size; i++)
 {
  cout<<arr[i]<<' ';
 }

return 0;
}

void insertionsort(int arr[], int size)
{
 for(int j = 2; j<=size; j++)
 {
  int key = arr[j];
  int k = j-1;

  while(key>arr[k])
  {
   arr[k+1] = arr[k];
   k--;
  }

  arr[k+1] = key;
 }
}
 

IMPLEMENTATION OF CIRCULAR QUEUE USING ARRAYS (2ND LOGIC)

// THIS PROGRAM IMPLEMENTS CIRCULAR QUEUE

#include <iostream>
#include <cstdlib>
#define size 4

using namespace std;

class queue{
	int front,rear;
	int q[size];
	
	public:
		queue()
		{
			front = rear = -1;
		}
		void insert();
		int deleteq();
		void display();
};

int main()
{
	queue q;
	int choice;
	char wish;
	
	do{
		system("cls");
		cout<<"\nIMPLEMENTING CIRCULAR QUEUE USING ARRAYS \n";
		cout<<"\nPLEASE ENTER A CHOICE\n";
		cout<<"\n1. INSERT AN ELEMENT\n";
		cout<<"\n2. DELETE AN ELEMENT\n";
		cout<<"\n3. SHOW QUEUE STATUS\n";
		cout<<"\n4. EXIT FROM THE PROGRAM\n\n\n";
		cin>>choice;
		while(choice <1 || choice > 4)
		{
			cout<<"\n\nInvalid choice\n";
			cout<<"\nPlease enter your choice again\n\n";
			cin>>choice;
		}
		
		int del;
		
			switch(choice)
			{
				case 1: q.insert();
						cout<<"\n\nThe queue after insertion is\n\n";
						q.display();
						break;
				case 2: del = q.deleteq();
						cout<<"\n\n'"<<del<<"'"<<" deleted from the queue\n\n";
						cout<<"\nUpdated queue status is\n\n";
						q.display();
						break;
				case 3: cout<<"\n\nThe queue status is \n\n";
						q.display();
						break;
				case 4: cout<<"\n\nEXITING FROM THE PROGRAM...\n\n";
						exit(0);
			}
			
	cout<<"\n\nDo any other operation?\n\n Press 'y' or 'Y' to continue. \n\nAny other option to exit\n\n";
	cin>>wish;
	}while(wish == 'Y' || wish == 'y');	
	
	cout<<"\nThanks!!! Do come again\n";
	
	return 0;
}

void queue::insert()
{
	int item;
	if((rear+1)%size == front)
	{
		cout<<"\nOverflow !!!!! Terminating.....";
		exit(1);
	}
	else if(front == -1 && rear == -1)
	{
		front = 0;
		rear = 1;
		cout<<"\nEnter the element to be inserted\n";
		cin>>item;
		q[front] = item;
		cout<<"\nInsertion Successful\n\n";
	}
	else
	{
		cout<<"\nEnter the element to be inserted \n";
		cin>>item;
		q[rear] = item;
		rear = (rear+1)%size;
		cout<<"\nInsertion Successful\n\n";
	}
}

int queue::deleteq()
{
	int item;
	if(front == rear)
	{
		cout<<"\nUnderflow!!!. Terminating....\n";
		exit(-1);
	}
	else
	{
		item = q[front];
		front = (front+1)%size;
		cout<<"\nDeletion Successful !!!\n\n";
	}
	
	return item;
}

void queue::display()
{
	int ctr = front;
	
	while(ctr != rear)
	{
		cout<<q[ctr]<<' ';
		ctr = (ctr+1)%size;
	}
}