help with arrays


int array1[255];
int array2[255];
int array3[255];
int array4[255];
int array5[255];
int array6[255];
int array7[255];

i have seven arrays with a maximum of 255 values. is there a way to make it to where all the arrays in total cannot reach over 400?
Yes!

1
2
3
4
5
6
7
int array1[50];
int array2[30]
int array3[23]
int array4[47]
int array5[35]
int array6[95]
int array7[120];


And there are many other combinations! :-)

Nah, but seriously: Yes. Without knowing what you want or need exactly, you would have to keep track of the total number of elements in each array, and then check the total (the sum of all these element counts) BEFORE allowing a new entry in any of the arrays. Use a centralized function for this.
Last edited on
no each individual array has to be able to hold at most 255, but all of the arrays total cannot reach over 400.
As WebJose says, as each array has something added to it, increase a number that started at zero. When that number reaches 400, refuse to allow anything else to be added to the arrays.
Last edited on
im not being too clear. every array can only hold 255 individually. i want to figure out if i can make it to where all 7 arrays added together can only hold 400 max. would this work?

int array1[255];
int array2[255];
int array3[255];
int array4[255];
int array5[255];
int array6[255];
int array7[255];

array1 + array2 + array3 + array4 + array 5 + array6 + array7 = int total_arrays[400];
No. This would not work. This is not something that C++ does for you. You will have to code it yourself.
Your problem is that you want to not let any more value stored inside any of these array if 400 values are stored OR
to just allocate 400 places and store 400 values.

By the second option I mean you realize that by using arrays you have allocated 7*255 int already right? If you don't then you need to go around this by allocating dynamically array by demand.

If it's the first case you are interested in then the suggested solution are both satisfactory and easy to implement. Just define a function that do all the value assignments and contain a counter of the values assigned so far. If this is reached prohibit any more storing of values.
no i want this for a video game

lets say we are playing a video game and you can get points to go towards stats

int strength[255];
int defense[255];
int vitality[255];
int endurance[255];
int speed[255];

now every stat can be enhanced to reach 255, but i want it to where collectively you can only have it to where you can only have 500 points for all of the stats combined. any idea on where to start with this?
In this case I would not use arrays. I would just use ints.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int strength = 0;
int defense = 0;
int vitality = 0;
int endurance = 0;
int speed = 0;

//Determine if you levelup and what stats to level up here
...

//Levelup function...
if (level_up)
{
  int total_stats = strength + defense + vitality + endurance + speed;
  if (str_up && strength < 255 && total_stats < 500)
    str_up = false, strength++;

  if (def_up && defense < 255 && total_stats < 500)
    def_up = false, defense++;

  ...

  level_up = false;
}
Last edited on
If you wanted to turn this into a function, I would do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct STATS  //Stats are saved in a single structure.
{
  int strength;      int defense;      int vitality;      int endurance;      int speed;
};

enum ATTRIBUTE {   str,          def,          vit,          end,          spd,    };

STATS level_up(STATS stats, int attribute)
{
  int total_stats = stats.strength + stats.defense + stats.vitality + stats.endurance + stats.speed;
  if (total_stats >= 500) return stats; // Don't level up if total stats are 500.

  switch (attribute)
  {
  case str:
    if (strength < 255) stats.strength++; // Increase strength if str was selected and is not above 255
    break;
  case def:
    ...
  }

  return stats;
}
Last edited on
closed account (zb0S216C)
I vote for struct/class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct SO_NPCAttribute
{
    SO_NPCAttribute();
    SO_NPCAttribute(const unsigned int, const unsigned int);
    
    unsigned int Max, Min, Current;
};

SO_NPCAttribute::SO_NPCAttribute() : Max(500), Min(1), Current(1)
{ }

SO_NPCAttribute::SO_NPCAttribute(const unsigned int NewMin, const unsigned int NewMax)
    : Min(NewMin), Max(NewMax), Current(NewMin)
{ }

int main()
{
    SO_NPCAttribute SI_StrengthAttrib(1, 255);
    SO_NPCAttribute SI_SpeedAttrib(1, 255);
}


Wazzak
Topic archived. No new replies allowed.