A local apple farmer has asked you to writ a program to help him manage his appl
ID: 3685194 • Letter: A
Question
A local apple farmer has asked you to writ 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 seven 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 sale[currentDay] = 0
//if the array is updated with demand, the functions should return True.
//Otherwise, the function should return False
void harvestApples(int harvest[], int currentDay, int dayHarvest)
//upate 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 and if sellApples() returns True, indicating that the sale was successful and apples needed 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>
using namespace std;
const int days=30;
bool sellApples(int sales[], int inventory, int demand, int currentDay)
{
if(demand<=inventory)
{
sales[currentDay]=demand;
return true;
}
else
sales[currentDay]=0;
return false;
}
void harvestApples(int harvest[], int currentDay, int dayHarvest) {
harvest[currentDay] = dayHarvest;
}
int updateInventory(int inventory, int change) {
return inventory + change;
}
void printArray(int mydata[], int len) {
for (int i=0; i<len; i++) {
cout<<mydata[i]<<" ";
}
cout<<endl;
}
bool endOfMonth(int currentDay) {
if (currentDay == days-1)
return true;
else
return false;
}
double calculateAverageHarvest(int harvest[])
{
int total=0.0;
for(int i=0;i<days;i++)
total+=harvest[i];
return total/days;
}
double calculateAverageSales(int sales[])
{
int total=0.0;
for(int i=0;i<days;i++)
total+=sales[i];
return total/days;
}
int main()
{
int harvest[days];
int sales[days];
int currentDay=0;
int inventory = 0;
int dayHarvest;
int demand;
for (int day=0; day<days; day++) {
// Get the harvest amount for the day
cout<<"Enter the havest amount for the day: ";
cin>>dayHarvest;
harvestApples(harvest,day-1,dayHarvest);
inventory = updateInventory(inventory,dayHarvest);
cout<<"Enter the number of apples requested (demand): ";
cin>>demand;
bool sold=sellApples(sales, inventory,demand,day-1);
// logic for selling apples
if(sold)
{
int change=demand*-1;
inventory = updateInventory(inventory,change);
}
cout<<"Inventory: "<<inventory<<endl;
if(endOfMonth(day))
{
cout<<"Congrats! You have "<< days<<" days of data. Lets have a look at it."<<endl;
cout<<"Average harvest apples: "<<calculateAverageHarvest(harvest)<<endl;
cout<<"Average sales :"<< calculateAverageSales(sales)<<endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.