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

printf(knapsack Capacity: 
); int W; FILE *fileCapacity; fileCapacity = fopen(knapsack1_capacity.txt, r); if (fileCapa

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

1 Expert Approved Answer
Step: 1 Unlock

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

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!