Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-05 Thread Vishal Thanki
Hi Gene,

I was thinking the same thing which you implemented in your first
snippet. But I tried it without using typedef. Following is my code:

#include stdio.h
#include malloc.h

int main()
{
int (*ptr)[4] ;
ptr = (int ((*)[4]))malloc(2*sizeof(int (*)[4]));
if (ptr) {
int i,j,cnt =0;
for (i= 0; i  2;i++)
for (j = 0; j  4; j++)
ptr[i][j] = cnt++;

for (i= 0; i  2;i++)
for (j = 0; j  4; j++)
printf(%d\n,ptr[i][j]);

/* Free will not work, as the above code
 * overwrites the signature used by free
 */
/* free((ptr)); */
}
return 0;
}


This code is buggy, because as soon as the malloc returns, I try to
write some data and in that process, I am overwriting the signature
(which is used by free()), and hence free() call fails. Can you point
out the problem?

Thanks,
Vishal

On Sun, Jul 3, 2011 at 2:12 AM, Gene gene.ress...@gmail.com wrote:
 Unfortunately this invokes undefined behavior, so it may not work for
 all architectures and compilers.  It relies on pointers to int having
 the same alignment as int.

 By far the best way to do this is use C99 features:

 double (*a)[n_cols] = calloc(n_rows, sizeof *a);

 If you don't have C99 and the row length is fixed, then you can
 allocated a 2d array very easily:

 #include stdio.h
 #include stdlib.h

 #define ROW_SIZE 10
 typedef double ROW[ROW_SIZE];

 int main(void)
 {
  int i, j n_rows = 15;
  ROW *array = malloc(n_rows * sizeof(ROW));
  for (i  = 0; i  n_rows; i++)
    for (j = 0; j  ROW_SIZE; j++)
      array[i][j] = 100 * i + j;
  for (i  = 0; i  n_rows; i++) {
    for (j = 0; j  ROW_SIZE; j++)
      printf(%5.0f, array[i][j]);
    printf(\n);
  }
  return 0;
 }

 If you need both array axes to be variable, then the norm is to
 allocate a 1d array and define a macro to allow 2d access:

 #include stdio.h
 #include stdlib.h

 typedef double *ARRAY;
 #define Elt(A, I, J, NCOLS)  ((A)[(I) * (NCOLS) + (J)])

 int no_fixed(void)
 {
  int i, j, n_rows = 15, n_cols = 10;
  ARRAY array = malloc(n_rows * n_cols * sizeof array[0]);
  for (i  = 0; i  n_rows; i++)
    for (j = 0; j  n_cols; j++)
      Elt(array, i, j, n_cols) = 100 * i + j;
  for (i  = 0; i  n_rows; i++) {
    for (j = 0; j  n_cols; j++)
      printf(%5.0f, Elt(array, i, j, n_cols));
    printf(\n);
  }
  return 0;
 }


 On Jul 2, 2:05 pm, vaibhav shukla vaibhav200...@gmail.com wrote:
 @sandeep sir: thnx... good 1 :)





 On Sat, Jul 2, 2011 at 11:32 PM, Sandeep Jain sandeep6...@gmail.com wrote:
  Here's my solution.

  int** allocateMatrix(int m, int n)
  {
   int **rowList = (int**)malloc(sizeof(int)*m*n + sizeof(int*)*m);
   int *colList = (int*)(rowList+m);
   int i;
   for(i=0; im; i++)
   {
     rowList[i] = colList+i*n;
   }
   return rowList;
  }

  And here's the main method to test/understand the allocation.

  int main()
  {
   int m=3, n=4;
   int **mat = allocateMatrix(m,n);
   int i, j, c=0;
   int wordCount= m*n+m; //sizeof(int)*4*5 + sizeof(int*)*4; counting
  words so sizeof is not needed
   int* memMap = (int*)mat;

   // Fill array elements with some values to be able to test
   for(i=0; im; i++)
     for(j=0; jn; j++)
      mat[i][j] = c++;

   printf(\nAddress\t    Value\n);
   for(i=0; iwordCount; i++)
     printf(\n%u\t== %u, memMap+i, memMap[i]);

   getchar();
  }

  --
  You received this message because you are subscribed to the Google Groups
  Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

 --
   best wishes!!
 Vaibhav Shukla- Hide quoted text -

 - Show quoted text -

 --
 You received this message because you are subscribed to the Google Groups 
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to 
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: 2 D array(dynamic allocation)

