Sub0Zero1990
Sub0Zero1990

Reputation: 27

Converting a 2D array into an array of same size, but shaped as a 1D?

I am trying to convert the multidimensional array table into a monodimensional one without creating 2 monodimensional arrays. How can i do it?

#include <stdio.h>
#include <stdbool.h>
#define ROWS 100
#define COLUMNS 100
int matrice[ROWS][COLUMNS];
int i = 0;
int j = 0;
int rows = 0;
int columns = 0;
void table_creation(void);
void insert_data(void);

int main() {
table_creation();
i = 0;
j = 0;
while (i < rows) {
    j = 0;
    while (j < columns) {
        printf("\t%d ", table[i][j]);
        j++;
    }
    printf("\n");
    i++;
}
i = 0;
j = 0;
while (i < rows) {
    j = 0;
    while (j < columns) {
        printf("\t%d ", table[i][j]);
        j++;
    }
    printf("\n");
    i++;
}
}

void table_creation(){
i = 0;
j = 0;
do{
    printf("How many rows do you want?\n");
    scanf("%d", &rows);
} while (rows > ROWS || rows < 0);
do{
    printf("How many columns do you want?\n");
    scanf("%d", &columns);
} while (columns > COLUMNS || columns < 0);
insert_data();
}

void insert_data(){
i = 0;
j = 0;
printf("\nInsert numbers\n");
while (i < rows) {
    j = 0;
    printf("\n");
    while (j < columns) {
        printf("Insert the element in %d row and %d column\n", i ,j);
        scanf("%d", &table[i][j]);
        j++;
    }
    i++;
    printf("\n");
}
}

The output of this function are the number that are in the table. Now i want the same output but with a monodimensional array

Upvotes: 0

Views: 85

Answers (1)

ryyker
ryyker

Reputation: 23208

Addressing only the title question:

"convert the multidimensional array table into a monodimensional one without creating 2 monodimensional arrays. How can i do it?"

Creating a 1D to match size of a 2D is easily doable, and updating the new array with contents of 2D is just as easy. both can be done in a variety of ways. Following shows some of them:

Given the following, creating a 1D can be done in multiple ways:

#define ROWS 5 (using smaller sizes for illustration)
#define COLS 3

int main(void)
{
    //simplest: 
    int matrix1[ROWS*COLS] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

    //less simple, using variable length array:
    int matrice[ROWS][COLS];
    size_t elements = sizeof(matrice)/sizeof(int);//give number of int memory locations in 2D matrix
    
    //use count of int memory locations to create matrix of same size, but of different shape
    int newMatrix[elements];//note VLA cannot be initialized
    memset(newMatrix, 0, sizeof(newMatrix));//...its done in another statement. 

    return 0;
}

It is noteworthy that the memory layout for a 2D matrix such as int matrice[ROWS][COLS]; is laid out in memory identically to that of a 1D matrix of the same size, eg int matrice[ROWS*COLS];. Both exist in memory as 1 contiguous and sequential block. So matrix1, matrice and newMatrix all conceptually look like this:

         | | | | | | | | | | | | | | | | 
 Rows    0     1     2     3     4      (each with 3 columns)            

Also good to about pointer offset notation i.e. that the expression *(newMatrix + 0) = val; is equivalent to normal array notation: newMatrix[0] = val;

And using this arrangement, and pointer offset notation, a 2D can be easily copied into a 1D using this, which copies 2D matrix1 to 1D newMatrix:

for(int i=0;i<elements;i++)
{
     *(newMatrix + i) = *(matrix1 +i);
}

But even easier is to use memcpy();. The following copies 2D matrix1 to 1D newMatrix

memcpy(newMatrix, matrix1, elements*sizeof(int));

Each of these methods copy the contents of memory with exactly the same result, i.e. in the same order, row 0 appended by row 1, ..., appended by row 4

Note, VLAs are available in compilers compliant with C99, and optional other others since C99. More on variable length array

Upvotes: 2

Related Questions