Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Program: How do I read text files and and put it into the array wt[] and val[]? Text Files: knapsack1_values.txt: 24,13,23,15,16 knapsack1_weights.txt: 12,7,11,8,9 #

C Program: How do I read text files and and put it into the array wt[] and val[]?

Text Files:

knapsack1_values.txt: 24,13,23,15,16

knapsack1_weights.txt: 12,7,11,8,9

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

# 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 val[], int n) { int i, W; int K[n+1][W+1]; int totalWeight=0; // Build table K[][] in bottom up manner for (i = 0; i && res > 0; i--) { Il either the result comes from the top // (K[i-1][w]) or from (val[i-1] + K[i-1] // [w-wt[i-1]]) as in knapsack table. If // it comes from the latter one/ it means // the item is included. if (res == K[i - 1][w]) continue; else { // This item is included. //printf("%d", wt[i-1]); totalWeight=totalWeight+wt[i-1]; // Since this weight is included its // value is deducted res = res - vali - 1]; W = W - wt[i - 1]; struct knapsack knap; knap.value=K[n][W]; knap.weight=totalweight; return knap; }// end struct knapsackExhaustive int main(void) { struct knapsack aSack; int i; time tt; int val[5]; int wt[5]; //Intializes random number generator srand((unsigned) time(&t)); printf("Knapsack Brute Force "); // Print 5 random values from 3 to 15 printf("Five Random Values: "); FILE *fileValues; fileValues = fopen("knapsack1_values.txt", "r"); if (fileValues) { while ((i = getc(fileValues)) != EOF) putchar(i); fclose(fileValues); int i; printf("Five Random Weights: "); FILE *fileWeights; fileWeights = fopen("knapsack1_weights.txt", "r"); if (fileValues) { while ((j = getc(fileValues)) != EOF) putchar(j); fclose(fileWeights); printf("Knapsack Capacity:"); int W; FILE *fileCapacity; fileCapacity = fopen("knapsack1_capacity.txt", "r"); if (fileCapacity) { while ((W = getc(fileCapacity)) != EOF) putchar(W); fclose(fileCapacity); int n = sizeof(val)/sizeof(val[@]); aSack = knapsackExhaustive(W, wt, val, n); // Total value and weight for the items chosen to put into the knapsack printf("Total Value: %d\t ", aSack.value); printf("Total Weight:%d\t ", aSack.weight); return 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL 10 High Performance Expert Techniques For Query Optimization High Availability And Efficient Database Maintenance

Authors: Ibrar Ahmed ,Gregory Smith ,Enrico Pirozzi

3rd Edition

1788474481, 978-1788474481

More Books

Students also viewed these Databases questions

Question

d. Present your teams conclusions to the class.

Answered: 1 week ago

Question

explain the concept of strategy formulation

Answered: 1 week ago