0

My guess is. However all the example I see create an instance of item_type say item_type_instance. However my case is more simpler...I want something descriptive for my array not just using 0 and 1.

enum item_type {weight, cost};

and then substitute weight and cost for 0 and 1.

void algo(int cost_low,int cost_high,int throw_weight, int item_id)
  {
  int quantity,remainder;
  quantity=throw_weight/item_matrix[item_id][0];
  remainder=throw_weight%item_matrix[item_id][0];
  if(remainder==0)
    {
    cost_low=(quantity-1)*item_matrix[item_id][1];
    cost_high=quantity*item_matrix[item_id][1];
    throw_weight-=(quantity-1)*item_matrix[item_id][0];
    }
  else
    {
    cost_low=quantity*item_matrix[item_id][1];
    cost_high=(quantity+1)*item_matrix[item_id][1];  
    throw_weight-=quantity*item_matrix[item_id][0];
    }
  }
user1001776
  • 187
  • 1
  • 3
  • 9
  • by the way, this algo has no side effects, nor a return value... So it's equivalent to `void foo(){}`. Unless you mean to pass `cost_low`, `cost_high` and `throw_weight` by reference as in `algo( int& cost_low etc...)` – xtofl Oct 19 '11 at 16:32
  • yes I want to pass be reference for cost_low, cost_high, and throw_weight...need to udpate – user1001776 Oct 19 '11 at 16:39

3 Answers3

3

Of course you could do that; but wouldn't you rather represent the items in the item_matrix by something more meaningful than an array?

struct Item {
  int weight;
  int cost;
};

This may render your algorithm more readable:

void algo(int cost_low,int cost_high,int throw_weight, int item_id)
  {
  int quantity,remainder;
  Item& item = item_matrix[item_id];
  quantity=throw_weight/item.weight;
  remainder=throw_weight%item.weight;
  if(remainder==0)
    {
    cost_low=(quantity-1)*item.cost;
    cost_high=quantity*item.cost;
    throw_weight-=(quantity-1)*item.weight;
    }
  else
    {
    cost_low=quantity*item.cost;
    cost_high=(quantity+1)*item.cost;  
    throw_weight-=quantity*item.cost;
    }
  }

It may be possible to refactor even further, and delegating the calculation to the Item, too.

-- EDIT I couldn't resist... It is possible to delegate to the Item itself, getting rid of all the item.xxx notations.

struct Item {
   int weight;
   int cost;

   void algo( int& cost_low, int& cost_high, int& throw_weight ) {
      int quantity = throw_weight / weight;
      int remainder = throw_weight % weight;

      cost_low=(quantity-1)*cost;
      cost_high=quantity*cost;
      throw_weight -= (quantity-1)*weight;

      if( remainder != 0 ) {
         cost_low += cost;
         cost_high += cost;
         throw_weight += weight;
      }
   }
};

Usage:

item_matrix[item_id].algo( cost_low, cost_high, throw_weight );
xtofl
  • 40,723
  • 12
  • 105
  • 192
  • but with an array I know how the memory is laid out...continuously...but for a struct I do not.. – user1001776 Oct 19 '11 at 16:33
  • You could look it up and see it is, too. – xtofl Oct 19 '11 at 16:34
  • @user1001776, do you have a requirement for the memory to be laid out continuously? The layout of a struct is well defined by the standard, except for possible padding which shouldn't be an issue when the items in the struct are the same type. – Mark Ransom Oct 19 '11 at 16:39
  • @user1001776: "google c++ struct memory-layout", which points you to http://stackoverflow.com/questions/422830/structure-of-a-c-object-in-memory-vs-a-struct/423355#423355. – xtofl Oct 19 '11 at 17:55
1

Simply define the enumerators to be 0 and 1:

enum item_type
{
    weight = 0
  , cost = 1
};

The standard conversion from enum to int will allow you to use enumerations to index an array.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • 0 and 1 is default..correct..i don't have to to this explicitly – user1001776 Oct 19 '11 at 16:26
  • 2
    Yes, 0 and 1 are the default values, but you want to make this explicit. Otherwise someone may come later and swap the order of the enumerators and your code would stop working. – K-ballo Oct 19 '11 at 16:29
  • 1
    If you are depending on the values of `weight` and `cost` being 0 and 1 respectively, do as @K-ballo demonstrates and specify the values explicitly, as a signal to future code maintainers that they need to have those values. It stops people from inserting a new item at the front (or between them) in the enum and so breaking your code. – Daniel Earwicker Oct 19 '11 at 16:30
0

If I understand your question correctly then I think you want to use enum as index to the array. If so, then you can do that:

quantity=throw_weight/item_matrix[item_id][weight]; //weight <=> 0
cost_low=(quantity-1)*item_matrix[item_id][cost];   //cost   <=> 1

The value of weight and cost are 0 and 1 respectively, so the above code is perfectly fine. If the value for enumeration is not provided, then it starts with 0 and increments 1 with each subsequent enum label.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • @user1001776: Yes. Why don't you TRY? – Nawaz Oct 19 '11 at 16:29
  • i knew someon would say this...b.c I just refactored about 600 lines of code...and It's going to take a while to dig thought errors..but yes I should have made changes incrementally and fixed as I went along...got impatient..but yes you are right – user1001776 Oct 19 '11 at 16:35