How do I read text files and and put it into the array wt[] and val[]? Text
Fantastic news! We've Found the answer you've been seeking!
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
Transcribed Image Text:
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; 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;
Expert Answer:
Answer rating: 100% (QA)
Solution Below is the C code to read the integers from text file and put them into integer arrays as ... View the full answer
Related Book For
Business and Administrative Communication
ISBN: 978-0073403182
10th edition
Authors: Kitty o. locker, Donna s. kienzler
Posted Date:
Students also viewed these programming questions
-
How can Mary split up 12 hamburgers and 16 hot dogs among her sons Richard, Peter, Christopher, and James in such a way that James gets at least one hamburger and three hot dogs, and each of his...
-
1. Prompt the user to enter a number, read the number and then put it into memory at address Ox10010000 2. Prompt the user to enter a number, read the number and then put it into memory at address...
-
Question 1: You can invest in two risky assets A and B, and in the risk-free asset with a return of 5%. The expected return and the standard deviation of the risky asset A is 10% and 0.2 respectively...
-
Consider a uniform plane wave having E = Ex(z, t), and H = H, (z,t), and propagating in the +z direction in a lossless dielectric medium. The time variation of Ex in a constant z-plane and the...
-
Describe three changes that occur in chromosome structure.
-
On March 9, Modern Kitchens, a retail store, received Credit Memorandum 244 for $3,420 from J & M Appliance Corporation. The credit memorandum covered a return of damaged trash compactors originally...
-
Why is an ethical culture considered necessary for fraud prevention and deterrence?
-
Brisky Corporation had net sales of $2,400,000 and interest revenue of $31,000 during 2012. Expenses for 2012 were: cost of goods sold $1,450,000; administrative expenses $212,000; selling expenses...
-
The BiHye Cycle Company has three plants that supply deluxe bicycles to three main countries of USA, Canada, and Germany. The plants' daily capacities, the countries' yearly requirements, as well as...
-
In this mini-case you will perform some procedures required as a part of audit planning. For ease your audit manager has already organized the workpapers and completed several of the required...
-
Refer to the tax structure below for the following questions. Taxpayer Jennifer John Harriott Salary rate? 40,000 20,000 40,000 Total Tax $4,500 $4,500 ??? Use the following format for your answers...
-
Consider an investment that pays $17.17 every year forever. This firm does not intend to grow and has an interest rate (required rate of return) of 8%. What is the present value of this investment...
-
Suppose that the demand for wheat from domestic buyers is given as: Qdd=1465-88p and the demand for export buyers is given as: Qde=1344-138p. Find the market demand function by horizontally summing...
-
In the space 12, equipped with the standard norm 1113 ||*||2:=x2/2 we define additional two norms. |||| : k=1 2 x7/ ||2||16:=x-1 k=1 + 2 = (Tk) h=1 1. co WI 1 DE C + 2/2k k=1 Show that the norms ||...
-
Suppose you have agreed to lend your roommate a certain amount of money today. Your roommate has promised to pay you back $5,000 two years from today and $7,000 four years from today. Assume the...
-
A given image has the gray level PDF, p. (r), shown below. It is desired to transform the gray levels of this image so that they will have the specified p(z) shown below. Assuming continuous...
-
1. Jane Spring maintained this record of vehicle expenses for last year: gas, $845.96; oil, lube, miscellaneous, $68.85; insurance, $418.50; and license, $35.00. She drove 8,280 miles last year. Her...
-
In the synthesis of the keto acid just given, the dicarboxylic acid decarboxylates in a specific way; it gives Explain. HO rather than HO
-
For the following topics, indicate the types of sample(s) you would use in collecting survey data and in conducting interviews. a. How can your school save money to limit tuition increases? b. How...
-
Why are career objectives hard to write?
-
What are some communication signals you might receive from specific body language cues?
-
A chemical reaction is found to be 15 times faster at \(100^{\circ} \mathrm{C}\) than at \(25^{\circ} \mathrm{C}\). Measurements show that the pre-exponential term contains temperature to the power...
-
(a) What is meant by the terms (i) a global reaction; (ii) an elementary reaction; (iii) a reaction mechanism. (b) Describe the steps required to form a chain reaction and explain why chain reactions...
-
The rate of change of mole concentration of constituent \(A\) in a chemical reaction is expressed as \[\frac{\mathrm{d}[\mathrm{A}]}{\mathrm{d} t}=-k[\mathrm{~A}]^{\mathrm{n}}\] While mole...
Study smarter with the SolutionInn App