Question

In: Computer Science

C Program: (Knapsack Exhaustive): How do I add the 5 random generated values into the val...

C Program: (Knapsack Exhaustive): How do I add the 5 random generated values into the val [] arrays and how to add the total maximum weights ?

   // returns maximum of two integers
   int max(int a, int b) { return (a > b)? a : b; }
  
   // Returns the maximum value that can be put in a knapsack of capacity W
   int knapSackExhaustive(int W, int wt[], int val[], int n){
  

if (n == 0 || W == 0)
return 0;
  
// If weight of the nth item is more than Knapsack capacity W, then
// this item cannot be included
if (wt[n-1] > W)
return knapSackExhaustive(W, wt, val, n-1);

else return max( val[n-1] + knapSackExhaustive(W-wt[n-1], wt, val, n-1),
knapSackExhaustive(W, wt, val, n-1)
);

}
  

int main() {
  
  
int i;
time_t t;


//Intializes random number generator
srand((unsigned) time(&t));

// Print 5 random values from 3 to 15
int q;
printf("Five Random Values:\n");
for( i = 0 ; i < 5 ; i++ ) {
int q=printf(" %d\n",rand() % 15+2);
}
int val[]= {i};

   int j;
   //Print 5 random weights from 1 and 10000
   printf("Five Random Weights:\n");
for( j = 0 ; j < 5 ; j++ ) {
printf(" %d\n", rand() % 10000);
}

int wt[]={j};

  
int W = 10000;
int n = sizeof(val)/sizeof(val[0]);
n = sizeof(wt)/sizeof(wt[0]);
  
printf("Total Value: %d\t\n", knapSackExhaustive(W, wt, val, n));

printf("Total Weight:\t");

return 0;
}

Solutions

Expert Solution

Program:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// returns maximum of two integers

int max(int a, int b) { return (a > b)? a : b; }

// Returns the maximum value that can be put in a knapsack of capacity W

int knapSackExhaustive(int W, int wt[], int val[], int n){

if (n == 0 || W == 0)

return 0;

// If weight of the nth item is more than Knapsack capacity W, then

// this item cannot be included

if (wt[n-1] > W)

return knapSackExhaustive(W, wt, val, n-1);

else return max( val[n-1] + knapSackExhaustive(W-wt[n-1], wt, val, n-1),

knapSackExhaustive(W, wt, val, n-1)

);

}


int main() {

int i;

time_t t;

int val[5] ; // Intialises random number generator

srand((unsigned) time(&t));

// Print 5 random values from 3 to 15

printf("Five Random Values:\n");

for( i = 0 ; i < 5 ; i++ ) {

val[i] = rand() % 12+3; // It generate random numbers between 3 to 15 where rand() % 12 generate 0 to 12 so need to add 3

printf("%d\n",val[i]);

}

int j;

int wt[5] ;

//Print 5 random weights from 1 and 10000

printf("Five Random Weights:\n");

for( j = 0 ; j < 5 ; j++ ) {

wt[j] = rand() % 10000+1; // It generate random numbers between 1 to 100 where rand() % 1000 generate 0 to 999 so need to add 1

printf("%d\n",wt[j]);

}

int W = 10000;

int n = sizeof(val)/sizeof(val[0]);

n = sizeof(wt)/sizeof(wt[0]);

int totWt=0;

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

totWt = totWt+wt[i];


printf("Total Value: %d\t\n", knapSackExhaustive(W, wt, val, n));

printf("Total Weight: %d\t",totWt);

return 0;

}

Output:


Related Solutions

C Program: How do I write a Greedy function for 0-1 knapsack, to find total value...
C Program: How do I write a Greedy function for 0-1 knapsack, to find total value only( replace struct Knapsack) # include #include #include struct Knapsack {    int value;    int weight; };    // returns maximum of two integers    int max(int a, int b) { return (a > b)? a : b; }    // Returns the maximum value that can be put in a knapsack of capacity W    struct Knapsack knapSackExhaustive(int W, int wt[], int...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT BY THE USER AS SPECIFIED IN THEH FIRST PART OF THE QUESTION? Ask the user to enter a number and display the number, followed by ***, followed by the number squared, followed by &&&, and followed by the number cubed. Allow the user to enter as many numbers as he/she wishes. So, use a reasonable sentinel value to end the loop (for example, -999)....
Part1: Write a program in C/C++ to solve the 0/1 knapsack problem using (i) Dynamic Programming...
Part1: Write a program in C/C++ to solve the 0/1 knapsack problem using (i) Dynamic Programming based algorithm and (ii) Branch and Bound Search based algorithm Go through the related text and implement each of these algorithms using the efficient data structure. Show the results of different steps of these algorithms for an instance of the 0/1 Knapsack problem with number of items, n=4. The capacity of the knapsack, weights and profits of the items may be generated randomly with...
How do I add an output operator to a class in C++? The specific part of...
How do I add an output operator to a class in C++? The specific part of the task said to: Define a ​class t​o hold accounts. Use the ​same​ stream variable! Write getters to access the fields in the accounts when printing. Add an output operator for your class. First, Repeat the print loop using this output operator.Now​ make the output operator a friend, by adding a friend​prototype​ to your account class definition. Add a line in the output operator...
How do I add white space in the beginning of my printf statement in C? I...
How do I add white space in the beginning of my printf statement in C? I also need the white space to be dynamic by using an int value as reference for the amount of spaces. Thanks!
How do you add all the values that return by all the threads from threadpools? I...
How do you add all the values that return by all the threads from threadpools? I have created a threadpool with 1000 of threads and run the task 1000 times. However, when I try to print out the result it gives a whole bunch of values that return by all the threads, it looks like... Thread-1 xxxx Thread-2 xxxx Thread-3 xxxx Thread-4 xxxx . . . . . Is there a possible way to get a single value back instead...
How do I add additional command line arguments in C++? I am working on a programming...
How do I add additional command line arguments in C++? I am working on a programming assignment that has the user input a file into the command line and then they have the option to also add a series of other arguments to the command line. I know how to accept the text file from the command line by using: int main(int argc, char *argv[]) { /.../ } Then filename(argv[1]) would be the text file that they put into the...
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack...
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. **The arguments to the knapsack function are target weight and the array index where the remaining items start. For example if you want your knapsack to weigh exactly 20 pounds and you have five items with weights of 11,8,7,6 and 5 pounds. In this case only combination that works is 8,...
How do I run a lex program written with C on xcode?
How do I run a lex program written with C on xcode?
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT