Our team came with the idea to solve the problem statement of students.So we solve a problem called as Knapsack Problem(factorial) which is the part of Data structure.In this blog we create a program to resolve the problem of students with the help of recursive functions.We use a Random function in our program so that we don't need to give input through keyboard that is compiler itself insert the inputs from buffer memory.So here you are guys.....Hope you like it..
If you like the program then please like our channel by clicking the link below and if you had any problem then you can ask our team anytime.

Gmail
Facebook_Page   
youtube

<PROGRAM/CODE> 

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void input(float w[],float v[],int n)
{
int i;
randomize();
for(i=0;i<n;i++)
{
w[i]=rand()%100;
v[i]=rand()%100;
}
cout<<endl;
}
void display(float w[],float v[],float r[],int n)
{
int i;
cout<<"\nWeight\tVolume\tRatio\n";
for(i=0;i<n;i++)
{
cout<<w[i]<<"\t"<<v[i]<<"\t"<<r[i]<<endl;
}
}
void cal(float w[],float v[],float r[],int n,float c)
{
int i,j;
float x[10],k;
for(i=0;i<n;i++)
{
x[i]=0.0;
}
//processing by bubble sort
for(j=0;j<n;j++)
{
for(i=0;i<n-1;i++)
{
if(r[i]<r[i+1])
{
k=r[i];        //swapping of ratio
r[i]=r[i+1];
r[i+1]=k;
k=w[i];
w[i]=w[i+1];     //swapping of weight
w[i+1]=k;
k=v[i];
v[i]=v[i+1];      //swapping of volume
v[i+1]=k;
}
}
}
for(i=0;i<n;i++)
{
if(w[i]>c)
{
break;
}
else
{
x[i]=1.0;
c=c-w[i];
}
}
if(i<=n)
{
x[i]=c/w[i];
}
float sum=0.0;
for(i=0;i<n;i++)
{
x[i]=x[i]*v[i];
sum=sum+x[i];
}
cout<<"\nProfit is : "<<sum;

}
void main()
{
clrscr();
int i,j,n;//c=capacity
float x[10],c,k,w[10],v[10],r[10]; //r=ratio,w=weight,v=volume
cout<<"Enter the maximum value \n";
cin>>n;
input(w,v,n);
cout<<"\nEnter the capacity :\n";
cin>>c;
for(i=0;i<n;i++)
{
r[i]=v[i]/w[i];
}
display(w,v,r,n);
cal(w,v,r,n,c);

getch();
}

Comments

Popular posts from this blog

Network File System

Fibonacci Series with Python