## Featured Replies in this Discussion

- by mvmalderen... Well, the first thing you need to figure out is: how many elements should the single dimensional array contain? That's a simple one: just as many as there are in the two dimensional array. How do we get that amount of elements? By using this formula: ... . Following from that formula is the formula which describes how many memory you should allocate using malloc(): ... . Once you've allocated the memory for the…

now the next task is to create a SINGLE dimensional array with enough size to fit the 2d array and load the 2d array there in row major order.

Well, the first thing you need to figure out is: how many elements should the single dimensional array contain? That's a simple one: just as many as there are in the two dimensional array. How do we get that amount of elements? By using this formula: `[I]rows_in_2d_array[/I] [B]*[/B] [I]cols_in_2d_array[/I]`

.

Following from that formula is the formula which describes how many memory you should allocate using malloc(): `[I]rows_in_2d_array[/I] [B]*[/B] [I]cols_in_2d_array[/I] * [B]sizeof[/B]( [I]type_of_2d_array[/I] )`

.

Once you've allocated the memory for the one dimensional array, the only thing left to you is copying the elements from the two dimensional array to the one dimensional array. The process is as follows: loop through all the elements of the two dimensional array, then you each time copy the current element from the two dimensional array to the one dimensional array. (*Hint*: use a variable to keep track of the index where the next element of the two dimensional array should be copied to).

[EDIT]

Don't forget to free the memory you've allocated using malloc().

I can't spot any call to free() in your code, so that means: memory leak.

[/EDIT]

One of the rules on this site is: Do not post homework problems expecting a quick answer without showing any effort yourself.

The posted code does not show any effort toward creating a 1D array.

uhm what?

i did all the work, i know how to make 1d array.. you sure you read my question? i have solved most of it, i just want to know how to make the size of the 1d array exactly enough to fit all the values in the 2d array.

Well, the first thing you need to figure out is: how many elements should the single dimensional array contain? That's a simple one: just as many as there are in the two dimensional array. How do we get that amount of elements? By using this formula:

`[I]rows_in_2d_array[/I] [B]*[/B] [I]cols_in_2d_array[/I]`

.

Following from that formula is the formula which describes how many memory you should allocate using malloc():`[I]rows_in_2d_array[/I] [B]*[/B] [I]cols_in_2d_array[/I] * [B]sizeof[/B]( [I]type_of_2d_array[/I] )`

.

Once you've allocated the memory for the one dimensional array, the only thing left to you is copying the elements from the two dimensional array to the one dimensional array. The process is as follows: loop through all the elements of the two dimensional array, then you each time copy the current element from the two dimensional array to the one dimensional array. (Hint: use a variable to keep track of the index where the next element of the two dimensional array should be copied to).[EDIT]

Don't forget to free the memory you've allocated using malloc().

I can't spot any call to free() in your code, so that means: memory leak.

[/EDIT]

Thanks, ill go from here.