2011-07-02 Thread Sandeep Jain
Here's my solution.

int** allocateMatrix(int m, int n)
{
  int **rowList = (int**)malloc(sizeof(int)*m*n + sizeof(int*)*m);
  int *colList = (int*)(rowList+m);
  int i;
  for(i=0; im; i++)
  {
rowList[i] = colList+i*n;
  }
  return rowList;
}

And here's the main method to test/understand the allocation.

int main()
{
  int m=3, n=4;
  int **mat = allocateMatrix(m,n);
  int i, j, c=0;
  int wordCount= m*n+m; //sizeof(int)*4*5 + sizeof(int*)*4; counting
words so sizeof is not needed
  int* memMap = (int*)mat;

  // Fill array elements with some values to be able to test
  for(i=0; im; i++)
for(j=0; jn; j++)
 mat[i][j] = c++;

  printf(\nAddress\tValue\n);
  for(i=0; iwordCount; i++)
printf(\n%u\t== %u, memMap+i, memMap[i]);

  getchar();
}

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-02 Thread vaibhav shukla
@sandeep sir: thnx... good 1 :)

On Sat, Jul 2, 2011 at 11:32 PM, Sandeep Jain sandeep6...@gmail.com wrote:

 Here's my solution.

 int** allocateMatrix(int m, int n)
 {
  int **rowList = (int**)malloc(sizeof(int)*m*n + sizeof(int*)*m);
  int *colList = (int*)(rowList+m);
  int i;
  for(i=0; im; i++)
  {
rowList[i] = colList+i*n;
  }
  return rowList;
 }

 And here's the main method to test/understand the allocation.

 int main()
 {
  int m=3, n=4;
  int **mat = allocateMatrix(m,n);
  int i, j, c=0;
  int wordCount= m*n+m; //sizeof(int)*4*5 + sizeof(int*)*4; counting
 words so sizeof is not needed
  int* memMap = (int*)mat;

  // Fill array elements with some values to be able to test
  for(i=0; im; i++)
for(j=0; jn; j++)
 mat[i][j] = c++;

  printf(\nAddress\tValue\n);
  for(i=0; iwordCount; i++)
printf(\n%u\t== %u, memMap+i, memMap[i]);

  getchar();
 }

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
  best wishes!!
Vaibhav Shukla

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: 2 D array(dynamic allocation)

2011-07-02 Thread Gene
Unfortunately this invokes undefined behavior, so it may not work for
all architectures and compilers.  It relies on pointers to int having
the same alignment as int.

By far the best way to do this is use C99 features:

double (*a)[n_cols] = calloc(n_rows, sizeof *a);

If you don't have C99 and the row length is fixed, then you can
allocated a 2d array very easily:

#include stdio.h
#include stdlib.h

#define ROW_SIZE 10
typedef double ROW[ROW_SIZE];

int main(void)
{
  int i, j n_rows = 15;
  ROW *array = malloc(n_rows * sizeof(ROW));
  for (i  = 0; i  n_rows; i++)
for (j = 0; j  ROW_SIZE; j++)
  array[i][j] = 100 * i + j;
  for (i  = 0; i  n_rows; i++) {
for (j = 0; j  ROW_SIZE; j++)
  printf(%5.0f, array[i][j]);
printf(\n);
  }
  return 0;
}

If you need both array axes to be variable, then the norm is to
allocate a 1d array and define a macro to allow 2d access:

#include stdio.h
#include stdlib.h

typedef double *ARRAY;
#define Elt(A, I, J, NCOLS)  ((A)[(I) * (NCOLS) + (J)])

