Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi there, I'm having trouble with this fill in the blank question from my quiz, we're given a skeleton code to work off of and

Hi there, I'm having trouble with this fill in the blank question from my quiz, we're given a skeleton code to work off of and have to select the right choices to make the code work, here is the question (I will bold the choices )-

You and your roommate are combining your DVD collections. What you'd like to do is see which DVD's you have duplicate copies of, and then sell the extra copies on eBay, splitting the proceeds equally. To automate the process of determining how much money you expect to make, you decide to write a computer program.

The part of the program remaining is the "getProfit" function. This function takes in two arrays of DVDs (representing your roommates' DVDs and your DVDs, respectively), the lengths of both arrays, and returns the sum of the sale prices of all the DVDs in both collections with the identical titles. If both of you share a DVD with the same title but different sale prices (the 7th edition always costs more than the 6th), assume that you'll sell the more expensive copy and keep the cheaper one, in an effort to maximize your return.

Fill in the function in the following program so that it will return the sum of the sale prices of all the duplicate DVDs.

#include

#include

#include

typedef struct {

char title[ 100 ];

int runTime;

int idtag;

double salePrice;

} DVD;

double getProfit(DVD* list1, int len1, DVD* list2, int len2) ;

double max(double a, double b);

DVD* get(FILE* ifp, int len);

int main() {

FILE* ifp = fopen("dvd.in", "r");

int numCases, loop, len1, len2;

DVD *myMovies, *yourMovies; fscanf(ifp, "%d", &numCases);

// Go through each case.

for (loop=0; loop

// Read in movies.

fscanf(ifp, "%d", &len1);

myMovies = get(ifp, len1);

fscanf(ifp, "%d", &len2);

yourMovies = get(ifp, len2);

// Output sale price of all common movies.

printf("%.2f ", getProfit(myMovies, len1, yourMovies, len2));

// Bookkeeping...

free(myMovies);

free(yourMovies);

}

fclose(ifp);

return 0;

}

// Pre-condition: list1 is an array of length len1, list2 is an array of length len2. No title appears in either list more than once.

// Post-condition: Returns the sum of the sale prices of the common DVDs in both lists. Two DVDs are considered the

// same if their titles are identical.

double getProfit(DVD* list1, int len1, DVD* list2, int len2) {

int i,j; double res = 0;

for (i= ["0", "1", "len1", "len2", "list1.idtag", "list2.idtag"] ; i < len1; i++) {

for (j=0; j < ["0", "1", "len1", "len2", "list1.idtag", "list2.idtag"] ; j++) {

if (strcmp(list1 ["0", "1", "i", "j", "len1", "len2"] .title, list2[ j ]. ["idtag", "runTime", "title", "salePrice"] ) == ["0", "1", "len1", "len2"] ) {

res += max ( list1[ i ].salePrice, ["list2[ j ].salePrice", "list2[ j ].title", "list2[ j ].runTime", "list2[ j ].idtag", "list2[ i ].salePrice", "list1[ j ].salePrice"] );

break;

}

}

} ["return list1;", "return list2;", "return len1;", "return len2;", "return res;", "return list1.salePrice;", "return list2.salePrice;", "//no statement is needed here"]

}

// Returns the larger of a and b.

double max(double a, double b) {

if (a > b) return a;

return b;

}

// Reads in len DVDs from ifp and returns those store in a dynamically allocated array.

DVD* get(FILE* ifp, int len) {

DVD* res = malloc(sizeof(DVD)*len);

int i;

for (i=0; i

fscanf(ifp, "%s%d%d%lf", res[ i ].title, &(res[ i ].runTime), &(res[ i ].idtag), &(res[ i ].salePrice));

return res;

}

(End of given code + question, here is the output and input files to help. The input is stored in dvd.in and the output is stored in dvd.out .)

dvd.in -

2 3 BackToTheFuture 108 1234567 15.99 Gremlins 120 13232423 9.99 IndependenceDay 135 2312412 12.99 2 Gremlins 123 13232425 13.99 MonstersInc 95 44232122 15.99 5 a 100 1234213 14.99 b 101 1245332 7.99 c 102 3431533 12.50 d 103 3431434 19.99 e 104 4314133 16.47 4 f 120 5314443 11111111.38 e 104 5134343 16.48 d 110 3431414 14.99 c 105 1234145 14.50

dvd.out -

13.99 50.97

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions