|
-
December 14th, 2006, 11:39 PM
#1
How to do this
Dear All,
I am new to C/C++. I have written a small program for copying
2 different 2D array into a single 3D array in a most stupid
way. Certainly there will be diiferent ways to solve it.
I need to reduce no of lines of the program.
Please suggest how to do this
//program for copying
2 different 2D array into a single 3D array
#include <string.h>
#include <stdio.h>
int *a; // 3D array
int *b; // 1st 2D array
int *c; // second array
void main()
{
a = new int[2*3*3];
b = new int[3*3];
c = new int[3*3];
int i,j,k;
//memset((void*)c,0,3*3*sizeof(int));
//memset((void*)b,1,3*3*sizeof(int));
// INitializing two arrays
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
c[i*3+j] = 2;
//printf("%d\n",c[i*3+j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
b[i*3+j] = 1;
//printf("%d\n",b[i*3+j]);
}
//memcpy((void*)b,(void*)c,3*3*sizeof(int));
//copying the first 2D array into 3D array
for(i=0;i<1;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[k*3+j*3+i] = c[i*3+j];
printf("%d\n",/*c[i*3+j],*/a[k*3+j*3+i]);
}
//copying the second 2D array into the 3D array
for(i=1;i<2;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[k*3+j*3+i] = b[i*3+j];
printf("%d\n",/*c[i*3+j],*/a[k*3+j*3+i]);
}
//Displaying the 3D array
for(i=0;i<2;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
//printf("%d\n",a[k*3+j*3+i]);
}
}
Thanks in advance
RAMACHANDRAN LAKSMI NARAYAN
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|