This program uses structures and functions The Problem State
This program uses structures and functions.
The Problem Statement
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.
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.
Function Prototype
You must use this prototype to receive credit for the assignment.
// 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);
Program Setup
A scaffold of the solution has been created for you.
Do not modify the code that is already present. Instead, fill in the getProfit function marked like this: /*** ... ***/
// Arup Guha// 7/22/2015// Scaffold for Common DVDs.
 #include <stdlib.h>#include <stdio.h>#include <string.h>
 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;    fscanf(ifp, \"%d\", &numCases);
     // Go through each case.    for (loop=0; loop<numCases; loop++) {
         // Read in movies.        int len1,len2;        fscanf(ifp, \"%d\", &len1);        DVD* myMovies = get(ifp, len1);        fscanf(ifp, \"%d\", &len2);        DVD* 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) {
     /*** FILL THIS IN ***/}// 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 allocatedarray.DVD* get(FILE* ifp, int len) { DVD* res = malloc(sizeof(DVD)*len); int i; for (i=0; i<len; i++) fscanf(ifp, \"%s%d%d%lf\", res[i].title, &(res[i].runTime),&(res[i].idtag), &(res[i].salePrice)); return res;}
Sample Run -
Input
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
Out put -
13.99
 50.97
(This is beginning programming C, not C++)
Solution
Here is the code for you:
 // Arup Guha
 // 7/22/2015
 // Scaffold for Common DVDs.
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 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;
 fscanf(ifp, \"%d\", &numCases);
 // Go through each case.
 for (loop=0; loop<numCases; loop++)
 {
 // Read in movies.
 int len1,len2;
 fscanf(ifp, \"%d\", &len1);
 DVD* myMovies = get(ifp, len1);
 fscanf(ifp, \"%d\", &len2);
 DVD* 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;
 }
 int found(DVD *list, int len, DVD key)
 {
 for(int i = 0; i < len; i++)
 if(strcmp((*(list+i)).title, key.title) == 0)
 return 1;
 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)
 {
 /*** FILL THIS IN ***/
 double sum = 0.0;
 for(int i = 0; i < len1; i++)   //for each DVD in list1
 if(found(list2, len2, *(list1 + i)))   //If that DVD is found in list2.
 sum += (*(list1 + i)).salePrice;
 return sum;
 }
 // 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 allocatedarray.
 DVD* get(FILE* ifp, int len)
 {   
 DVD* res = malloc(sizeof(DVD)*len);
 int i;
 for (i=0; i<len; i++)
 fscanf(ifp, \"%s%d%d%lf\", res[i].title, &(res[i].runTime),&(res[i].idtag), &(res[i].salePrice));
 return res;
 }



