1,105,332 Community Members

2d array into 1d array

Member Avatar
notuserfriendly
Junior Poster
103 posts since Jan 2007
Reputation Points: 1 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
      }
   }
}
Member Avatar
UncleLeroy
Junior Poster in Training
56 posts since Mar 2010
Reputation Points: 49 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
-1
 

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.

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
1
 

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]

Member Avatar
notuserfriendly
Junior Poster
103 posts since Jan 2007
Reputation Points: 1 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
notuserfriendly
Junior Poster
103 posts since Jan 2007
Reputation Points: 1 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: