Monday 2 November 2015

Reverse an Order Of a String Using JAVA

Reverse an Order Of a String Using JAVA

In this post i am going to show you how to reverse the order of the given string. the order of the string will be reverse means the last whole word become the first and first become the last word of the string and other remaining word also change the position in the order they appear.

Example: jiten vaghela
ouput: vaghela jiten

Download Program: Click here

Program:

import java.util.*;

public class reverseOrder{
public static void main(String [] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter your String:");
String data=input.nextLine();
String [] arrayData=data.split(" ");

data=new String("");
for(int i=arrayData.length-1;i>=0;i--){
data+=arrayData[i]+" ";
}
System.out.println("Reverse String:"+data);
}
}

output:


Count Sub String Occurrence Algorithm With Example using JAVA

Count Sub String Occurrence Algorithm With Example using JAVA

Sub String Occurrence count means to check how many  times a particular  part of a string repeat in the given string.

Algorithm:

step 1: get the length of MainString and SubString variables and initialize i=j=count=0;
            ex:
            len1=mainString.length();
            len2=subString.length();

Step 2: start loop from i=0 to len1-len2;
            initialize the j with 0 and flag with true;

Step 3: check whether character of mainString at position i is equals to character of SubString at position j ?
             ex:
             if(mainString.charAt(i)==subString.charAt(j))

Step 4: if both are sem then start another loop with j=1 and j is j<=subString.length(); now check whether the character of mainString at position i+j is not Equals to character of SubString at position j. then make the flag = false and break the loop.
           ex:
           for(j=1;j<=SubString.length();j++){
                         if(mainString.charAt(i+j)!=subString(j)){
                                     flag=false;
                                       break;
                          }
            }

Step 5:out side the inner for loop check whether the flag value is true if yes then count= count+1
            ex:
            if(flag)
               count++;

Download Program: Click Here

Program:

import java.util.*;

public class reverse{
public static int getSubString(String mainString,String subString){
int max=1+mainString.length()-subString.length();
int count=0;
for(int i=0;i<max;i++){
boolean flag=true;
int j=0;
if(mainString.charAt(i)==subString.charAt(j)){
for(j=1;j<subString.length();j++){
if(mainString.charAt(i+j)!=subString.charAt(j)){
flag=false;
break;
}
}

}else
flag=false;
if(flag)
count++;
}
return count;
}
public static void main(String [] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter Main String:");
String mainString=input.nextLine();
System.out.print("Enter Sub String :");
String subString=input.nextLine();

int count=getSubString(mainString,subString);

System.out.println("String Found:"+count);
}
}

compile the above code:javac subStringcounter.java
To Run the code: java subStringcounter

Output:


Tuesday 27 October 2015

Fibonacci Series Using JAVA

Fibonacci Series Using JAVA


More Details About Fibonacci Series: Click Here

Program in JAVA

public class fibo{
public static void main(String args[]){
int no=10;
int next,prev,sum;
next=1;
prev=0;
for(int i=0;i<no;i++){
System.out.print(prev+"  ");
sum=next+prev;
prev=next;
next=sum;
}
}
}

Download Program File: Click Here

Output:


Pascal's Triangle Using JAVA

Pascal's Triangle Using JAVA


In mathematics, Pascal's triangle is a triangular array of the binomial coefficients. In much of the Western world it is named after French mathematician Blaise Pascal, although other mathematicians studied it centuries before him in India, Iran, China, Germany, and Italy.
The rows of Pascal's triangle (sequence A007318 in OEIS) are conventionally enumerated starting with row n = 0 at the top (the 0th row). The entries in each row are numbered from the left beginning with k = 0 and are usually staggered relative to the numbers in the adjacent rows. Having the indices of both rows and columns start at zero makes it possible to state that the binomial coefficient \tbinom{n}{k} appears in the nth row and kth column of Pascal's triangle. A simple construction of the triangle proceeds in the following manner: In row 0, the topmost row, the entry is \tbinom{0}{0} = 1 (the entry is in the zeroth row and zeroth column). Then, to construct the elements of the following rows, add the number above and to the left with the number above and to the right of a given position to find the new value to place in that position. If either the number to the right or left is not present, substitute a zero in its place. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1), whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.
This construction is related to the binomial coefficients by Pascal's rule, which says that if
(x+y)^n=\sum_{k=0}^n{n \choose k}x^{n-k}y^{k}
then
 {n \choose k} = {n-1 \choose k-1} + {n-1 \choose k}
for any non-negative integer n and any integer k between 0 and n.
Pascal's triangle has higher dimensional generalizations. The three-dimensional version is called Pascal's pyramid or Pascal's tetrahedron, while the general versions are called Pascal's simplices.

Program Using JAVA:

public class pascal{
public static void main(String args[]){
int row=10;

for(int i=0;i<row;i++){
int no=1;
System.out.format("%"+(row-i)*2+"s","");
for(int j=0;j<=i;j++){
System.out.format("%4d",no);
no=no*(i-j)/(j+1);
}
System.out.println();
}
}
}

Download Program File: Click Here

Output:


Tower Of Hanoi with Recursion Using JAVA

Tower Of Hanoi with Recursion Using JAVA


The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower, and sometimes pluralized) is a mathematical game or puzzle.It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
  1. Only one disk can be moved at a time.
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
  3. No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n is the number of disks.

More Details About Tower Of Hanoi: Click Here

Program Using JAVA:

import java.util.*;

public class HanoiTower{

public static void doTower(int disk,char from,char mid,char end){
if(disk==1){
System.out.println("Disk 1 from "+from+" To "+end);
}else{
doTower(disk-1,from,end,mid);
System.out.println("Disk "+disk+" From "+from+" To "+end);
doTower(disk-1,mid,from,end);
}
}
public static void main(String args[]){
Scanner input=new Scanner(System.in);
int disk;
System.out.print("Enter No Of Disk:");
disk=Integer.parseInt(input.nextLine());
if(disk<=0){
System.out.println("Invalid Disk SIze");
}else{
doTower(disk,'A','B','C');
}
}
}

Download Program File: Click Here

output:




Tower of Hanoi without recursion using java

Tower of Hanoi without recursion using java



Program Using JAVA:

import java.util.*;

public class TowerOfHanoi{
public static int makeTower(Stack A,Stack B){
int a,b;
try{
a=Integer.parseInt(A.peek().toString());
}catch(Exception e){
a=0;
}
try{
b=Integer.parseInt(B.peek().toString());
}catch(Exception e){
b=0;
}
if(a==b) return 0;
if(a==0){
A.push(B.pop());
return 2;
}
else if(b==0){
B.push(A.pop());
return 1;
}
if(a>b){
A.push(B.pop());
return 2;
}
else if(a<b){
B.push(A.pop());
return 1;
}
return -1;
}
public static void main(String args[]){
Scanner input = new Scanner(System.in);
try{
Stack source=new Stack();
Stack Auxilary=new Stack();
Stack Destination=new Stack();
int stepno=0;
int status=0;

System.out.print("Enter no Of Disk:");
int disk=Integer.parseInt(input.nextLine());

if(disk<=0){
System.out.println("Invalid Disk Size");
System.exit(1);
}
for(int i=disk;i>0;i--)
source.push(i);
int m=disk%2;
do{
if(m==1){
status=makeTower(source,Destination);
if(status==1){
System.out.println((++stepno)+"Source --> Destination");
}
else if(status==2){
System.out.println((++stepno)+"Destination --> Source");
}
status=makeTower(source,Auxilary);
if(status==1){
System.out.println((++stepno)+"Source -->Auxilary");
}
else if(status==2){
System.out.println((++stepno)+"Auxilary --> Source");
}else
break;
}else{
status=makeTower(source,Auxilary);
if(status==1){
System.out.println((++stepno)+"Source -->Auxilary");
}
else if(status==2){
System.out.println((++stepno)+"Auxilary --> Source");
}
status=makeTower(source,Destination);
if(status==1){
System.out.println((++stepno)+"Source --> Destination");
}
else if(status==2){
System.out.println((++stepno)+"Destination --> Source");
}
}
status=makeTower(Auxilary,Destination);
if(status==1){
System.out.println((++stepno)+"Auxilary --> Destination");
}
else if(status==2){
System.out.println((++stepno)+"Destination --> Auxilary");
}

}while(Destination.size()!=disk);
System.out.println("\nfinish");
}catch(Exception ex){}
}
}

download program file: click here

output:


Sunday 17 May 2015

Fibonacci number using C language

Fibonacci number using C language

