Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program uses structures and functions. The Problem Statement You and your r

ID: 3818461 • Letter: T

Question

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. Program Setup A scaffold of the solution has been created for you: dvd-scaffold.cPreview the documentView in a new window Do not modify the code that is already present. Instead, fill in the getProfit function marked like this: /*** ... ***/ 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); Sample Run dvd.inView in a new window dvd.outView in a new window Specification Homework assignments are either correct or incorrect. Do not modify any of the printf statements in the scaffold. Do not add any printf statements to the program. Submission Test your program for correctness. Make sure you code is easy to read and that you include a header comment and comments throughout your function(s). Finally, attach your .c source file to this assignment and submit it.

Explanation / Answer

#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) {
int i = 0, j = 0;
double sum = 0, addSum;
/*** FILL THIS IN ***/
//sorts through all the titles and finds matches
for (j = 0; j < len1 && j < len2; j++){
for (i = 0; i < len1 && i < len2; i++)
{
//if it finds a match then...
if (strcmp(list1[i].title , list2[j].title) == 0)
{
//plugs the result of the higher price from function max into addSum
addSum = max(list1[i].salePrice, list2[j].salePrice);
//adds addSum to sum
sum += addSum;
}
else
//makes sure loop continues in case of issue
continue;
}
}
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 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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote