Updating one stock record through two parameters

An inventory simulation uses the following C function. Each units member records a number of items. Treat the two scenarios below as independent runs, and report all three requested final counts as exact integers in items.


C
struct Stock {
    int units;
};

void redistribute(struct Stock *source,
                  struct Stock *destination,
                  struct Stock opening)
{
    source->units -= opening.units / 4;
    destination->units += opening.units / 4;
    source->units += destination->units / 10;
}


In the shared-object scenario, the call is:


C
struct Stock bin = {160};
redistribute(&bin, &bin, bin);


In the separate-object scenario, the call is:


C
struct Stock reserve = {160};
struct Stock shelf = {60};
redistribute(&reserve, &shelf, reserve);


Find the final count in bin for the shared-object scenario, then the final counts in reserve and shelf for the separate-object scenario. All divisions in these runs have exact integer results.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Passing Structures to Functions Questions: