Hi,

I am trying to combine two arrays to a multidim one.
I have two int arrays stat[] and indexes[].
So the first looks like this 34,56,78,etc holding hundreds of numbers.

The second looks like this 0,1,2,3,4,5 etc.

I want to end with a multidim array like this 34,0 56,1 etc
I want to sort the first dimension while keeping the second dimension locked and staying with its stat.

I know how to create a multidim array, I'm confused about how to fill the values.

Recommended Answers

All 7 Replies

Multi dimensional array is collection row and column.
Its all upon you how would like manage your both array in single multi dimensional array, I mean to say row wise or column, how would you like to create array at run time or static.

int[,] my2Darray = new int[stat.Length, 2];
for (int i = 0; i < stat.Length; i++) {
    my2Darray[i,0] = stat[i];
    my2Darray[i,1] = indexes[i];
}

int n = stat.Length;        // Shell sort
int inc = n / 2;
while (int > 0) {
    for (int i = inc; i < n; i++) {
        int[] temp = new int[2];
        temp[0] = my2Darray[i,0];
        temp[1] = my2Darray[i,1];
        int j = i;
        while (j >= inc && my2Darray[j - inc, 0] > temp[0]) {
            my2Darray[j,0] = my2Darray[j - inc, 0];
            my2Darray[j,1] = my2Darray[j - inc, 1];
            j -= inc;
        }
        my2Darray[j,0] = temp[0];
        my2Darray[j,1] = temp[1];
    }
    inc = (int)(inc / 2.2);
}

This calls for a Dictionary

static void Main(string[] args)
        {
            const int ArLen = 5;

            Dictionary<int, int> Statist = new Dictionary<int, int>();                     
            var Stat = new int[ArLen]{34, 35, 43, 42, 18};
            int[] indexes = new int[ArLen]{0, 1, 2, 3, 4};

            for (int i = 0; i < ArLen; i++)
            {
                Statist.Add(Stat[i],indexes[i]);                
            }
            //more code here
        }

Look here for more details:http://msdn.microsoft.com/en-us/library/3eayzh46(VS.80).aspx

The only problem with a dictionary is that the first values might not be unique.

Thanks to all,
Great solutions.

The only problem with a dictionary is that the first values might not be unique.

Could you elaborate on that, because as I see it the OP said nothing about uniqueness of his first values.
In fact a Dictionary was the first thing that popped up.
I would probably do it like this (all comments appreciated :) )

class Program
    {
        struct OrderedPair
        {
            public int a;
            public int b;
        };

        static void Main(string[] args)
        {            
            const int ArLen = 5;
            
            List<OrderedPair> Statist = new List<OrderedPair>();
            var Stat = new int[ArLen] { 34, 35, 43, 42, 18 };
            int[] indexes = new int[ArLen] { 0, 1, 2, 3, 4 };
            OrderedPair OP;

            for (int i = 0; i < ArLen; i++)
            {
                OP.a = Stat[i];
                OP.b = indexes[i];
                Statist.Add(OP);
            }
            //more code here
        }
    }

You are correct, he didn't say anything about the uniqueness. My first thought was to use SortedDictionary, but since he didn't say they were unique, that might cause a problem. So I went with the 2d array approach.

I try not to make assumptions on input data that I know will cause issues unless they are a special case (unique being special).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.