The Fibonacci sequence is named after Italian mathematician Fibonacci His 1202 book Liber Abaci introduced the sequence to Western European mathematics, although the sequence had been described earlier in Indian mathematics. By modern convention, the sequence begins either with F0 = 0 or with F1 = 1. The Liber Abaci began the sequence with F1 = 1.
Fibonacci numbers are closely related to Lucas numbers in that they are a complementary pair of Lucas sequences. They are intimately connected with the golden ratio; for example, the closest rational approximations to the ratio are 2/1, 3/2, 5/3, 8/5, ... . Applications include computer algorithms such as the Fibonacci search technique and the Fibonacci heap data structure, and graphs called Fibonacci cubes used for interconnecting parallel and distributed systems. They also appear in biological settings such as branching in trees, phyllotaxis (the arrangement of leaves on a stem), the fruit sprouts of a pineapple the flowering of an artichoke, an uncurling fern and the arrangement of a pine cone's bracts.

for more detail: click here 


program:
#include <stdio.h>

// Fibonacci Series
/*
   Fibo(0)=1
   Fibo(1)=1
   Fibo(n) = Fibo(n-1)+Fibo(n-2)
*/

int Fibo(int no)
{
  int no1, no2;

  if (no==0)
    return 1;
  else
  if (no==1)
    return 1;
  else
  {
    no1 = Fibo(no-1);
    no2 = Fibo(no-2);
    return (no1+no2);
  }
}

void main()
{
  int i, number;

  clrscr();

  printf("Enter an integer : ");
    scanf("%d", &number);
  fflush(stdin);

  printf("Fibo(%d) = %d\n\n", number, Fibo(number));

  for (i=0; i<=number; i++)
    printf("Fibo(%d)\t= %10d\n", i, Fibo(i));

  getch();
}


Saturday 16 May 2015

greatest common divisor (gcd) using C language

greatest common divisor (gcd) using C Language

the Greatest Common Divisor (GCD) of two whole numbers, also called the Greatest Common Factor (GCF) and the Highest Common Factor (HCF), is the largest whole number that's a divisor (factor) of both of them. For instance, the largest number that divides into both 20 and 16 is 4. (Both 16 and 20 have larger factors, but no larger common factors -- for instance, 8 is a factor of 16, but it's not a factor of 20.) In grade school, most people are taught a "guess-and-check" method of finding the GCD. Instead, there is a simple and systematic way of doing this that always leads to the correct answer. The method is called "Euclid's algorithm." If you want to know how to truly find the Greatest Common Divisor of two integers.



for more Details: click here

program:
# include <stdio.h>

/* Definition of the Greatest Comman Divisor Function */
int GCD(int n, int m)
{
  if ((n>=m) && ((n%m)==0))
    return(m);
  else
    return GCD(m,(n%m));
}

/* main function */
void main()
{
  int no1, no2;
  int GCDresult;

  clrscr();
  printf("Program for GCD caluclation\n\n");

  printf("Input 2 integer numbers: ");
    scanf("%d%d", &no1,&no2);
  printf("\n\n");

  GCDresult = GCD(no1, no2);
  printf("no1 = %d\n", no1);
  printf("no2 = %d\n\n", no2);

  printf("Greatest Comman Diviser = %d\n", GCDresult);
  getch();
}

tower of hanoi using c language

Tower of Hanoi using c language

Tower of Hanoi

The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883. He was inspired by a legend that tells of a Hindu temple where the puzzle was presented to young priests. At the beginning of time, the priests were given three poles and a stack of 64 gold disks, each disk a little smaller than the one beneath it. Their assignment was to transfer all 64 disks from one of the three poles to another, with two important constraints. They could only move one disk at a time, and they could never place a larger disk on top of a smaller one. The priests worked very efficiently, day and night, moving one disk every second. When they finished their work, the legend said, the temple would crumble into dust and the world would vanish.
Although the legend is interesting, you need not worry about the world ending any time soon. The number of moves required to correctly move a tower of 64 disks is 2641=18,446,744,073,709,551,615. At a rate of one move per second, that is 584,942,417,355 years! Clearly there is more to this puzzle than meets the eye.
Figure 1 shows an example of a configuration of disks in the middle of a move from the first peg to the third. Notice that, as the rules specify, the disks on each peg are stacked so that smaller disks are always on top of the larger disks. If you have not tried to solve this puzzle before, you should try it now. You do not need fancy disks and poles–a pile of books or pieces of paper will work.
image
Figure 1: An Example Arrangement of Disks for the Tower of Hanoi
How do we go about solving this problem recursively? How would you go about solving this problem at all? What is our base case? Let’s think about this problem from the bottom up. Suppose you have a tower of five disks, originally on peg one. If you already knew how to move a tower of four disks to peg two, you could then easily move the bottom disk to peg three, and then move the tower of four from peg two to peg three. But what if you do not know how to move a tower of height four? Suppose that you knew how to move a tower of height three to peg three; then it would be easy to move the fourth disk to peg two and move the three from peg three on top of it. But what if you do not know how to move a tower of three? How about moving a tower of two disks to peg two and then moving the third disk to peg three, and then moving the tower of height two on top of it? But what if you still do not know how to do this? Surely you would agree that moving a single disk to peg three is easy enough, trivial you might even say. This sounds like a base case in the making.
Here is a high-level outline of how to move a tower from the starting pole, to the goal pole, using an intermediate pole:
  1. Move a tower of height-1 to an intermediate pole, using the final pole.
  2. Move the remaining disk to the final pole.
  3. Move the tower of height-1 from the intermediate pole to the final pole using the original pole.
As long as we always obey the rule that the larger disks remain on the bottom of the stack, we can use the three steps above recursively, treating any larger disks as though they were not even there. The only thing missing from the outline above is the identification of a base case. The simplest Tower of Hanoi problem is a tower of one disk. In this case, we need move only a single disk to its final destination. A tower of one disk will be our base case. In addition, the steps outlined above move us toward the base case by reducing the height of the tower in steps 1 and 3


 for more detail: click hera
program:
 # include <stdio.h>

/* Definition of the Tower Of Hanoi generator function */
void TowerOfHanoi(char peg1, char peg2, char  peg3, int no)
{
  if (no==1)
    printf("   Move Disk from %c ==> %c\n", peg1, peg3);
  else
  {
    TowerOfHanoi(peg1, peg3, peg2, no-1);
    TowerOfHanoi(peg1, peg2, peg3, 1);
    TowerOfHanoi(peg2, peg1, peg3, no-1);
  }
}

/* main function */
int main()
{
  int noDisks;

//  clrscr();
  printf("Solving Tower of Hanoi puzzle\n\n");
  printf("Input the number of disc: ");
    scanf("%d", &noDisks);
  if (noDisks<=0)
    printf("No of disks = %d is an INVALID value\n", noDisks);
  else
  {
    printf("Moving %d disks from peg A to peg C\n\n");
    TowerOfHanoi('A', 'B', 'C', noDisks);
  }

//  getch();
}


Saturday 21 March 2015

Job Sequencing Problem In Operations Research with Algorithm and Example using c++

Job Sequencing Problem In Operations Research using c++

there are mainly for types of jobs sequencing problem 
  1.  N jobs 2 machines
  2. N job 3 machines
  3. N job M machines
  4. 2 job M machines
the first 3 will solve by Johnson's method which is given here the and 4 th one is solve by graphical method 

the give program for 
  • N jobs 2 machine
  • N jobs 3 machine
  • N jobs M machine

Example


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#define MAX 50
class sequencingProblem{
int data[MAX][MAX];
int no_of_machines;
int no_of_jobs;
int sequence[MAX];
int sequenceMark[MAX];
int idleTime[MAX];
int inTime[MAX][MAX];
int outTime[MAX][MAX];
int fakeMachine[MAX][MAX];
int totalTime;
public:
sequencingProblem(){
int i,j;
for(i=0;i<MAX;i++){
sequence[i]=0;
sequenceMark[i]=0;
idleTime[i]=0;
for(j=0;j<MAX;j++){
data[i][j]=0;
inTime[i][j]=0;
outTime[i][j]=0;
fakeMachine[i][j]=0;
}
}
totalTime=no_of_jobs=no_of_machines=0;
}
void setNoOfMachine(int no){no_of_machines=no;}
void setNoOfJob(int no){no_of_jobs=no;}
void makeSequence(int [][MAX]);
void setData();
void displaySequence();
void display();
int getMinValue(int [],int);
int getPosition(int [],int,int,int);
int countValue(int [],int,int);
void calculateInOutTime();
void countIdleTime();
void countTotalTime();
void makeFakeMachine();
};
void sequencingProblem::makeFakeMachine(){
int i,j;
for(i=0;i<no_of_jobs;i++){
for(j=0;j<no_of_machines-1;j++)
fakeMachine[0][i]+=data[j][i];
}
for(i=0;i<no_of_jobs;i++){
for(j=1;j<no_of_machines;j++)
fakeMachine[1][i]+=data[j][i];
}
}
void sequencingProblem::countTotalTime(){
totalTime=outTime[no_of_machines-1][no_of_jobs-1];
}
void sequencingProblem::countIdleTime(){
int i,j;
countTotalTime();
for(i=0;i<no_of_machines;i++){
idleTime[i]=inTime[i][0];
for(j=1;j<no_of_jobs;j++)
idleTime[i]+=inTime[i][j]-outTime[i][j-1];
idleTime[i]+=totalTime-outTime[i][no_of_jobs-1];
}
}
void sequencingProblem::calculateInOutTime(){
int i,j;
for(i=0;i<no_of_machines;i++){
if(i==0){
inTime[i][0]=0;
outTime[i][0]=data[i][sequence[0]-1];
for(j=1;j<no_of_jobs;j++){
inTime[i][j]=outTime[i][j-1];
outTime[i][j]=data[i][sequence[j]-1]+inTime[i][j];
}
}else{
for(j=0;j<no_of_jobs;j++){
if(j==0){
inTime[i][j]=outTime[i-1][j];
outTime[i][j]=data[i][sequence[j]-1]+inTime[i][j];
}else{
if(outTime[i-1][j]<outTime[i][j-1])
inTime[i][j]=outTime[i][j-1];
else
inTime[i][j]=outTime[i-1][j];
outTime[i][j]=inTime[i][j]+data[i][sequence[j]-1];
}
}
}
}
}
int sequencingProblem::getMinValue(int array[],int no){
int min=9999;
for(int i=0;i<no;i++)
if(sequenceMark[i]!=1)
if(min>array[i])
min=array[i];
return min;
}
int sequencingProblem::getPosition(int array[],int start,int end,int value){
for(int i=start;i<end;i++)
if(sequenceMark[i]!=1)
if(value==array[i])
return i;
return -1;
}
int sequencingProblem::countValue(int array[],int no,int value){
int count=0;
for(int i=0;i<no;i++)
if(sequenceMark[i]!=1)
if(value==array[i])
count++;
return count;
}
void sequencingProblem::makeSequence(int array[][MAX]){
int i,j,minOfM1,minOfM2;
int pos=-1,temp_pos=-1,totalMin,totalMin1,min,max;
int start=0,end=no_of_jobs-1;
for(i=0;i<no_of_jobs;i++){
minOfM1=getMinValue(array[0],no_of_jobs);
minOfM2=getMinValue(array[1],no_of_jobs);
temp_pos=pos=-1;
if(minOfM1<minOfM2){
totalMin=countValue(array[0],no_of_jobs,minOfM1);
for(j=0;j<totalMin;j++){
min=9999;
if(totalMin>1){
temp_pos=getPosition(array[0],temp_pos+1,no_of_jobs,minOfM1);
if(array[1][temp_pos]<min){
min=array[1][temp_pos];
pos=temp_pos;
}
}
else
pos=getPosition(array[0],pos+1,no_of_jobs,minOfM1);
sequence[start++]=pos+1;
sequenceMark[pos]=1;
}
}else if(minOfM1>minOfM2){
totalMin=countValue(array[1],no_of_jobs,minOfM2);
for(j=0;j<totalMin;j++){
if(totalMin>1){
max=0;
for(int k=0;k<totalMin;k++){
temp_pos=getPosition(array[1],temp_pos+1,no_of_jobs,minOfM2);
if(array[0][temp_pos]>max){
max=array[0][temp_pos];
pos=temp_pos;
}
}
}
else
pos=getPosition(array[1],pos+1,no_of_jobs,minOfM2);
sequence[end--]=pos+1;
sequenceMark[pos]=1;
}
}else if(minOfM1==minOfM2 && minOfM1!=9999){
totalMin=countValue(array[0],no_of_jobs,minOfM1);
totalMin1=countValue(array[1],no_of_jobs,minOfM2);
for(j=0;j<totalMin;j++){
pos=getPosition(array[0],pos+1,no_of_jobs,minOfM1);
sequence[start++]=pos+1;
sequenceMark[pos]=1;
}
pos=-1;
for(j=0;j<totalMin1;j++){
pos=getPosition(array[1],pos+1,no_of_jobs,minOfM2);
if(sequenceMark[pos]==0 && pos!=-1){
sequence[end--]=pos+1;
sequenceMark[pos]=1;
}
}
}
}
}
void sequencingProblem::displaySequence(){
cout<<"sequence: ";
for(int i=0;i<no_of_jobs;i++)
cout<<char(sequence[i]+64)<<"\t";
cout<<endl;
}
void sequencingProblem::setData(){
int i,j;
cout<<"Enter Data:\n";
for(i=0;i<no_of_machines;i++){
cout<<"For Machine "<<i+1<<" :";
for(j=0;j<no_of_jobs;j++)
cin>>data[i][j];
}
}
int getMax(int data[][MAX],int row,int col){
int i,j;
int max=0;
for(i=1;i<row-1;i++)
for(j=0;j<col;j++)
if(max<data[i][j])
max=data[i][j];
return max;
}
void sequencingProblem::display(){
int i,j;
cout<<"Given Data:\n";
for(i=0;i<no_of_machines;i++){
cout<<"Machine "<<i+1<<" :";
for(j=0;j<no_of_jobs;j++)
cout<<data[i][j]<<"\t";
cout<<endl;
}
if(no_of_machines>2){
int minM1=getMinValue(data[0],no_of_jobs);
int maxMM_1=getMax(data,no_of_machines,no_of_jobs);
int minMM=getMinValue(data[no_of_machines-1],no_of_jobs);
if(minM1>=maxMM_1 || minMM>=maxMM_1){
makeFakeMachine();
makeSequence(fakeMachine);
}
else{
cout<<"\n\nNOTE: Solution Can Not possible Sorry...!!!";
getch();
exit(0);
}
}else
makeSequence(data);
displaySequence();
calculateInOutTime();
cout<<"\nsequence\t";
for(i=0;i<no_of_machines;i++)
cout<<"Machine "<<i+1<<" Time\t";
cout<<endl<<"\t";
for(i=0;i<no_of_machines;i++)
cout<<"\tIn\tout";
cout<<endl;
for(i=0;i<no_of_jobs;i++){
cout<<char(sequence[i]+64)<<"\t";
for(j=0;j<no_of_machines;j++)
cout<<"\t"<<inTime[j][i]<<"\t"<<outTime[j][i];
cout<<endl;
}
countIdleTime();
cout<<"\n\n\tTotal elapsed time :"<<totalTime;
for(i=0;i<no_of_machines;i++)
cout<<"\n\tTotal Idle Time for Machine "<<i+1<<" :"<<idleTime[i];
}
void main(){
clrscr();
int machine,job;
sequencingProblem sp1;
cout<<"enter no of machine:";
cin>>machine;
cout<<"enter no of job:";
cin>>job;
sp1.setNoOfMachine(machine);
sp1.setNoOfJob(job);
sp1.setData();
clrscr();
sp1.display();
getch();
}

