The apple-management system A local apple farmer has asked you to write a progra
ID: 3762149 • Letter: T
Question
The apple-management system A local apple farmer has asked you to write a program to help him manage his apple inventory. The farmer needs to keep track of how many apples he sells everyday, how many apples he picks, and how many apples he has in storage. Your apple inventory system needs to track data for 30 days.
Like any self-respecting apple farmer, this farmer thinks the inventory data should be stored in C++ arrays. Your program needs two arrays: one array to store the apple harvest for 30 days and one array to store the apple sales for 30 days.
There are six functions that your program needs to implement. Two of the functions, sellApples() and harvestApples() are used to update the sales and harvest arrays, respectively.
bool sellApples(int sales[], int inventory, int demand, int currentDay) //update sales[] to set sales[currentDay] = demand //The sales[] array should be updated with demand if demand <= inventory //if demand > inventory, then set sales[currentDay] = 0 //if the array is updated with demand, the function should return True. //Otherwise, the function should return False void harvestApples(int harvest[], int currentDay, int dayHarvest) //update harvest[] to set harvest[currentDay] = dayHarvest
Your program also needs a function to check whether there is room available in the harvest and sales arrays. After 30 days, you can stop storing data and print a nice message to let the user know to stop harvesting and selling apples. The function endOfMonth() checks if the array is full by checking if currentDay = 30.
bool endOfMonth(int currentDay) //check if the array is full by checking if currentDay = 30. //if the array is full, then return True. Otherwise, return False Your program also needs function to update the currentDay and inventory variables.
int updateCurrentDay(int currentDay) //this function adds 1 to currentDay and returns this value //return currentDay+1; //Note: if you use currentDay++ here it won’t work.
int updateInventory(int inventory, int change) /*
Use this function to update the inventory variable after calling sellApples() or harvestApples() to change the value of inventory. The parameter change is the change to inventory. If change < 0, it means that apples have been sold, and if change > 0, it means that apples have been harvested.
This function should only be called after sellApples() is called if sellApples() returns True, indicating that the sale was successful and apples need to be deducted from the inventory. The function should also be called after harvestApples() to add apples to the inventory.
The function should return the new value for inventory. */
Once 30 days of data has been collected, calculate the average harvest for the 30 days and the average sales for the 30 days. Your program needs to implement a function for each of these calculations.
double calculateAverageHarvest(int harvest[ ]) //calculate the average daily harvest after 30 days
double calculateAverageSales(int sales[ ]) //calculate the average apple sales after 30 days
Explanation / Answer
#include <iostream>
#define MONTH 30
using namespace std;
//update sales[] to set sales[currentDay] = demand
//The sales[] array should be updated with demand if demand <= inventory
//if demand > inventory, then set sales[currentDay] = 0
//if the array is updated with demand, the function should return True.
//Otherwise, the function should return False.
bool sellApples(int sales[], int inventory, int demand, int currentDay)
{
if(demand <= inventory)
{
sales[currentDay] = demand;
return true;
}
else
{
sales[currentDay] = 0;
return false;
}
}
//update harvest[] to set harvest[currentDay] = dayHarvest
void harvestApples(int harvest[], int currentDay, int dayHarvest)
{
harvest[currentDay] = dayHarvest;
}
//check if the array is full by checking if currentDay = MONTH.
//if the array is full, then return True. Otherwise, return False
bool endOfMonth(int currentDay)
{
if(currentDay == MONTH)
return true;
else
return false;
}
//this function adds 1 to currentDay and returns this value
//return currentDay+1;
//Note: if you use currentDay++ here it won’t work.
int updateCurrentDay(int currentDay)
{
return currentDay+1;
}
//Use this function to update the inventory variable after calling sellApples() or harvestApples()
//to change the value of inventory.
//The parameter change is the change to inventory.
//If change < 0, it means that apples have been sold, and if change > 0, it means that apples have been harvested.
//This function should only be called after sellApples() is called if sellApples() returns True,
//indicating that the sale was successful and apples need to be deducted from the inventory.
//The function should also be called after harvestApples() to add apples to the inventory.
//The function should return the new value for inventory.
int updateInventory(int inventory, int change)
{
return inventory + change;
}
//calculate the average daily harvest after MONTH days
double calculateAverageHarvest(int harvest[])
{
int totalHarvest = 0;
for(int i = 0; i < MONTH; i++)
totalHarvest += harvest[i];
return (double)totalHarvest/MONTH;
}
//calculate the average apple sales after MONTH days
double calculateAverageSales(int sales[])
{
int totalSales = 0;
for(int i = 0; i < MONTH; i++)
totalSales += sales[i];
return (double)totalSales/MONTH;
}
int main()
{
int harvest[MONTH], sales[MONTH], dayHarvest, demand, change;
int inventory = 0, currentDay = 0;
double averageHarvest, averageSale;
bool sold;
while(!endOfMonth(currentDay))
{
cout<<"Enter the harvest for day "<<currentDay+1<<": ";
cin>>dayHarvest;
harvestApples(harvest, currentDay, dayHarvest);
change = dayHarvest;
inventory = updateInventory(inventory, change);
cout<<"Enter the sales demand for day "<<currentDay+1<<": ";
cin>>demand;
sold = sellApples(sales, inventory, demand, currentDay);
if(sold)
{
change = -1 * demand;
inventory = updateInventory(inventory, change);
}
currentDay = updateCurrentDay(currentDay);
}
cout<<"Done for the month. Stop harvesting and selling apples."<<endl;
averageHarvest = calculateAverageHarvest(harvest);
averageSale = calculateAverageSales(sales);
cout<<"The average harvest for the month is: "<<averageHarvest<<endl;
cout<<"The average sale for the month is: "<<averageSale<<endl;
if(averageSale >= averageHarvest)
cout<<"Congratulations. You ended up with Profits..."<<endl;
else
cout<<"Oops. You ended up with Losses..."<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.