answersLogoWhite

0

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

#include "knapheap.h"

struct knapshap *c;

float x[100];

void knapshap(struct knapshap *c,int m,int n)

{

int i;

for(i=0;i<n;i++)

{

x[i]=0.0;

}

i=0;

while(c[i].totwt<=m &&i<n)

{

x[i]=1.0;

m=m-c[i].totwt;

i++;

}

if(i<=n)

{

x[i]=(m/c[i].totwt);

m=0;

}

cout<<"\n the soln. vector=";

for(int j=0;j<n;j++)

cout<<"\n"<<x[j];

}

void main()

{

clrscr();

int p[100],w[100],n,i,m=0;

cout<<"enter the size of the vector";

cin>>n;

cout<<"enter the price of each item";

for(i=1;i<=n;i++)

cin>>p[i];

cout<<"enter the wt of each item";

for(i=1;i<=n;i++)

{

cin>>w[i];

}

cout<<"enter maxm wt of knapshap";

cin>>m;

for(i=1;i<=n;i++)

{

a[i].pcwt=p[i]/w[i];

a[i].totwt=w[i];

}

build_max_heap(a,n);

c=heap_sort(a);

cout<<"\n sorted items \n";

for(i=0;i<n;i++)

{

cout<<" pc="<<c[i].pcwt<<" wt="<<c[i].totwt<<"\n";

}

knapshap(c,m,n);

getch();

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What is the importance of stack algorithm in your program?

Stack implementations allow us to easily implement backtracking algorithms.


Write a program to implement prim's algorithm?

dfgbrgffee


How do you implement simulated annealing algorithm in unit commitment problem?

pleas send to me a sample program fro SA by Matlab


Write a program in C language to implement the apriori algorithm?

JavaScript is one program that has been written in C to implement the Apriori algorithm. There are also several other known programs available on the Internet that implement it as well.


What is the difference between an algorithm and a program, and how do they each contribute to the process of solving computational problems?

An algorithm is a step-by-step procedure for solving a problem, while a program is a set of instructions written in a specific programming language to implement the algorithm on a computer. Algorithms provide the logic and structure for solving computational problems, while programs execute the algorithm to produce the desired output. In essence, algorithms define the problem-solving approach, while programs implement that approach to find solutions.


What is the difference between a program and an algorithm, and how do they each contribute to the execution of tasks in computer science?

An algorithm is a step-by-step procedure for solving a problem, while a program is a set of instructions written in a specific programming language to implement the algorithm on a computer. Algorithms provide the logic and structure for solving a problem, while programs translate the algorithm into a format that a computer can execute. Together, algorithms and programs work to efficiently and accurately perform tasks in computer science.


CPP Program for Implementing Knuth-morris-pratt pattern matching algorithm?

what is the pure algorithm instead of cpp program?


How is a pso program in matlab..how is it created from algorithm..please help?

The PSO or Particle Swarm Optimization Program algorithm in MatLab is created by first creating a binary genetic algorithm.


Is an algorithm easy to program?

Some of them are.


How do you write an Algorithm for a C plus plus Program?

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.


Crossword puzzle program in c sharp?

To create a crossword puzzle program in C#, you can start by defining a grid data structure, typically a 2D array, to represent the puzzle. Implement methods to add words both horizontally and vertically, ensuring they fit within the grid and do not overlap incorrectly. You can also include features like user input for word placement, validation of word positions, and a display function to visualize the current state of the puzzle. For a more advanced version, consider adding a backtracking algorithm for automatic puzzle solving or generating random puzzles.


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.