Wednesday, August 28, 2013

WaTER JuGgggggg ProBlem

After a long time i am writing post in my blog. Hereafter i am planning to  post one algorithm on each day. It will be useful to me and peoples who are reading my blog also. I will try guys. I am not sure. Today i am discussing abt most famous puzzle (water jug problem.)
Question:

Given two vessels, one of which can accommodate a liters of water and the other which can accommodate b liters of water, determine the number of steps required to obtain exactly c liters of water in one of the vessels. You have infinite supply of water.
At the beginning both vessels are empty. The following operations are counted as ‘steps’:
  • emptying a vessel,
  • filling a vessel,
  • pouring water from one vessel to the other, without spilling, until one of the vessels is either full or empty.
For example , you have 3 litres of water jug and 5 litres capacity of water jug, users wants 4 litres in any of jugs means , how you will get it. Here the following processes will help u to get 4 litres.

assume A-5, B-3
                          A              B
fill ()                  5               0
transfer()            2               3
empty()              2               0
transfer()            0               2
fill()                   5               2
transfer()            4               1
 
wow. finally u got 4 litres in one jug. I hope u understand the above processes. Next we will discuss how to write programs for getting no. of steps to reach final output.

Assume A-5, B-3, C-4

if(A>C)  we can get  C litres of water in A
if(B>C) we can get C litres of water in B
if both (A and B > C) we can get C litres of water in A or B

Algorithm is ,

if A>C & B<C)  transfer A->B until you get C litres of water in A
and viceversa.
in last case. if both are bigger than C means, transfer from A->B or B->A.

in first case (A>C), first fill 5 to A, and transfer to B upto it reaches the capacity and

return the transferred to A and reduces the current amount in A .

Now I hope you understand the logic.

Now we will write program for this.

Program:

#include<iostream>
#include"Jug.h"
using namespace std;
int main()
{
 Jug j1(3,0),j2(5,0);

 int no=4;
 int count;
 count=j2.make(no,j1); // do comparisons which objects passes , other object as argument. i am very lazy doing like these things.

 if(!count)
  cout<<"failed to make"<<endl;
 else
  cout << "no. of steps : " << count << endl;

}

program2: jug.cpp

#include "Jug.h"
#include<iostream>
using namespace std;
int count=0;
Jug::Jug(int c,int curr)
{
 this->Capacity=c;
 this->Current=curr;
}

Jug::~Jug(void)
{
}
int Jug::make(int no,Jug &V2)
{

 if(CheckGCD(Capacity,V2.Capacity)!=1 || Capacity< no)  // we have to check if gcd is exist or not.
 {
  cout<< "Solution not exist" << endl;
 
  return 0;
 }
 while(Current!=no)  // until u get the 4
 {
  ++count;
 
  if(!Current)
  {
   Current=Capacity;   // if empty means fill the jug with full capacity
 
   cout<<"fill() of high quantity vessel"<<endl;
  }
 
  cout<<"current: "<<Current<<endl;
    
  cout<<"transfer()"<<endl;
  Current=Current-V2.transfer(Current);// transfer the amount of water in Jug 1
 
  if(!Current)
 
   cout<<"empty() of high quantity vessel"<<endl;
  if(V2.Current==V2.Capacity)
    {
     V2.empty();
    
     cout<< "empty() of less quantity vessel" <<endl;
    }
 }
 cout<<"current"<<Current<<endl;
 return count;
}

int Jug::transfer(int amount)
{
 int transferred=0;
 if(Current+ amount > Capacity )
 {
  transferred=Capacity-Current;
 
  Current=Capacity;
  cout<<"fill() of less quantity vessel" << endl;
 
  return transferred;
 }
 else

 {
  Current=Current+ amount;

  cout<<"fill() of less quantity vessel" << endl;
  return amount;
 }

}
void Jug::empty()
{
 Current=0;
}
int CheckGCD(int a,int b)
{
 return b? CheckGCD(b, a % b) : a;
}

Jug.h contains declarations for all the functions.

int CheckGCD(int, int);
class Jug
{
 int Capacity,amount,Current;
public:
 Jug(int , int);
 ~Jug(void);
 void empty();
 int transfer(int );
 int make(int , Jug &);

};

reply if any test cases are wrong in this program.


References:

                  http://amrendra.in/2012/09/13/water-jug-problem/







 

Tuesday, January 22, 2013

Recover Ubuntu/Linux mint After Installing Win 7/xp/vista.

Hi guys,
           Many of the users, using dual boot system (windows 7 + linux mint/ubuntu). Win 7 created a lot of problem due to viruses. So many of the times, win 7 crashes. So when u are installing win 7 again , old ubuntu got disappeared. This post will help u a lot to recover old ubuntu. there is no need to reinstall ubuntu/linux mint again.
           By default, Windows 7 takes over your boot-up process and wants to be your only OS, and Linux treats Windows like a weekend hobby you keep in a shed somewhere on your hard drive. Follow through this guide, and I'll explain how to rebuild a system from the ground up with Windows 7 and Ubuntu, with either a backed-up and cleaned-out hard drive (recommended) or Windows 7 already installed. When we're done, you can work and play in either operating system, quickly and conveniently access your documents, music, pictures, and other files without worry or inconvenience, and boot into either system without having to worry about whether Windows is going to get mad at you.

For this , u need win7 installation disc, ubuntu/mint live cd, internet connection for downloading boot-repair.

Boot-Repair  is an Ubuntu tool which can easily resolve such issues. As this is a GUI based application, therefore, you will require being logged into your operating system for it to work. Therefore, despite the boot issues, you should be able to get access to your desktop for this app to resolve the boot issues. Having said that, the issues which it resolves mainly do not cause the inability of a system to loose boot functionality. For example, the GRUB boot problems may be causing inconsistency in allowing you to boot from your OS. Similarly, if you have installed another operating system, you might not be able to login from Ubuntu by default.

So, by reinstalling grub we can recover old ubuntu/mint.

steps:

1. after installing win7, insert live cd of ubuntu/linux mint.

2. connect to internet in ubuntu/mint.(many of struck with connecting to internet using wireless card , for this, use wvdial.exe or create a network profile and connect via mobile broadband)

3. after connection, open terminal, and

type the following commands:

-> sudo add-apt-repository ppa:yannubuntu/boot-repair
-> sudo apt-get update
-> sudo  apt-get install boot-repair


first cmd link to boot-repair website.

and 2nd cmd update all packages.because when u r using apt-get install cmd without update, it shows,
 error: command not found.

3rd cmd to download boot-repair.

after downloading boot-repair,open this app.

4. click recommended repair. after repair, it shows url. note it for future reference.

5. If the repair did not succeed, indicate the URL to people who help you by email or forum.

 see the url: https://help.ubuntu.com/community/Boot-Repair .