Question: 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 int main(void) {
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
![int main(void) { struct knapsack aSack; int i; time_t t; int val[5]; int wt[5]; //Intializes random number generator srand((u](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2022/08/6308cd1385de1_1716308cd1332f3a.jpg)

int main(void) { } } struct Knapsack aSack; int i; time_t t; 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 j; printf("Five Random Weights: "); FILE *fileWeights; fileWeights = fopen("knapsack1_weights.txt", "r"); if (fileValues) { while ((j = getc (fileValues)) != EOF) putchar (j); fclose(fileWeights); N } 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[0]); 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
3.42 Rating (152 Votes )
There are 3 Steps involved in it
To read values from text files into the arrays wt and val you can utilize fscanf to parse integers efficiently Below is a modified version of your cod... View full answer
Get step-by-step solutions from verified subject matter experts
