-1

I want to collect a 3-dimensional matrix using malloc. There is also the matrix printing part but I did not include that.

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int i,j,k,m,n,o;
   float ***A;
   printf("Input dimension for matrix(m,n,o)\n");
   printf("Enter m : ");
   scanf("%d",&m);
   printf("Enter n : ");
   scanf("%d",&n);
   printf("Enter o : ");
   scanf("%d",&o);
   A = (float***)malloc(o*sizeof(float**));
   for(i=0;i<o;i++){
           A[i] = (float**)malloc(m*sizeof(float*));
           for(j=0;j<m;j++){
               A[i][j] = (float*)malloc(n*sizeof(float));
           }
   }
   for(i=0;i<o;i++){
       for(j=0;j<m;j++){
           for(k<0;k<n;k++){
               printf("Input number for (%d,%d,%d) : ",j+1,k+1,i+1);
               scanf("%f",&A[i][j][k]);
           }
       }
   }
   return 0;
}

Program seems to be skipping to the end after I finish input m,n,o, so I cannot enter any value of matrix. I have to use malloc because it was required for the task.

EDIT: everything that is wrong. m is for row,n is for column, and o is for a number of mXn matrix.

3 Answers3

3

Use dynamic Variable-Length Array:

float (*A)[n][m] = calloc(o, sizeof *A);

And free it at the end with

free(A);

More one must passed an address of the scanf-ed object, not it's value. It should be:

scanf("%f", &A[i][j][k])
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
tstanisl
  • 13,520
  • 2
  • 25
  • 40
1

I think all you did is messed up in the for loop while taking the inputs. As per the context, the 3D matrix, I debugged the code and came up with a version that makes sense.

I will go on and point out the mistakes you made one by one.

First, scanf takes the memory address of the variable not the dereferenced version of it.

So, change it from this, scanf("%f",A[i][j][k]); to this scanf("%f",&(A[i][j][k]));. This one would also work scanf("%f",A[i][j] + k);.

In this line, for(k<0;k<n;k++), k is not even initialized. So you are essentially operating on a garbage value. As my mind was already baffled by deciphering the c pointer hell, I couldn't even process the end result. Code that would make more sense is this for(k=0;k<n;k++).

After that, you switched the control logic of the nested for loops.

Replace the variable m in this code for(j=0;j<m;j++) with n. for(j=0;j<n;j++)

Do vice-versa in the next loop. Modify this for(k=0;k<n;k++) to this for(k=0;k<m;k++).

The final code would look like this,

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int i,j,k,m,n,o;
   float ***A;
   printf("Input dimension for matrix(m,n,o)\n");
   printf("Enter m : ");
   scanf("%d",&m);
   printf("Enter n : ");
   scanf("%d",&n);
   printf("Enter o : ");
   scanf("%d",&o);
   A = (float***)malloc(o*sizeof(float**));
   for(i=0;i<o;i++){
           A[i] = (float**)malloc(n*sizeof(float*));
           for(j=0;j<n;j++){
               A[i][j] = (float*)malloc(m*sizeof(float));
           }
   }
   for(i=0;i<o;i++){
       for(j=0;j<n;j++){
           for(k=0;k<m;k++){
               printf("Input number for (%d,%d,%d) : ", i + 1, j + 1, k + 1);
               scanf("%f",&(A[i][j][k]));
           }
       }
   }
   
   
   return 0;
}

It would now work as a 3D matrix.

To understand the code I had to draw a messy diagram. I am attaching that. It may be of some help.

diagram

As you have already omitted some code I hope you have not forgotten to use free to prevent memory leaks.

nilTheDev
  • 380
  • 3
  • 14
0

This should work:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    float ***A;

    // n1, n2 and n3 will contain the three dimensions (i.e.: A[n1][n2][n3])
    size_t n1, n2, n3;

    printf("Insert first dimension: ");
    if (scanf("%zu", &n1) != 1) {
        printf("ERROR: cannot read number\n");
        return 1;
    }

    printf("Insert second dimension: ");
    if (scanf("%zu", &n2) != 1) {
        printf("ERROR: cannot read number\n");
        return 1;
    }

    printf("Insert third dimension: ");
    if (scanf("%zu", &n3) != 1) {
        printf("ERROR: cannot read number\n");
        return 1;
    }

    // firstly, here we alloc space for first dimension
    A = malloc(n1 * sizeof(float**));

    for (size_t i = 0; i < n1; i += 1) {
        // now we alloc space for second dimension
        A[i] = malloc(n2 * sizeof(float*));
        for (size_t j = 0; j < n2; j += 1) {
            // here we alloc space for third dimension
            A[i][j] = malloc(n3 * sizeof(float));
        }
    }

    // let's ask the user to input all elements into the array
    for (size_t i = 0; i < n1; i += 1) {
        for (size_t j = 0; j < n2; j += 1) {
            for (size_t k = 0; k < n3; k += 1) {
                printf("Insert value for A[%zu][%zu][%zu]: ", i, j, k);
                if (scanf("%f", &(A[i][j][k])) != 1) {
                    printf("ERROR: cannot read number\n");
                    return 1;
                }
            }
        }
    }

    // now let's print all the numbers that the user has input previously
    for (size_t i = 0; i < n1; i += 1) {
        for (size_t j = 0; j < n2; j += 1) {
            for (size_t k = 0; k < n3; k += 1) {
                printf("A[%zu][%zu][%zu] = %f\n", i, j, k, A[i][j][k]);
            }
        }
    }

    return 0;
}

I've not compiled it, but it should be correct. Take a close look at how allocation has been made.

NOTE: of course, total correctness (e.g.: use of free() and avoidance of scanf()) has been avoided on purpose.

P.S.: as other users already mentioned, take care to enable warnings for your compiler (e.g.: -Wall -Wextra on GCC), because it will simplify debugging very much for you.

Luca Polito
  • 2,387
  • 14
  • 20