Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

arrays - CodeSignal problem. I want to complete the code

I have the following problem. The function printMatrix

Receive an matrix for example:

matrix:
[[0,1,1,2], 
 [0,5,0,0], 
 [2,0,3,3]]

The code that I must use is the following:

// Definition for arrays:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
void printMatrix(arr_arr_integer matrix) 
{

}

As a clue they give me that the number of columns and rows can be determined in the following way.

  int columns = matrix.arr->size; //No.columns
  int rows = matrix.size; //No.rows
  
  //Or

  int columns = matrix.arr[0].size; //No.columns
  int rows = matrix.size; //No.rows

My question lies in how is the rest of the code written so that the previous tracks can work?

That is, for this to work within the function printMatrix

What should you add or modify in your code for the above methods to work?

typedef struct arr_arr_integer {
   int size;
   type *arr;
} arr_arr_integer;

arr_arr_integer alloc_arr_arr_integer(int len) {
arr_arr_integer a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
   return a;
}

void printMatrix(arr_arr_integer matrix) 
{
   int columns = matrix.arr->size; //No.columns
   int rows = matrix.size; //No.rows
   
   //print matrix?
}

int main(int argc, char const *argv[])
{
   //input matrix?

   printMatrix(arr_arr_integer matrix)

   return 0;
}

I repeat. I must use this code strictly

int columns = matrix.arr->size; //No.columns
int rows = matrix.size; //No.rows

The problem is that when I try to use those tracks I get the following compilation error.

error: request for member 'size' in something not a structure or union
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The function alloc_arr_integer allocates a 1D array of integers.

If you need a 2D array, you'll have to call the function multiple times.

Something like:

arr_integer my2Darray[rows];

// Create the 2D array
for (int i = 0; i < rows; ++i)
{
    my2Darray[i] = alloc_arr_integer(columns);
    assert(my2Darray[i].arr != NULL);
}

// Initialize the 2D array
for (int i = 0; i < rows; ++i)
{
    for (int j = 0; j < columns; ++j)
    {
        my2Darray[i].arr[j] = i * 1000 + j;
    }
}

Putting it together:

int main(void)
{
    int rows = 3;
    int columns = 5;
    
    arr_integer my2Darray[rows];

    // Create the 2D array
    for (int i = 0; i < rows; ++i)
    {
        my2Darray[i] = alloc_arr_integer(columns);
        assert(my2Darray[i].arr != NULL);
    }

    //Initialize the 2D array
    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < columns; ++j)
        {
            my2Darray[i].arr[j] = (i + 1) * 1000 + j;
        }
    }

    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < columns; ++j)
        {
            printf("%d ", my2Darray[i].arr[j]);    
        }
        printf("
");
    }

    return 0;
}

OUTPUT

1000 1001 1002 1003 1004 
2000 2001 2002 2003 2004 
3000 3001 3002 3003 3004 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...