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

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 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

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!