this program i created isn't showing right results. When you run the program For example it suppose to output: Day 2: 9 unit(s) shipped on 2 truck(s)
but instead it outputs: Day 2: 9 units shipped on 16385 trucks.
see the test program and u can see that the function works, it's just not working with the array from main as one of its parameter. What do i need to fix in first program to get result:Day 2: 9 unit(s) shipped on 2 truck(s)
#include <iostream> // console input/output for C++
#include <iomanip>
#include <cmath>
usingnamespace std;
int GetValidInt(int min, int max); // function prototype BEFORE main()
double calculateTrucks(double inputValue, double maxUnits); // function prototype EFFORE main()
int main()
{
// DECLARATIONS
double average; // the resulting average units
int count; // counter for the student grade loop
int sum = 0; // calculate the sum
int numberOfDays; // number of days
int *units; // number of units
constint MAX_UNITS = 8;
double numberOfTrucks;
double inputValue;
// INPUT
cout << "\nPlease enter the number of shipment days to be analyzed: ";
numberOfDays = GetValidInt(0, 900);
try
{
// Create the array
units = newint [numberOfDays];
// Loop ? times
for(count = 0; count < numberOfDays; count++)
{
// Prompt for grade
cout << "\nPlease enter the number of units for day # " << count + 1 << ": ";
// Get the grade
units[count] = GetValidInt(0, 900);
sum += units[count];
}
// PROCESS
// Calculate the average = sum / count
average = (double)sum / count;
// Calculate the number of trucks
numberOfTrucks = calculateTrucks(units[count], MAX_UNITS);
// OUTPUT
// Loop to display the units and trucs stored in the array
for(count = 0; count < numberOfDays; count++)
{
if(units[count] <= MAX_UNITS)
{
cout << "\n\nDay " << count + 1 <<": " << units[count] << " unit(s) shipped on 1 Truck(s)";
}
else
{
cout << "\n\nDay " << count + 1 <<": " << units[count] << " unit(s) shipped on " << numberOfTrucks << " Truck(s)";
}
}
// Display the average
cout << fixed << setprecision(2);
cout << "\nAverage: " << average << endl;
delete [] units; // we are done with this array, allow other processes to use the memory
}
catch(bad_alloc) // the memory for the array could not be allocated at run-time
{
cout << "\nCould not allocate memory!";
}
// end of program, pause the console
cout << endl;
system("pause");
return 0;
}
int GetValidInt(int min, int max)
{
int inputValue;
bool valid = false;
// Loop until valid
while(!valid)
{
cin >> inputValue;
fflush(stdin);
if(cin.fail() || inputValue < min || inputValue > max)
{
// input is bad
cout << "\nInput is not valid. Please try again: ";
cin.clear();
}
else
{
valid = true;
}
}
return inputValue;
}
double calculateTrucks(double inputValue, double maxUnits)
{
double trucks;
trucks = inputValue / maxUnits;
return ceil(trucks);
}
when the program asks you to enter number of units for day 1
1 2 3 4 5 6 7 8 9 10
for(count = 0; count < numberOfDays; count++)
{
// Prompt for grade
cout << "\nPlease enter the number of units for day # " << count + 1 << ": ";
// Get the grade
units[count] = GetValidInt(0, 900);
sum += units[count];
}
if you enter anything more than 8 than function takes that value and devides it by 8 and rounds up to next whole number.
the test program in my frist post shows you what the function suppose to do.
for day 1 if it's anything more that 8 units than it's suppose to divide the units by 8 and round up to the next whole number(so 9 divided by 8 would be 2 trucks) and display this as number of trucks for day 1.
same goes for day 2,3,4,5 and so on.