Need some help writing in the needed code for this program T

Need some help writing in the needed code for this program. The code needed is denoted by /*********/

Program Setup

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.

File in- dvd.txt

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

File out-dvd.out

13.99
50.97

#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 allocated array.

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;

}

Solution

Include the strings.h header file for string insensitive comparison

Required Snippet:

// Variables Declarations
// Double Variable to Store Maximum Profit
double profit = 0.0;

// Integer Variables for indexing List1 and List2
int i, j;

/* Pointer to Store the list1
This list will be updated if movie price in list2 is greater than list1 for same movie
and when distinct movies in list2
*/
DVD* moviesCollection = list2;

// Integer to store the length of movies collection
int moviesCollectionLength = len2;

// Flag for Duplicate Movie
char isDuplicate;

// Variables to store movie names in list1 and list2
char myMovieTitle[100], yourMovieTitle[100];

/*
Logic Implemented to find the maximum profit
Store the distinct movies from list2 to moviescollection
If duplicates exist, update the salePrice to greater price
*/
for(i=0; i<len1; i++){
   isDuplicate = \'f\';
   myMovieTitle = *list1[i].title;
   for(j=0; j<len2; j++){
       yourMovieTitle = *list2[j].title;
       if(smpstri(myMovieTitle, yourMovieTitle) == 0) {
           isDuplicate = \'t\';
           break;
       }
   }
   if(isDuplicate == \'t\') {
       // If duplicates exist, update the salePrice to greater price
       if(*list1[i].salePrice >= *list2[j].salePrice) {
           moviesCollection[i].salePrice = *list1[i].salePrice;
       } else {
           moviesCollection[i].salePrice = *list2[j].salePrice;
       }
   } else {
       // Store the distinct movies from list2 to moviescollection
       moviesCollection[moviesCollectionLength++] = list1[i];
   }
}

// Calculate the Profit
for(i=0; i<moviesCollectionLength; i++){
   profit += *moviesCollection[i].salePrice;
}

// Return the profit
return profit;

Need some help writing in the needed code for this program. The code needed is denoted by /*********/ Program Setup This function takes in two arrays of DVDs (r
Need some help writing in the needed code for this program. The code needed is denoted by /*********/ Program Setup This function takes in two arrays of DVDs (r
Need some help writing in the needed code for this program. The code needed is denoted by /*********/ Program Setup This function takes in two arrays of DVDs (r
Need some help writing in the needed code for this program. The code needed is denoted by /*********/ Program Setup This function takes in two arrays of DVDs (r

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site