int no_fixed(void)
{
  int i, j, n_rows = 15, n_cols = 10;
  ARRAY array = malloc(n_rows * n_cols * sizeof array[0]);
  for (i  = 0; i  n_rows; i++)
for (j = 0; j  n_cols; j++)
  Elt(array, i, j, n_cols) = 100 * i + j;
  for (i  = 0; i  n_rows; i++) {
for (j = 0; j  n_cols; j++)
  printf(%5.0f, Elt(array, i, j, n_cols));
printf(\n);
  }
  return 0;
}


On Jul 2, 2:05 pm, vaibhav shukla vaibhav200...@gmail.com wrote:
 @sandeep sir: thnx... good 1 :)





 On Sat, Jul 2, 2011 at 11:32 PM, Sandeep Jain sandeep6...@gmail.com wrote:
  Here's my solution.

  int** allocateMatrix(int m, int n)
  {
   int **rowList = (int**)malloc(sizeof(int)*m*n + sizeof(int*)*m);
   int *colList = (int*)(rowList+m);
   int i;
   for(i=0; im; i++)
   {
     rowList[i] = colList+i*n;
   }
   return rowList;
  }

  And here's the main method to test/understand the allocation.

  int main()
  {
   int m=3, n=4;
   int **mat = allocateMatrix(m,n);
   int i, j, c=0;
   int wordCount= m*n+m; //sizeof(int)*4*5 + sizeof(int*)*4; counting
  words so sizeof is not needed
   int* memMap = (int*)mat;

   // Fill array elements with some values to be able to test
   for(i=0; im; i++)
     for(j=0; jn; j++)
      mat[i][j] = c++;

   printf(\nAddress\t    Value\n);
   for(i=0; iwordCount; i++)
     printf(\n%u\t== %u, memMap+i, memMap[i]);

   getchar();
  }

  --
  You received this message because you are subscribed to the Google Groups
  Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

 --
   best wishes!!
 Vaibhav Shukla- Hide quoted text -

 - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-01 Thread Anika Jain
do we need to add m in each a[i]?? shudnt it be a[i] = a+ n*i?? i think i m
doing a silly mistake.. bt plz tell me..

On Thu, Jun 30, 2011 at 7:41 AM, Dave dave_and_da...@juno.com wrote:

 @Rizwan: Not completely. What if ROW in your code is a variable (not a
 constant) that is not known until run time? Then you need to
 dynamically allocate space for your arr2D as well. So, contradicting
 my earlier No response, maybe something like this would work to
 allocate an array a with m rows and n columns:

 int* a = (int*)malloc( m * ( sizeof(int*) + n * sizeof(int) ) );
 for( i = 0 ; i  m ; ++i )
a[i] = a + m + i * n;

 Dave

 On Jun 29, 4:41 pm, rizwan hudda rizwanhu...@gmail.com wrote:
  I have solved this using one malloc find the code inhttp://
 ideone.com/BV9Kj
 
 
 
 
 
  On Thu, Jun 30, 2011 at 1:20 AM, Piyush Sinha ecstasy.piy...@gmail.com
 wrote:
   ohh sorrymy bad...i didnt read the whole question..i just read the
   subject...:P
 
   i think its not possible if u want other than hary's solution...
 
   On 6/30/11, Apoorve Mohan apoorvemo...@gmail.com wrote:
   @piyush: only one call to malloc...ur sol has 2
 
   On Thu, Jun 30, 2011 at 12:58 AM, Piyush Sinha
   ecstasy.piy...@gmail.comwrote:
 
   int **p;
   p = (int **)malloc(sizeof(int *)*row);
   for(i = 0;irow;i++)
 p[i] = (int *)malloc(sizeof(int)*column);
 
   On 6/30/11, Apoorve Mohan apoorvemo...@gmail.com wrote:
though thankx :)
 
On Thu, Jun 30, 2011 at 12:44 AM, Apoorve Mohan
apoorvemo...@gmail.comwrote:
 
@above: man i need a 2d array not a 1d array...
 
On Thu, Jun 30, 2011 at 12:38 AM, hary rathor
harry.rat...@gmail.comwrote:
 
#includestdlib.h
 
