Tuesday, May 19, 2009

C Sharp Queries

Anyone Who has Any Problem related to code(Syntax) in C Sharp or any other Language can leave their query and it will be solved as early as possible.

Knapsack Problem 0-1

void PRINT_01(int**,int*,int*,int,int);

void main()
{
clrscr();
int i,w;
cout<<"Enter No. Of Items : ";
int n;
cin>>n;
int *p,*q;
p=new int[n+1];
q=new int[n+1];
for(i=1;i<=n;i++)
{
cout<<"Enter Weigth : ";
cin>>q[i];
cout<<"Enter Profit : ";
cin>>p[i];
}
cout<<"\nEnter Maximum Weight : ";
int W;
cin>>W;
int **B;
B=new int*[W];
for(w=0;w<=W;w++);
B[w]=new int[n+1];

for(w=0;w<=W;w++)
B[0][w]=0;
for(i=0;i<=n;i++)
B[i][0]=0;

for(i=1;i<=n;i++)
{
for(w=1;w<=W;w++)
{
if(q[i]<=w) // item i can be part of the solution
if((p[i] + B[i-1][w-q[i]])>B[i-1][w])
B[i][w]=(p[i] + B[i-1][w-q[i]]);
else
B[i][w]=B[i-1][w];
else
B[i][w]=B[i-1][w]; // wi > w
}
}
for(w=0;w<=W;w++)
{
for(i=0;i<=n;i++)
cout< cout<<"\n";
}
cout<<"\n\n";
cout<<"Item No. Selected\tWeigth\tProfit\n\n";
PRINT_01(B,q,p,n,W);
cout<<"Total Profit : "< getch();
}

void PRINT_01(int **B,int *Q,int *P,int i,int w)
{
if(i==0||w==0)
return;
if(B[i-1][w]!=B[i][w])
{
PRINT_01(B,Q,P,i-1,w-Q[i]);
cout<<"\t"< }
else
PRINT_01(B,Q,P,i-1,w);
}

Monday, April 13, 2009

Radix Sort

void radixsort(int numbers[],int n)
{
//for base 10
int temp; int bucket[10][20], buck_count[10], b[10];
int i,j,k,r,no_of_passes=0,divisor=1,largest,pass_no;
largest=numbers[0];
for(i=1;i{
if(numbers[i] > largest)
largest=numbers[i];
}
while(largest > 0) //Find number of digits in largest number
{
no_of_passes++;
largest /= 10;
}
for(pass_no=0; pass_no < no_of_passes; pass_no++)
{
for(k=0; k<10; k++) buck_count[k]=0; //Initialize bucket count
for(i=0;i less than n;i++)
{
r=(numbers[i]/divisor) % 10;
bucket[r][buck_count[r]++]=numbers[i];
}
i=0; //collect elements from bucket
for(k=0; k<10; k++)
for(j=0; j less than buck_count[k]; j++)
numbers[i++] = bucket[k][j];
divisor *= 10;
}
}


for any query just leave a message

Monday, February 9, 2009

Bridge Problem.... in C++

Problem..
A city is built on two islands connected by a narrow bridge. There are different types of vehicles (ambulance, VIP vehicles, cars, trucks, others) driving throughout the city and occasionally crossing the bridge. The bridge is only wide enough for traffic in one direction at a time and at most 6 vehicles can be on the bridge at a time from one end to another end. Because the bridge is narrow the vehicle must also travel slowly while crossing the bridge (i.e. it should take some time). There is no traffic light.

When a vehicle decides to cross the bridge, one of three situations can occur:
The bridge is free, in which case the vehicle may cross.
The bridge is occupied, and the traffic on the bridge is travelling in the right direction, so the vehicle is allowed to cross.
The bridge is occupied, because traffic is in the opposite direction. Now the vehicle must either wait until the bridge is free, or come back later and try again.

Make sure that you have enough vehicles, and that they decide to cross the bridge often enough that all three of the traffic situations can occur.
The priorities of different vehicles are as follows:
The ambulance has the highest priority for crossing the bridge.
The VIP vehicles have the second highest priority for crossing the bridge.
The truck has the lowest priority. The priority of truck will become normal between 11 PM – 8 AM. But if the bridge is free it can cross the bridge, incase of emergency.

The entries of all vehicles on each side have to be saved in the file. The following calculations have to be done at the end of the day:
Total vehicles crossed per day.
Total vehicles crossed categories wise each side.

Solution:..
http://rapidshare.com/files/196034971/LAB3.zip.html

Post your doubt or problem in forum.

Sunday, February 8, 2009

Sierpinski Triangle Fractal

/*copy this file and place it in bin folder in tc.. and
egavga file must be present in C:\tc\bgi path*/
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "graphics.h"
int ran=0;
void linebreak(int x1, int y1, int x2, int y2, int x3 ,int y3,int n)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
switch(ran%3)
{
case 0: setfillstyle(1,1);
break;
case 1: setfillstyle(1,2);
break;
case 2:setfillstyle(1,4);
break;
}
floodfill((x1+x2+x3)/3,(y1+y2+y3)/3,15);
delay(15);
while(n>0)
{
n--;
linebreak(x1,y1,(x1+x2)/2,(y1+y2)/2,(x3+x1)/2,(y1+y3)/2,n);
linebreak(x2,y2,(x2+x3)/2,(y2+y3)/2,(x2+x1)/2,(y2+y1)/2,n);
linebreak(x3,y3,(x1+x3)/2,(y1+y3)/2,(x2+x3)/2,(y2+y3)/2,n);
}
ran++;
}
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int x1,x2,y1, y2,e,f,i,j,k,l,n;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
printf("enter the no of levels");
scanf("%d",&n);
linebreak(320,30,580,450,60,450,n-1);
getch();
closegraph();
return 0;
}