Hi everyone, I have some c++ code for a project and my online grader is finding
ID: 3685773 • Letter: H
Question
Hi everyone, I have some c++ code for a project and my online grader is finding some problems with some of the functions in it. First I will post my code and then I will post the errors it is pulling up. can someone please help me correct these errors?
Heres The Code
#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;
}
Here are the Errors
1.)
2.)
Grading the calculateAverageHarvest function...
3.)
Thanks! and please help me get the correct outputs!
Explanation / Answer
See. There's nothing wrong with your code. The only logical mistake is, you initialized the MONTH in the preprocessor as: #define MONTH 5, and therefore, your logic, when connected to the automated testing system, is checking only the first 5 elements in the array, and therefore, in error 1, and error 2, both the cases the average is being calculated using the first 5 random values generated. So, the values in error 1 are: (14 + 4 + 16 + 7 + 21) / 5 = 12.4. And the same kind of problem arises in error 2 also.
So, all you have to do is change the preprocessor value in the second line of the code from #define MONTH 5, to #define MONTH 30
And it will work like a charm. If not, just get 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.