int main ()
{
int *mat;
int i,j;
int ROW=4;
int COL=3;
int k=0;
mat=(int *)malloc(ROW*COL*sizeof(int));
 
   for(i=0;iROW;i++)
   for(j=0;jCOL;j++)
   mat[i*COL+j]=++k;
 
   for(i=0;iROW;i++)
   for(j=0;jCOL;j++)
   printf(%d,,mat[i*COL+j]);
 
return 0;
 
}
 
 --
You received this message because you are subscribed to the
 Google
   Groups
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at
   http://groups.google.com/group/algogeeks?hl=en.
 
--
regards
 
Apoorve Mohan
 
--
regards
 
Apoorve Mohan
 
--
You received this message because you are subscribed to the Google
Groups
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at
   http://groups.google.com/group/algogeeks?hl=en.
 
   --
   *Piyush Sinha*
   *IIIT, Allahabad*
   *+91-8792136657*
   *+91-7483122727*
   *https://www.facebook.com/profile.php?id=10655377926*
 
   --
   You received this message because you are subscribed to the Google
 Groups
   Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
   --
   regards
 
   Apoorve Mohan
 
   --
   You received this message because you are subscribed to the Google
 Groups
   Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
   --
   *Piyush Sinha*
   *IIIT, Allahabad*
   *+91-8792136657*
   *+91-7483122727*
   *https://www.facebook.com/profile.php?id=10655377926*
 
   --
   You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group athttp://
 groups.google.com/group/algogeeks?hl=en.
 
  --
  Thanks and regards
  Rizwan A Huddahttp://sites.google.com/site/rizwanhudda2- Hide quoted
 text -
 
  - Show quoted text -

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 

Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-01 Thread sourabh jakhar
#includestdio.h
int main()
{
int i,j;
int ct=0;
int *a[3];
for( i=0;i3;i++)
{
a[i]=(int **)malloc(sizeof(int)*4);
for(i=0;i3;i++){
for(j=0;j4;j++)
a[i][j]=ct++;
printf(%d,a[i][j]);
}
}
}
On Sat, Jul 2, 2011 at 12:13 AM, Anika Jain anika.jai...@gmail.com wrote:

 do we need to add m in each a[i]?? shudnt it be a[i] = a+ n*i?? i think i m
 doing a silly mistake.. bt plz tell me..


 On Thu, Jun 30, 2011 at 7:41 AM, Dave dave_and_da...@juno.com wrote:

 @Rizwan: Not completely. What if ROW in your code is a variable (not a
 constant) that is not known until run time? Then you need to
 dynamically allocate space for your arr2D as well. So, contradicting
 my earlier No response, maybe something like this would work to
 allocate an array a with m rows and n columns:

 int* a = (int*)malloc( m * ( sizeof(int*) + n * sizeof(int) ) );
 for( i = 0 ; i  m ; ++i )
a[i] = a + m + i * n;

 Dave

 On Jun 29, 4:41 pm, rizwan hudda rizwanhu...@gmail.com wrote:
  I have solved this using one malloc find the code inhttp://
 ideone.com/BV9Kj
 
 
 
 
 
  On Thu, Jun 30, 2011 at 1:20 AM, Piyush Sinha ecstasy.piy...@gmail.com
 wrote:
   ohh sorrymy bad...i didnt read the whole question..i just read the
   subject...:P
 
   i think its not possible if u want other than hary's solution...
 
   On 6/30/11, Apoorve Mohan apoorvemo...@gmail.com wrote:
   @piyush: only one call to malloc...ur sol has 2
 
   On Thu, Jun 30, 2011 at 12:58 AM, Piyush Sinha
   ecstasy.piy...@gmail.comwrote:
 
   int **p;
   p = (int **)malloc(sizeof(int *)*row);
   for(i = 0;irow;i++)
 p[i] = (int *)malloc(sizeof(int)*column);
 
   On 6/30/11, Apoorve Mohan apoorvemo...@gmail.com wrote:
though thankx :)
 
On Thu, Jun 30, 2011 at 12:44 AM, Apoorve Mohan
apoorvemo...@gmail.comwrote:
 
@above: man i need a 2d array not a 1d array...
 
On Thu, Jun 30, 2011 at 12:38 AM, hary rathor
harry.rat...@gmail.comwrote:
 
#includestdlib.h
 
