I am coding 2 dynamic arrays. basically ive done the first part which requires the user to enter rows and columns, i also implemented the functions to print and fill the created arrays. 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.

#include <stdio.h>
#include <stdlib.h>


void fillit(double **, int , int);
void printit(double **, int, int);

int main()
{
   double **m;
   int r, c;

   int i;

   printf("\n How Many Rows? ");
   scanf("%d", &r);

   printf("\n How many Cols ");
   scanf("%d", &c);

   m=(double **)malloc(r*sizeof(double *));

   for(i=0; i<r; i++)
      m[i]=(double *)malloc(c*sizeof(double));
   fillit(m, r, c);
   printit(m, r, c);

   return 0;

}

void printit(double **m, int r, int c)
{
   int i, j;

   for(i=0; i<r; i++){
      for(j=0; j<c; j++){
         printf("%.*f,",2, m[i][j]);
      }
      printf("\n");
   }
}

void fillit(double **m, int r, int c)
{
   int i, j;

   for(i=0; i<r; i++){
      for(j=0; j<c; j++){
         m[i][j]=(i*100)+j;
      }
   }
}

Recommended Answers

All 4 Replies

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.

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.

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.