Hi I stumbled upon a question in my textbook that states: 'Write a function that makes a copy of the contents of a 3D array of integers. The function should support any 3D array size.'
After discussing it with my lecturer he specified that the prototype of the function should look something similar to this (this is 2D, I need 3D).
int sum2d(int rows, int cols, int ar[rows][cols]);
Now how I currently coded it is by making everything in the main function and it works like it should, i.e copies all contents etc.
int main()
{
int x,y,z;
printf("Enter x value.
");
scanf("%d", &x);
printf("Enter y value.
");
scanf("%d", &y);
printf("Enter z value.
");
scanf("%d", &z);
int *arrx = malloc(x * sizeof(*arrx));
int *arry = malloc(y * sizeof(*arry));
int *arrz = malloc(z * sizeof(*arrz));
printf("The size of the array is %d.
", x*y*z);
/* 3D array declaration*/
int disp[x][y][z];
int cpydisp[x][y][z];
/*Counter variables for the loop*/
int i, j, k;
for(i=0; i<x; i++) {
for(j=0;j<y;j++) {
for (k = 0; k < z; k++) {
printf("Enter value for disp[%d][%d][%d]:", i, j, k);
scanf("%d", &disp[i][j][k]);
}
}
}
memcpy(cpydisp,disp, sizeof(disp));
//Displaying array elements
printf("Three Dimensional array elements:
");
for(i=0; i<x; i++) {
for(j=0;j<y;j++) {
for (k = 0; k < z; k++) {
printf("%d ", cpydisp[i][j][k]);
}
printf("
");
}
}
}
However this is not correct since I need to implement a function just for copying and I came up with this. Create a function called void array_copy
which practically copies the contents of the array disp
to another array cpydisp
by memcpy. The function array_copy
is then called in the main however this is not working.
int i, j, k;
int x,y,z;
int disp[x][y][z];
int cpydisp[x][y][z];
void array_copy() {
memcpy(cpydisp, disp, sizeof(disp));
//Displaying array elements
printf("Three Dimensional array elements:
");
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
for (k = 0; k < z; k++) {
printf("%d ", cpydisp[i][j][k]);
}
printf("
");
}
}
}
int main()
{
printf("Enter x value.
");
scanf("%d", &x);
printf("Enter y value.
");
scanf("%d", &y);
printf("Enter z value.
");
scanf("%d", &z);
//int *arrx = malloc(x * sizeof(*arrx));
//int *arry = malloc(y * sizeof(*arry));
//int *arrz = malloc(z * sizeof(*arrz));
printf("The size of the array is %d.
", x*y*z);
/* 3D array declaration*/
/*Counter variables for the loop*/
int i, j, k;
for(i=0; i<x; i++) {
for(j=0;j<y;j++) {
for (k = 0; k < z; k++) {
printf("Enter value for disp[%d][%d][%d]:", i, j, k);
scanf("%d", &disp[i][j][k]);
}
}
}
array_copy();
}
Any thoughts please on how I can get it fixed, since I can't seem to understand what's wrong with it when it clearly can take any size by the user imputing the size he wants prior to having anything started.
Thanks in Advance
Edit:
#include<stdio.h>
#include <string.h>
int x,y,z;
int i, j, k;
void user_input(){
printf("Enter x value.
");
scanf("%d", &x);
printf("Enter y value.
");
scanf("%d", &y);
printf("Enter z value.
");
scanf("%d", &z);
}
void array_copy() {
int disp[x][y][z];
int cpydisp[x][y][z];
memcpy(cpydisp, disp, sizeof(disp));
//Displaying array elements
printf("Three Dimensional array elements:
");
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
for (k = 0; k < z; k++) {
printf("%d ", cpydisp[i][j][k]);
}
printf("
");
}
}
}
int main()
{
user_input();
int disp[x][y][z];
int cpydisp[x][y][z];
printf("The size of the array is %d.
", x*y*z);
/* 3D array declaration*/
/*Counter variables for the loop*/
for(i=0; i<x; i++) {
for(j=0;j<y;j++) {
for (k = 0; k < z; k++) {
printf("Enter value for disp[%d][%d][%d]:", i, j, k);
scanf("%d", &disp[i][j][k]);
}
}
}
array_copy();
}
I have retried it this way now something is being outputted rather than the second attempt however the output is just random numbers. i.e:
Enter x value.
1
Enter y value.
2
Enter z value.
3
The size of the array is 6.
Enter value for disp[0][0][0]:1
1
Enter value for disp[0][0][1]:2
2
Enter value for disp[0][0][2]:3
3
Enter value for disp[0][1][0]:4
4
Enter value for disp[0][1][1]:5
5
Enter value for disp[0][1][2]:6
6
Three Dimensional array elements:
797168 0 6421264
0 3 0
Process finished with exit code 0
The correct output should be 1,2,3,4,5,6
Thanks again
See Question&Answers more detail:
os