int main ()
{
int *mat;
int i,j;
int ROW=4;
int COL=3;
int k=0;
mat=(int *)malloc(ROW*COL*sizeof(int));
 
   for(i=0;iROW;i++)
   for(j=0;jCOL;j++)
   mat[i*COL+j]=++k;
 
   for(i=0;iROW;i++)
   for(j=0;jCOL;j++)
   printf(%d,,mat[i*COL+j]);
 
return 0;
 
}
 
 --
You received this message because you are subscribed to the
 Google
   Groups
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com
 .
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at
   http://groups.google.com/group/algogeeks?hl=en.
 
--
regards
 
Apoorve Mohan
 
--
regards
 
Apoorve Mohan
 
--
You received this message because you are subscribed to the Google
Groups
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at
   http://groups.google.com/group/algogeeks?hl=en.
 
   --
   *Piyush Sinha*
   *IIIT, Allahabad*
   *+91-8792136657*
   *+91-7483122727*
   *https://www.facebook.com/profile.php?id=10655377926*
 
   --
   You received this message because you are subscribed to the Google
 Groups
   Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
   --
   regards
 
   Apoorve Mohan
 
   --
   You received this message because you are subscribed to the Google
 Groups
   Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
   --
   *Piyush Sinha*
   *IIIT, Allahabad*
   *+91-8792136657*
   *+91-7483122727*
   *https://www.facebook.com/profile.php?id=10655377926*
 
   --
   You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group athttp://
 groups.google.com/group/algogeeks?hl=en.
 
  --
  Thanks and regards
  Rizwan A Huddahttp://sites.google.com/site/rizwanhudda2- Hide quoted
 text -
 
  - Show quoted text -

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 

[algogeeks] Re: 2 D array(dynamic allocation)

2011-07-01 Thread sravanreddy001
@saurabh.. No man.. it involves variable number of calls.. even if its 
present only once in code.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/2VonkRB6WMEJ.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-01 Thread sourabh jakhar
#includestdio.h
int main()
{
int i,j;
int ct=0;
int *a[3];
for( i=0;i3;i++)
{
a[i]=(int **)malloc(sizeof(int)*4);
}
for(i=0;i3;i++){
for(j=0;j4;j++)
a[i][j]=ct++;
printf(%d,a[i][j]);
}
On Sat, Jul 2, 2011 at 2:27 AM, sravanreddy001 sravanreddy...@gmail.comwrote:

 @saurabh.. No man.. it involves variable number of calls.. even if its
 present only once in code.

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/2VonkRB6WMEJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
SOURABH JAKHAR,(CSE)(3 year)
ROOM NO 167 ,
TILAK,HOSTEL
'MNNIT ALLAHABAD

The Law of Win says, Let's not do it your way or my way; let's do it the
best way.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: 2 D array(dynamic allocation)

2011-06-29 Thread Dave
Apoorve: No.

Dave

On Jun 29, 2:34 pm, Apoorve Mohan apoorvemo...@gmail.com wrote:
 @piyush: only one call to malloc...ur sol has 2

 On Thu, Jun 30, 2011 at 12:58 AM, Piyush Sinha 
 ecstasy.piy...@gmail.comwrote:





  int **p;
  p = (int **)malloc(sizeof(int *)*row);
  for(i = 0;irow;i++)
        p[i] = (int *)malloc(sizeof(int)*column);

  On 6/30/11, Apoorve Mohan apoorvemo...@gmail.com wrote:
   though thankx :)

   On Thu, Jun 30, 2011 at 12:44 AM, Apoorve Mohan
   apoorvemo...@gmail.comwrote:

   @above: man i need a 2d array not a 1d array...

   On Thu, Jun 30, 2011 at 12:38 AM, hary rathor
   harry.rat...@gmail.comwrote:

   #includestdlib.h

   int main ()
   {
       int *mat;
       int i,j;
       int ROW=4;
       int COL=3;
       int k=0;
       mat=(int *)malloc(ROW*COL*sizeof(int));

      for(i=0;iROW;i++)
      for(j=0;jCOL;j++)
      mat[i*COL+j]=++k;

      for(i=0;iROW;i++)
      for(j=0;jCOL;j++)
      printf(%d,,mat[i*COL+j]);

       return 0;

   }

    --
   You received this message because you are subscribed to the Google
  Groups
   Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.

   --
   regards

   Apoorve Mohan

   --
   regards

   Apoorve Mohan

   --
   You received this message because you are subscribed to the Google Groups
   Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.

  --
  *Piyush Sinha*
  *IIIT, Allahabad*
  *+91-8792136657*
  *+91-7483122727*
  *https://www.facebook.com/profile.php?id=10655377926*

  --
  You received this message because you are subscribed to the Google Groups
  Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

 --
 regards

 Apoorve Mohan- Hide quoted text -

 - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: 2 D array(dynamic allocation)

