-
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
-
Re: How to do this
Why not memcpy the arrays? As long as their data types are POD, there's not problem. Otherwise you should use std::copy.
Have you made any progress on this so far?
Note that nested loops can get very slow at level 3 and up, so you should avoid that - especially if the inner loop were to be more complicated.
-Andy
-
Re: How to do this
Dear sir,
Thanks a lot. Memcpy is not working in my case.
How to rewrite the code using memcpy.
Thanks in advance
-
Re: How to do this
1) Use [ code ][ /code ] tags when posting source code.
2) Your header files are incorrect. Standard library headers with .h are not used anymore.
(Though I see you use printf(), so this is probably some old C code which allows them)
Should be...
Code:
#include <cstring>
#include <cstdio>
3) Also, don't forget to use the standard namespace when you change the header files
Code:
using namespace std;
4) main() return an int type, not void. Using void is incorrect.
-
Re: How to do this
You code does not work. Have you tested it? The indexing is totally flawed. This is a working version with a little changes. Try using functions further to it:
Code:
#include <iostream>
using namespace std;
int main()
{
int *a; // 3D array
int *b; // 1st 2D array
int *c; // second array
a = new int[2*3*3];
b = new int[3*3];
c = new int[3*3];
int i,j,k;
// INitializing two arrays
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i*3+j] = i*3+j+2;
cout << c[i*3+j] << "\t";
}
cout << "\n";
}
cout << "\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i*3+j] = i*3+j+1;
cout << b[i*3+j] << "\t";
}
cout << "\n";
}
cout << "\n";
//copying the first 2D array into 3D array
i=0;
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
a[i*9+j*3+k] = c[j*3+k];
}
}
//copying the second 2D array into the 3D array
i=1;
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
a[i*9+j*3+k] = b[j*3+k];
}
}
//Displaying the 3D array
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
cout << a[i*9+j*3+k] << "\t";
}
cout << "\n";
}
//cout << "\n";
}
cout << "\n";
return 0;
}
The copy code (shown in italics above) can be reduced to just this much:
Code:
std::copy(c, c+9, a);
std::copy(b, b+9, a+9);
You would need to include <algorithm>
Lastly, the arrays you have are one dimensional, not 2-dimensional or 3-dimensional as you're thinking. However, that can be interpreted as a 2-d array at most. The third one "a" is a 2-d array only which is what would have been a result of merge between "b" and "c".
Take a look at the how to encapsulate a 2-d array and work with them. You can add a member function merge() to it in order to achieve the functionality you tried getting here. Here's the link: