#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
clrscr();
printf("\nEnter the number of elements\n");
scanf("%d",&n);
printf("Enter the profit and weights of the elements\n");
for(i=1;i<=n;i++)
{
printf("For item no %d\n",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity \n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included are\n");
printf("Sl.no\tweight\tprofit\n");
for(i=1;i<=n;i++)
if(x[i])
printf("%d\t%d\t%d\n",++count,w[i],p[i]);
printf("Total profit = %d\n",profit);
getch();
}
KNAPSACK problem using greedy method in C Programming
#include
#include
typedef struct
{
char acName[5];
int iProfit,iWeight;
}Item;
void fnDisplay(Item aiA[10],int iN)
{
int iI;
printf("\nNAME WEIGHT PROFIT\n");
for(iI=0;iI
printf("%s%8d%8d\n",
aiA[iI].acName,aiA[iI].iWeight,aiA[iI].iProfit);
}
void fnUpdate(Item aiA[10],int iN)
{
int iI,iJ;
Item t;
for(iI=0;iI
for(iJ=iI+1;iJ
if((float)(aiA[iI].iProfit)/(aiA[iI].iWeight)
<(float)(aiA[iJ].iProfit)/(aiA[iJ].iWeight))
{
t=aiA[iI];
aiA[iI]=aiA[iJ];
aiA[iJ]=t;
}
}
void fnKnapsack(int iM,Item aiA[10],float aiX[10],int iN)
{
int iI,iJ,iCur;
float iProfit=0;
for(iI=1;iI<=iN;iI++)
aiX[iI]=0;
iCur=iM;
for(iI=0;iI
{
if(aiA[iI].iWeight>iCur)
break;
else
{
aiX[iI]=1;
iCur=iCur -aiA[iI].iWeight;
iProfit=iProfit+aiA[iI].iProfit;
}
}
if(iI<=iN)
{
aiX[iI]=iCur/(float)aiA[iI].iWeight;
iProfit=iProfit+aiA[iJ].iProfit*aiX[iI];
}
printf("\nThe Profit is : %f",iProfit);
}
void main()
{
Item aiA[10];
int iN,iI,iM;
float aiX[10],fProfit;
clrscr();
printf("How many items : ");
scanf("%d",&iN);
printf("\nNAME WEIGHT PROFIT\n");
for(iI=0;iI
{
scanf("%s",&aiA[iI].acName);
scanf("%d%d",&aiA[iI].iWeight,&aiA[iI].iProfit);
}
printf("\nEnter the knapsack capacity : ");
scanf("%d",&iM);
printf("\nThe Orignal List \n");
fnDisplay(aiA,iN);
getch();
fnUpdate(aiA,iN);
printf("\nUPdate List\n");
fnDisplay(aiA,iN);
fnKnapsack(iM,aiA,aiX,iN);
printf("\nThe items insert into knapsack is : ");
printf("\nNAME WEIGHT\n");
for(iI=0;iI
if(aiX[iI]!=0)
printf("%3s %8f\n",aiA[iI].acName,aiX[iI]);
getch();
}
/*
How many items : 4
NAME WEIGHT PROFIT
a 12 3
b 4 6
c 8 5
d 7 4
Enter the knapsack capacity : 20
The Orignal List
NAME WEIGHT PROFIT
a 12 3
b 4 6
c 8 5
d 7 4
UPdate List
NAME WEIGHT PROFIT
b 4 6
c 8 5
d 7 4
a 12 3
The Profit is : 15.250000
The items insert into knapsack is :
NAME WEIGHT
b 1.000000
c 1.000000
d 1.000000
a 0.083333
*/
yes
no.
Perform encryption on the following PT using RSA and find the CT p = 3; q = 11; M = 5
Please visit http://talentsealed.blogspot.com/2009/10/to-find-smallest-among-three-using.htmlfor answer.
Yes. More generally, every algorithm (defined as a sequence of finite steps to solve a problem that can be easily understood by a human) can be converted into machine code such that the algorithm can be understood by a machine. The C programming language is just one such method of converting algorithms into working machine code.
yes
You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.
no.
reymond rillera reymond rillera
They are bosom-friends.
The C code for Prim's algorithm can be found in the following link. https://sites.google.com/site/itstudentjunction/lab-programming-solutions/data-structures-programs/program-to-find-minimal-spanning-tree-using--prims-algorithm
Not used
The program itself is the solution. All programs are a solution to a given problem; that's the entire point of writing a program, to solve a problem. The program's algorithm specifies how the problem is solved and it's the programmer's job to convert that algorithm into working code.
Perform encryption on the following PT using RSA and find the CT p = 3; q = 11; M = 5
Please visit http://talentsealed.blogspot.com/2009/10/to-find-smallest-among-three-using.htmlfor answer.
Yes. More generally, every algorithm (defined as a sequence of finite steps to solve a problem that can be easily understood by a human) can be converted into machine code such that the algorithm can be understood by a machine. The C programming language is just one such method of converting algorithms into working machine code.