2011-06-29 Thread Dave
@Rizwan: Not completely. What if ROW in your code is a variable (not a
constant) that is not known until run time? Then you need to
dynamically allocate space for your arr2D as well. So, contradicting
my earlier No response, maybe something like this would work to
allocate an array a with m rows and n columns:

int* a = (int*)malloc( m * ( sizeof(int*) + n * sizeof(int) ) );
for( i = 0 ; i  m ; ++i )
a[i] = a + m + i * n;

Dave

On Jun 29, 4:41 pm, rizwan hudda rizwanhu...@gmail.com wrote:
 I have solved this using one malloc find the code inhttp://ideone.com/BV9Kj





 On Thu, Jun 30, 2011 at 1:20 AM, Piyush Sinha ecstasy.piy...@gmail.com 
 wrote:
  ohh sorrymy bad...i didnt read the whole question..i just read the
  subject...:P

  i think its not possible if u want other than hary's solution...

  On 6/30/11, Apoorve Mohan apoorvemo...@gmail.com wrote:
  @piyush: only one call to malloc...ur sol has 2

  On Thu, Jun 30, 2011 at 12:58 AM, Piyush Sinha
  ecstasy.piy...@gmail.comwrote:

  int **p;
  p = (int **)malloc(sizeof(int *)*row);
  for(i = 0;irow;i++)
        p[i] = (int *)malloc(sizeof(int)*column);

  On 6/30/11, Apoorve Mohan apoorvemo...@gmail.com wrote:
   though thankx :)

   On Thu, Jun 30, 2011 at 12:44 AM, Apoorve Mohan
   apoorvemo...@gmail.comwrote:

   @above: man i need a 2d array not a 1d array...

   On Thu, Jun 30, 2011 at 12:38 AM, hary rathor
   harry.rat...@gmail.comwrote:

   #includestdlib.h

   int main ()
   {
       int *mat;
       int i,j;
       int ROW=4;
       int COL=3;
       int k=0;
       mat=(int *)malloc(ROW*COL*sizeof(int));

      for(i=0;iROW;i++)
      for(j=0;jCOL;j++)
      mat[i*COL+j]=++k;

      for(i=0;iROW;i++)
      for(j=0;jCOL;j++)
      printf(%d,,mat[i*COL+j]);

       return 0;

   }

    --
   You received this message because you are subscribed to the Google
  Groups
   Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.

   --
   regards

   Apoorve Mohan

   --
   regards

   Apoorve Mohan

   --
   You received this message because you are subscribed to the Google
   Groups
   Algorithm Geeks group.
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.

  --
  *Piyush Sinha*
  *IIIT, Allahabad*
  *+91-8792136657*
  *+91-7483122727*
  *https://www.facebook.com/profile.php?id=10655377926*

  --
  You received this message because you are subscribed to the Google Groups
  Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

  --
  regards

  Apoorve Mohan

  --
  You received this message because you are subscribed to the Google Groups
  Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

  --
  *Piyush Sinha*
  *IIIT, Allahabad*
  *+91-8792136657*
  *+91-7483122727*
  *https://www.facebook.com/profile.php?id=10655377926*

  --
  You received this message because you are subscribed to the Google Groups 
  Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to 
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group 
  athttp://groups.google.com/group/algogeeks?hl=en.

 --
 Thanks and regards
 Rizwan A Huddahttp://sites.google.com/site/rizwanhudda2- Hide quoted text -

 - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.