problem trying to use array as function value

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)

1st program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream> // console input/output for C++
#include <iomanip>
#include <cmath>
using namespace 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
    const int 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 = new int [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);

}



test program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream> // console input/output for C++
#include <iomanip>
#include <cmath>
using namespace std;

double calculateTrucks(double inputValue, double maxUnits); // function prototype EFFORE main()

int main()
{
double daysNumber;
double numberOfTrucks;

cout << "\nPlease enter the number of units for day day 1 : ";
cin >> daysNumber;

// Calculate the number of trucks
numberOfTrucks = calculateTrucks(daysNumber, 8);


cout << "\n\nDay "  << numberOfTrucks << " Truck(s)";

  // end of program, pause the console
    cout << endl;
    system("pause");
    return 0;
}

double calculateTrucks(double inputValue, double maxUnits)
{
    
double trucks;
trucks = inputValue / maxUnits;
return ceil(trucks);

}
Last edited on
You pass to function an element that is not exist

numberOfTrucks = calculateTrucks(units[count], MAX_UNITS);

At this point count is equal to numberOfDays while the acceptable range is ( 0, numberOfDays -1 )
ok what do i need to change?
First of all you shall pass correct values to functions. As for your code I do not understand what are you trying to do in this function.
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.
Last edited on
Topic archived. No new replies allowed.