Challenging problem. Please help C++ The apple-management system A local apple f
ID: 3684841 • 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
Heres all my code I have so far
#include <iostream>
using namespace std;
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;
}
int main() {
// You can prompt for data or set up data in arrays
//int salesdata[] = { 44, 31, 22 }; // add more data to 30 elements
//int harvestdata[] = { 52, 22, 14 }; // add more data to 30 elements
int harvest[30];
int sales[30];
int inventory = 0;
int dayHarvest;
int demand;
for (int day=0; day<30; day++) {
// Get the harvest amount for the day
cout<<"Enter the havest amount for the day: ";
cin>>dayHarvest;
harvestApples(harvest,day,dayHarvest);
inventory = updateInventory(inventory,dayHarvest);
cout<<"Enter the number of apples requested (demand): ";
cin>>demand;
// logic for selling apples
cout<<"Inventory = "<<inventory<<endl;
}
// logic for caculating final averages
printArray(harvest, 3);
cout<<"Inventory is "<<inventory<<endl;
// print averages here
return 0;
}
Heres a second block of code I have
#include <iostream>
using namespace std;
bool endOfMonth(int currentDay) {
if (currentDay == 30)
return true;
else
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 a[], int len) {
for (int i=0; i<len+1; i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}
int main() {
// Example array if you want to access harvestDay & demand from an array
//int salesdata[] = { 44, 31, 22 }; // add more data to equal30 elements
//int harvestdata[] = { 52, 22, 14 }; // add more data to equal 30 elements
// Harvest variables
int harvest[30];
int sales[30];
int inventory = 0;
// Daily values
int dayHarvest;
int demand;
int currentDay = 0;
while (!endOfMonth(currentDay)) {
// Ask the user for a harvest amount, or read from another array
cout<<"Enter the number of apples harvested for the day: ";
cin>>dayHarvest;
// Call harvestApples() to update the harvest available
harvestApples(harvest,currentDay,dayHarvest);
// Update the Inventory
inventory = updateInventory(inventory, dayHarvest);
printArray(harvest,currentDay);
cout<<"Current Inventory: "<<inventory<<endl;
// Ask the user for a sales amount, or read from another array
// Call sellApples() to update the sales
// If sellApples() returns True
//Update inventory
// Update the current day
currentDay++; // Make sure you put function call here
}
// calculate averages
return 0;
}
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.