output



Wednesday 4 February 2015

Project Evolution and Review Technique(PERT) with Algorithm and program in c++

Project Evolution and Review Technique(PERT) with Algorithm and program in c++

For More Detail Information and Algorithm: Click Here

from the above image of pert diagram you have to make adjacency matrix for its optimistic time, most likely Time and pessimistic Time and provide that data as a input so the give program will generate output according to below screen shots 

Program Code in C++


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#define MAX 50

class pert{
    int optimistic[MAX][MAX];
    int mostLikely[MAX][MAX];
    int pessimistic[MAX][MAX];
    int estimatedTime[MAX][MAX];
    int variance[MAX][MAX];
    int criticalPath[MAX];
    int no_of_activity;
    int no_of_critical_activity;
    int totalDuration;
    public:
        pert(){
            int i,j;
            for(i=0;i<MAX;i++){
                criticalPath[i]=0;
                for(j=0;j<MAX;j++){
                    optimistic[i][j]=0;
                    mostLikely[i][j]=0;
                    pessimistic[i][j]=0;
                    estimatedTime[i][j]=0;
                    variance[i][j]=0;
                }
                totalDuration=no_of_critical_activity=no_of_activity=0;
            }
        }
        void setNoOfActivity(int no){no_of_activity=no;}
        void getData();
        void display();
        void findEstimatedTime();
        void findVariance();
        void findCriticalPath();
};
int getMaxVal(int array[],int no){
    int i,max=0;
    for(i=0;i<no;i++){
        if(max<array[i])
            max=array[i];
    }
    return max;
}
int getPosition(int array[],int no,int value){
    for(int i=0;i<no;i++)
        if(array[i]==value)
            return i+1;
    return 0;
}
void pert::findCriticalPath(){
    int i=0,maxVal,pos,k=0;
    criticalPath[k]=1;
    do{
        maxVal=getMaxVal(optimistic[i],no_of_activity);
        if(maxVal!=0){
            pos=getPosition(optimistic[i],no_of_activity,maxVal);
            criticalPath[++k]=pos;
            i=pos-1;
            totalDuration+=maxVal;
            no_of_critical_activity++;
        }
    }while(maxVal!=0);
}
void pert::findVariance(){
    int i,j,sum=0;
    for(i=0;i<no_of_activity;i++){
        for(j=0;j<no_of_activity;j++)
            if(optimistic[i][j]!=0){
                sum=(pessimistic[i][j]-optimistic[i][j])/6;
                variance[i][j]=sum*sum;
            }
    }
}
void pert::findEstimatedTime(){
    int i,j;
    for(i=0;i<no_of_activity;i++){
        for(j=0;j<no_of_activity;j++)
            if(optimistic[i][j]!=0){
                estimatedTime[i][j]=(optimistic[i][j]+(4*mostLikely[i][j])+pessimistic[i][j])/6;
            }
    }
}
void pert::getData(){
    int i,j;
    cout<<"enter adjecency matrix of optimistic data:\n";
    for(i=0;i<no_of_activity;i++){
        cout<<"Row "<<(i+1)<<" : ";
        for(j=0;j<no_of_activity;j++)
            cin>>optimistic[i][j];
    }
    clrscr();
    cout<<"enter adjecency matrix of MostLikely data:\n";
    for(i=0;i<no_of_activity;i++){
        cout<<"Row "<<(i+1)<<" : ";
        for(j=0;j<no_of_activity;j++)
            cin>>mostLikely[i][j];
    }
    clrscr();
    cout<<"enter adjecency matrix of pessimistic data:\n";
    for(i=0;i<no_of_activity;i++){
        cout<<"Row "<<(i+1)<<" : ";
        for(j=0;j<no_of_activity;j++)
            cin>>pessimistic[i][j];
    }
}
void pert::display(){
    int i,j;
    cout<<"enterd Data:\n";
    cout<<"\tJob\toptimistic\tmost Likely\tpessimistic\n";
    for(i=0;i<no_of_activity;i++){
        for(j=0;j<no_of_activity;j++)
            if(optimistic[i][j]!=0){
                cout<<"\t"<<(i+1)<<" - "<<(j+1)<<"\t\t"<<optimistic[i][j]<<"\t\t"<<mostLikely[i][j]<<"\t\t"<<pessimistic[i][j]<<endl;
            }
    }
    cout<<"\n\n\n\t\tpress any key.......";
    getch();
    clrscr();
    cout<<"\tJob\tTO\tTM\tTP\tTE\tVariance\n";
    for(i=0;i<no_of_activity;i++){
        for(j=0;j<no_of_activity;j++)
            if(optimistic[i][j]!=0){
                cout<<"\t"<<(i+1)<<" - "<<(j+1)<<"\t"<<optimistic[i][j]<<"\t"<<mostLikely[i][j]<<"\t"<<pessimistic[i][j]<<"\t"<<estimatedTime[i][j]<<"\t"<<variance[i][j]<<endl;
            }
    }
    cout<<"\ncritical path= ";
    for(i=0;i<no_of_critical_activity;i++)
        cout<<criticalPath[i]<<" - ";
    cout<<no_of_activity;
    cout<<"\nTotal Duration : "<<totalDuration<<" Days";
}
void main(){
    clrscr();
    pert p1;
    int no;
    cout<<"enter no of Activity :";
    cin>>no;
    p1.setNoOfActivity(no);
    p1.getData();
    clrscr();
    p1.findEstimatedTime();
    p1.findVariance();
    p1.findCriticalPath();
    p1.display();
    getch();
}

output:

Screen 1:

Screen 2:
Screen 3:
Screen 4:
Screen 5: