Finding and Printing index of arrays

I'm currently stuck on the final part of my homework assignment for intro to c++. I'm having difficulty getting my program to output the cost. I can't seem to figure out how to compare my mass array and costs array to output the cost at all. I am completely stuck. I have the FindIndexOfMass function completed, however i do not know how to use it to calculate the associated cost. Below is a snippet of the task that I am required to do. It sounds relatively easy, but my brain just wont process how to do it, even though its clearly written out. Guidance would be greatly appreciated.


"The program should print the transaction number, the mass, and either the cost of sending the package or a rejection message (see above). The cost of sending the package should ONLY be determined if the package is NOT rejected for any of the reasons listed above. This cost is determined by searching the list of masses which are assumed already ordered in ascending order by mass in grams, for example the list of masses in the sample input below are 50, 100, 200, 400, 1000, 2000, 4000, 8000, 16000 and 32000. These 10 values would be stored in the first 10 elements of a mass array. The corresponding costs $1.00, $2.00, $4.00, $8.50, $15.00, $20.50, $25.00, $30.00, $40.00, and $50.00 are stored in the parallel costs array (see sample input below). Using the transaction mass of 200 grams from the transaction mentioned in the previous paragraph, the program would lookup the 200 grams in the mass array, returning the index of the first mass in mass array such that 200 grams is less than or equal to it. In this example, 200 grams in the mass array is index 2. Your program would return the index, 2 via FindIndexOfMass function (see below), and then use this index, 2, into the parallel cost array to determine that the cost of sending this package is Costs[2] which is $4.00.

SAMPLE INPUT AND OUTPUT:
http://imgur.com/OVaigPj

I'm having a hell of a time using the FindIndexOfMass function to calculate the cost. I know that it has to be incredibly simple to do, but i just can't figure out how to do it. I'm in desperate need of guidance. Do i need to add another function?

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  //---------------------------------------------------------------------
//
// Name:  
//
// Course:  CS 1430, Section 4
//
// Purpose: Program outputs a post offices list of packages
//and displays how many packages, cost of rejected packages
//, how many rejected packages, and a table showing mass of packages
//
// Input: 1 integer for mass, 1 integer for cost
//3 integers for each side of box, integer for trans. number, 
//and 2nd integer for mass of transaction table
//
// Output:  outputs a table of mass and corresponding cost
//outputs table showing each individual transaction
//and their individual number, mass, or corresponding rejection.
//---------------------------------------------------------------------


const int MAX_ARRAY_SIZE = 25;
const int MAX_TRANS_SIZE = 3;

const int DIM_MASS_LIMIT = 32000;
const int DIM_LENGTH_LIMIT = 101;
const int DIM_SUM_LIMIT = 240;

const int DOUBLER = 2;
void ReadParcelPostTable(int masses[], float costs[], int & sizeOfTable);
void PrintParcelPostTable(const int masses[], const float costs[], 
								  int sizeOfTable);
int FindIndexOfMass(const int masses[], int lookUpMass, int size);
float FindLargestDimension( const int dim[], int size);
float Girth( const int dim[], int size);
bool TestLength ( const int s1, const int s2, const int s3 );
bool TestMass ( const int mass );
void PrintTransactionTable ( int transNumber, int mass, int s1,
									 int s2, int s3);

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
   cout << fixed << showpoint << setprecision(2);

   int masses[MAX_ARRAY_SIZE];
   float costs[MAX_ARRAY_SIZE];
   int transNumber = 0;
   int sizeOfTable, mass = 0;
   int s1 = 0, s2 = 0, s3 = 0;
   ReadParcelPostTable( masses, costs, sizeOfTable );
   PrintParcelPostTable( masses, costs, sizeOfTable );
   PrintTransactionTable ( transNumber, mass, s1, s2, s3 );

   return 0;
}

//Reads the parcel post table, reading the size of the table first
//and that many masses and costs.  Assume that the massess are
//ordered in ascending order by mass in (integer) grams.
//params: in, in, inout
void ReadParcelPostTable(int masses[], float costs[], int & sizeOfTable)
{
   cout << "Enter how many in Parcel Post Table (Grams Dollars):\n\n";
   cin >> sizeOfTable;
   cout << "Enter Parcel Post Table (mass & cost) information:\n\n";
   for ( int i = 0; i < sizeOfTable; i++ )
      cin >> masses[i] >> costs[i];
}

//Prints the parcel post table in tabular form. Prints the heading 
//that includes printing the size of the table. (See sample output)
//params: in, in, in
void PrintParcelPostTable(const int masses[], const float costs[],
								  int sizeOfTable)
{
   cout << "Parcel Post Table in tabular form with " << sizeOfTable
      << " entries. \n";
   cout << setw(10) << "Mass" << setw(12) << "Cost" << endl;
   cout << setw(10) << "------" << setw(12) << "-----" << endl;
   for ( int i = 0; i < sizeOfTable; i++ )
      cout << setw(10) << masses[i] << setw(12) << costs[i] << endl;
   cout << setw(10) << "------" << setw(15) << "-----\n\n\n";

}

//Finds and returns an index of the look-up-mass in the Masses 
//array. Since the Masses array is ordered in ascending order by 
//mass, this function returns the index of the first mass 
//in the masses array such that lookUpMass is less than or equal 
//to masses[i].
//On the off chance that no such index can be found, returns -1.
//params: in, in, in
int FindIndexOfMass(const int masses[], int lookUpMass, int size)
{
   int index = -1;
   for ( int i = 0; i < size; i++ )
   {
      if ( lookUpMass <= masses[i] )
         return i;
   }
   return index;
}


//Returns the largest value in the dim array; size is how many items
//are in the dim array.
//params: in
float FindLargestDimension( const int dim[], int size)
{
   float largest = 0;
   for ( int i = 0; i < size; i++ )
   {
      if ( dim[i] > largest )
         largest = dim[i];
   }
   return largest;
}


//Returns the girth of a parcel where girth is calculated as twice 
//the difference of the sum of the elements in the dim array and the 
//largest of the dimensions. 
//params: in
float Girth( const int dim[], int size)
{
   float girth;
   int sum = 0;
   for (int i = 0; i < size; i++ )
      sum += dim[i];
   girth = DOUBLER * ( sum - FindLargestDimension (dim, size ) );
   return girth;
	//Girth = 2 * ( s1 + s2 + s3 – length)
}

bool TestMass ( const int mass )
{
   if ( mass > DIM_MASS_LIMIT )
      return true;
   else
      return false;
}

bool TestLength ( const int s1, const int s2, const int s3 )
{
   if ( s1 >= DIM_LENGTH_LIMIT || s2 >= DIM_LENGTH_LIMIT
      || s3 >= DIM_LENGTH_LIMIT )
      return true;
   else
      return false;
}

void PrintTransactionTable ( int transNumber, int mass, int s1,								 int s2, int s3 )
{
   int processed_Counter = 0, rejected_Counter = 0;
   float cost_Of_Rejected = 0;
   cout << "Transaction Number" << setw(11) << "Mass" << setw(27)
      << "Cost/Rejection Message" << endl;
   cout << "------------------" << setw(11) << "-----" << setw(36)
      << "-------------------------------" << endl;
   cin >> transNumber >> mass >> s1 >> s2 >> s3;
   while ( !cin.eof() )
   {
      processed_Counter++;
      cout << setw(18) << transNumber << setw(11) << mass;
      if ( TestMass(mass) )
      {
         rejected_Counter++;
         cout << setw(35) << "REJECT_01 - EXCEEDS MASS LIMIT";
      }
      else if ( TestLength( s1, s2, s3) )
      {
         rejected_Counter++;
         cout << setw(40) << "REJECT_02 - EXCEEDS DIMENSION LIMIT";
      }

      cin >> transNumber >> mass >> s1 >> s2 >> s3;
      cout << endl;
   }
   cout << setw(30) << "\nNumber of packages processed is " 
      << processed_Counter << endl;
   cout << setw(30) << "Number of packages rejected is " 
      << rejected_Counter << endl;
   cout << setw(30) << "Total cost of sending non-rejected packages is "
      << "$" << endl;
}
Last edited on
closed account (48T7M4Gy)
As a first step why not hard code the mass-cost table with a few values and test that instead of being to enter the table each time?

You don't have to run the whole she-bang just to test a part of it. Having run your program it looks pretty good but doing it this way will save a lot of time and angst. :)

Topic archived. No new replies allowed.