Question: 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 int
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

![into the array wt[] and val[]? Text Files: knapsack1_values.txt: 24,13,23,15,16 knapsack1_weights.txt: 12,7,11,8,9](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f112e5dd1c8_04566f112e560855.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); 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
