Challenging problem. Please help C++ The apple-management system A local apple f
ID: 3685607 • Letter: C
Question
Challenging problem. Please help C++
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. 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 It is important that your functions are named exactly as they are shown here (see definitions below).
/////It also means that any printing you do in your main function will be ignored by COG. Any printing inside your functions will confuse COG. In your main() function, you can test that your functions are working correctly by implementing the algorithm shown below. It’s important to note that COG won’t run your main function for this assignment, any code you write in main will be only for testing your functions.
You are welcome to write additional tests in main to verify that your functions are working. For example, you may want to write a while loop that calls harvestApples() until endOfMonth() is true and update the inventory each time through the loop.
Function Definitions: bool sellApples(int sales[], int inventory, int demand, int currentDay) void harvestApples(int sales[], int currentDay, int dayHarvest) bool endOfMonth(int currentDay) int updateCurrentDay(int currentDay) int updateInventory(int inventory, int change) double calculateAverageHarvest(int harvest[]) double calculateAverageSales(int sales[])
Sample Logic for Main: Create integer arrays for sales and harvest to store 30 integers Initialize current day to 0. // We start with a current day = 0 because it is the index in the array // The 30 days of data will be stored in the array at indices 0 ... 29. Initialize the inventory to 0 While current day < 30: Ask the user for a harvest amount, or read from another array Call harvestApples() to update the harvest available Update the inventory Ask the user for a sales amount, or read from another array Call sellApples() to update the sales If sellApples() returns True Update the inventory Update the current day Check if it’s the end of the month Print a nice message saying that you have 30 days of data Calculate the average harvest Print the average harvest result Calculate the average sales Print the average sales result
Here is the code I have so far:
#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;
}
The program which is grading my code is telling me that the integer currentDay is unused yet the problem states I need to implement, also my grading program is not running my code and returning the error
Now looking at the problem statement I am using cout in the main function. If anyone could carefully go over the guidelines and tell me what is wrong with the code I have that would be awesome. The expected output should look something like this
Explanation / Answer
Here is the perfectly working code for you:
#include <iostream>
#define MONTH 5
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;
}
You just try with this code. If you find any problem with this, just get back to me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.