I was instructed to copy all elements from my 2d array, matrix2D
, to my 3d array matrix3D
using a single memcpy
command and then print out the 3d array (separating elements with a comma, rows with a semicolon and each 2d matrix with a new line). Before that, the program takes in user input for dimensions of the 3d matrix, and then all the data to go into the matrix (floats). My functions malloc2d
and malloc3d
are working correctly and I am getting no segmentation faults, but when I attempt to memcpy
the data and then print the 3d matrix, nothing is printing. Here is the code:
#include "utilities.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
unsigned int dim1,dim2,dim3;
printf("Dimensionality of 3-D matrix ===>");
scanf("%u",&dim1); //takes user input for dimensions
scanf("%u",&dim2); //takes user input for dimensions
scanf("%u",&dim3); //takes user input for dimensions
//allocating memory for 3d, 2d and 1d matrices
float*** matrix3D = malloc3D((unsigned int)(dim1), (unsigned int)(dim2), (unsigned int)(dim3), sizeof(float));
float** matrix2D = malloc2D(dim1, dim2*dim3, sizeof(float));
float* matrix1D = malloc(dim1*dim2*dim3);
for(int i = 0; i<(dim1*dim2*dim3); i++){ //taking user input floats
fscanf(stdin, "%f", &matrix1D[i]);
}//for
for(int i = 0; i<(dim1*dim2*dim3); i++){//writing contents of 1d matrix to 2d matrix
matrix2D[i/(dim2*dim3)][i%(dim2*dim3)] = matrix1D[i];
}//for
for (int i = 0; i < dim1; i++){//printing out 2d array as asked
for (int j = 0; j < (dim2*dim3); j++){
printf("%0.2f", matrix2D[i][j]);
if(j==((dim2*dim3)-1)){
printf(";");
}//if
else{
printf(",");
}//else
}//for
}//for
printf("\n"); //new line for readability
memcpy(matrix3D, matrix2D, dim1*dim2*dim3*sizeof(float));
for (int i = 0; i < dim1; i++){//printing out 3d array as asked
for (int j = 0; j < dim2; j++){
for(int k = 0; k < dim3; k++){
printf("%0.2f", matrix3D[i][j][k]);
if(j==((dim2)-1)){
printf(";");
}//if
else if(k==((dim3)-1)){
printf("\n");
}//else if
else{
printf(",");
}//else
}//for
}//for
}//for
free3D(matrix3D);
free2D(matrix2D);
free(matrix1D);
} //main
The example input and output given is (I have everything printing except the last 2 lines - the 3d matrix):
Here are malloc2D, malloc3D, free2D and free3D. I was under the impression they were right because they were given to us after none of us were able to get it.
void** malloc2D(size_t rows, size_t cols, size_t sizeOfType){
void* block = malloc(sizeOfType * rows * cols);
void** matrix = malloc(sizeof(void*) * rows);
for (int row = 0; row < rows; ++row) {
matrix[row] = block + cols * row * sizeOfType;
}//for
return matrix;
}//malloc2D
void free2D(void*** matrix){
free((*matrix)[0]);
free((*matrix));
*matrix = NULL;
}//free2D
void*** malloc3D(size_t depth, size_t rows, size_t cols, size_t sizeOfType){
void* block = malloc(sizeOfType * rows * cols * depth);
void** matrix = malloc(sizeof(void*) * rows * depth);
void*** matrix2 = malloc(sizeof(void**) * depth);
for (int depth1 = 0; depth1 < depth; ++depth1) {
matrix2[depth1] = (void**)(matrix + depth * rows * sizeof(void**));
for(int row = 0; row < rows; ++row){
matrix[depth1 * rows + row] = block + (depth1 * rows + row) * cols * sizeOfType;
}//for
}//for
return matrix2;
}//malloc3D
void free3D(void**** matrix){
free((*matrix)[0][0]);
free((*matrix)[0]);
free((*matrix));
*matrix = NULL;
}//free3D