Article 53XGD Converting many 1D array to 2D array

Converting many 1D array to 2D array

by
srinietrx
from LinuxQuestions.org on (#53XGD)
Hi,
I am writing a program in which recursive function call with (array and its size as argument) multiple number of times.

For simplicity I am not showing recursive function below instead called from main many times this function Copying1Dto2D(a, 4);.

Converting this array to 2D array using Copying1Dto2D() and accessing Accessing2D()as shown below.
It is working fine as expected.

I declared 2D array Out[5][10] and row_index as global. Which I felt is not that good. Is there is any better way of writing this code?


Code:int Out[5][10];
int row_index;

void Copying1Dto2D(int A[], int size)
{
int i;
for(i=0;i<size;i++)
{
Out[row_index][i] = A[i];
printf("%d\t", A[i]);
}
Out[row_index][i] = NULL;
row_index++;
printf("\n");
}

void Accessing2D(void)
{
int len_rows = sizeof(Out[0])/sizeof(Out[0][0]);
int i=0,j=0;
for(i=0;i< len_rows;i++)
{
for(j=0;Out[i][j]!=NULL;j++)
{
printf("%d\t", Out[i][j]);
}
printf("\n");
}
}

int main()
{
int a[5] = {1,2,3,4};
int b[6] = {7, 6, 3};
int c[6] = {2};
int d[5] = {4, 9};

Copying1Dto2D(a, 4);
Copying1Dto2D(b, 3);
Copying1Dto2D(c, 1);
Copying1Dto2D(d, 2);
printf("----------------------------\n");
Accessing2D();

return 0;
}latest?d=yIl2AUoC8zA latest?i=2VJhHbK0suI:nDAXHXiToCg:F7zBnMy latest?i=2VJhHbK0suI:nDAXHXiToCg:V_sGLiP latest?d=qj6IDK7rITs latest?i=2VJhHbK0suI:nDAXHXiToCg:gIN9vFw2VJhHbK0suI
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments