Array (multi-dimensional ?) matrix brain-fart.

I seem to be brain-farting today. I have an array matrix I'm trying to implement and could use some advice, please.

Here's basically an example of data I have:

                         Level
           1   2   3   4   5   6   7   8   9   10
           --------------------------------------
Skill 5  | 6   2   3   3   9   1   1   1   6   8
      10 | 6   1   1   8   5   7   2   3   7   3
      15 | 4   4   2   8   6   5   4   7   9   9
      20 | 8   3   3   8   7   3   6   7   9   1
      25 | 9   2   4   1   9   6   8   9   8   3
      30 | 2   1   5   1   9   2   1   1   8   3
      35 | 2   1   6   1   8   7   2   1   9   8
      40 | 1   4   7   4   8   8   2   1   9   7
      45 | 1   8   8   4   9   8   2   3   4   6
      50 | 3   8   9   3   9   2   1   5   3   5
      

I'd like to build a few arrays (perhaps multi-dimensional?) to pull the numbers I need.

Example of how this is to be used:

If a person of level 6 has a skill of 20, I want to return the number "3".
If a person of level 9 has a skill of 45, I want to return the number "4".
etc...

So, given the two constants (level and skill), how can I design an array to hold the return values, and return them as needed?

Here's what I have so far:
1
2
3
4
5
const int MAX_MATRIX = 11;
struct Skill_Matrix {
    int level[MAX_MATRIX];
    int skill[MAX_MATRIX];
};


Now, there are 100 different possibilities (10 x 10). Granted, I *could* do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const int MAX_MATRIX = 11;
struct Skill_Matrix {
    int level[MAX_MATRIX];
    int skill_5[MAX_MATRIX];
    int skill_10[MAX_MATRIX];
    int skill_15[MAX_MATRIX];
    int skill_20[MAX_MATRIX];
    int skill_25[MAX_MATRIX];
    int skill_30[MAX_MATRIX];
    int skill_35[MAX_MATRIX];
    int skill_40[MAX_MATRIX];
    int skill_45[MAX_MATRIX];
    int skill_50[MAX_MATRIX];
};


... and then do something like, person.skill == 25 ? Skill_Matrix.skill_25[person.level]:null;

... but that doesn't seem very efficient. Again, I only want to pull the values needed. The arrays would be stuffed with the data elsewhere.

Suggestions are welcome. Thanks!
Last edited on
Maybe it would be better to use

std::map<unsigned int, std::array<unsigned int, 10>> m;

In this case you could access elements the following way

std::cout << m[20][6 -1 ] << std:;endl; // 3
std::cout << m[45][9 - 1] << std:;endl; // 4


An example of using std::map with std::array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <map>
#include <array>
 
int main()
{
 
        std::map<unsigned int, std::array<unsigned int, 10>> m;
 
        auto it = m[25].begin();
        for ( unsigned int x : { 9, 2, 4, 1, 9, 6, 8, 9, 8, 3 } ) *it++ = x;
        
        for ( auto x : m[25] ) std::cout << x << ' ';
        std::cout << std::endl;
    
}
Thanks for the reply, vlad.

It seems apparent that a multidimensional array is what I'm needing.

Never used std::map or anything of the sort. No big deal. I'll try to figure something out with int result[11][11]; and work from there.

Again, thanks for your input.
Last edited on
Why are you declaring

int result[11][11];

when in your example there are only 10 rows and 10 columns?

Because the element 0 will never get hit. Neither Person level OR skill will never be less than 1.
There is a simple relation between the skil and array indexes. Any index == skill - 1 or vice versa skill == index + 1. So there is no need to declare the array having size 11. It is simpler to set its size to 10 according to the number of skills.
Last edited on
I agree. It doesn't hurt either way, but I see your point totally.
As an aside, in the long run I find it simpler to treat 1D arrays as multi-dimensional arrays:

1
2
my2dArray[(y * width) + x];
my3dArray[((z * width * height) + (y * width)) + x];

Understanding why this works could be of great use to you if ever you need to do more advanced operations with an array since you'll have more insight behind how multi-dimensional arrays work. It can also be used in a situation where a container might not support multiple dimensions (where an alternative would be to make something like vector<vector <int> >).
Topic archived. No new replies allowed.