Iterating one dimension array as two dimension array

后端 未结 3 1017
[愿得一人]
[愿得一人] 2020-12-28 09:22

I have,

int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, index = 0;

as shown here, we create the the two dimensional one from the origin.

3条回答
  •  无人及你
    2020-12-28 09:57

    I think this is what your trying to do...convert a one-dim array into a two-dim array.

    //this is just pseudo code...not real syntax
    
    int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    int first_dim = 5;
    int second_dim = 2;
    
    int[first_dim][second_dim] new_array;
    
    for (int fdi = 0; fdi < first_dim; fdi++){
       for (int sdi = 0; sdi < second_dim; sdi++) {
    
          //this is the crux...you're calculating the one dimensional index to access the value
    
          new_array[fdi][sdi] = oneDim[fdi*second_dim + sdi] 
    
       }
    }
    

提交回复
热议问题