-3

I did a code and I test in a different one just to make sure it works because it belongs to a switch, to be honest I have problems with functions I know how the parameters works but my real problem is the use of multidimensional arrays without pointers (because I don't know how to use them).

int ar, ac, br, bc, d, e, f;

int readMat(int mat[d][e], int row, int col) //Don't know what to do with mat[d][e]
{
    for(d=0;d<row;d++)
    {
       for(e=0;e<col;e++)
       {
          p("[%d][%d]: ",d+1,e+1);
          s("%d", &mat[d][e]);
       }
    }
}
main()
{   
    //Scans for the rows and col
    printf("Numero de renglonesde la matriz  A: ");
    scanf("%d", &ar);   //Mat A row
    printf("Numero de columnas de la matriz  A: ");
    scanf("%d", &ac);   //Mat A col
    printf("Numero de renglones de la matriz B: ");
    scanf("%d", &br);   //Mat B row
    printf("Numero de columnas de la matriz  B: ");
    scanf("%d", &bc);   //Mat b col
    if(ac!=br)
    {
        printf("No es posible hacer la multiplicación.\n");
        system("pause");
        return 0;
    }
    int A[ar][ac], B[br][bc], C[ar][bc];
    p("Escriba el valor de la primer matriz");
    int readMat(A[d][e],ar,ac);  //Use of the function
}    

Edit: I couldn't solve not using pointer even how Marian explain me. I know pointers are necessary but is a topic that I didn't use in class (I don't know how the teacher wanted us to solve it) so I wasn't allowed to 'use'.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • You should really learn about pointers if you really want to get a grasp in C. They can be handled very similarly to arrays with a few minor differences – Mauricio Trajano Nov 10 '14 at 07:27
  • http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work read this, it does a really good job explaining pointers. Also if you want to have changes from other functions affect variables that you care about you are probably going to have to use pointers because technically C is pass by value – user3282276 Nov 10 '14 at 07:30
  • It is not very clear what you want to know. I think you should start by reading up, first about matrices and how to use them, already without functions. In fact it seems that you are mixing the sizes of the matrix (`e` here) and indeces to individual elements, where you also use `e`. Then go for functions and learn how they work. – Jens Gustedt Nov 10 '14 at 07:30

2 Answers2

0

Variable length arrays were introduced in C99. You have to pass dimensions of the array as parameters before the array itself. So the code will look like:

int readMat(int d, int e, int mat[d][e], int row, int col)
{ ... }

However, you shall be aware that this is only a syntactic sugar. The array is actually passed as a pointer (as the address of its first element). The only difference is that you can index it by two indexes and the compiler makes the necessary calculation with indexes for you.

Marian
  • 7,402
  • 2
  • 22
  • 34
0

you should use pointers. Don't want use them because you don't know how to use? Well, Please try this code.

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

void readMat(int **mat, int row, int col){
    int i,j;
    for(i=0;i<row;i++){
        for(j=0;j<col;j++){
            printf("[%d][%d] : ", i+1,j+1);
            scanf("%d",&mat[i][j]);
        }
    }
} 

void printMat(int **mat, int row, int col){
    int i,j;
    for(i=0;i<row;i++){
       for(j=0;j<col;j++){
          printf("%d\t",mat[i][j]);
        }
        printf("\n");
    }
}
/*for beginner*/
int **createMat(int row, int col){
    int **mat;
    int i;
    mat = malloc(row*sizeof(int*));
    for(i=0;i<col;i++){
        mat[i] = malloc(col*sizeof(int));
    }
    return mat;
}

/*for handsome guy*/
int **createMat2(int row, int col){
    int **mat;
    int i;
    mat = malloc(row*sizeof(int*));
    mat[0] = malloc(row*col*sizeof(int));
    for(i=1;i<row;i++){
        mat[i] = mat[0] + i*col;
    }
    return mat;
}


void deleteMat(int **mat,int row, int col){
    int i;
    for(i=0;i<row;i++){free(mat[i]);}
    free(mat);
}

void deleteMat2(int **mat, int row, int col){
    free(mat[0]);
    free(mat);
}

int main()
{   
    int ar, ac, br, bc, d, e, f;
    int **A, **B, **C;
    //Scans for the rows and col
    printf("Numero de renglonesde la matriz  A: ");
    scanf("%d", &ar);   //Mat A row
    printf("Numero de columnas de la matriz  A: ");
    scanf("%d", &ac);   //Mat A col
    printf("Numero de renglones de la matriz B: ");
    scanf("%d", &br);   //Mat B row
    printf("Numero de columnas de la matriz  B: ");
    scanf("%d", &bc);   //Mat b col
    if(ac!=br)
    {
        printf("No es posible hacer la multiplicación.\n");
        system("pause");
        return 0;
    }
    printf("Escriba el valor de la primer matriz\n");
    A = createMat(ar, ac);
    B = createMat2(br,bc);
    C = createMat(ar,bc);

    readMat(A,ar,ac);  //beginner
    printMat(A,ar,ac);

    readMat(B,ar,ac);  //Handome guy 
    printMat(B,ar,ac);

    deleteMat(A,ar,ac);
    deleteMat2(B,ar,ac);
    deleteMat(C,ar,ac);
}   
  • Thank you so much! I really need to learn pointers but at class we didn't touch the topic yet... that's the reason why I didn't use them(I wasn't allowed neither). – Herli Ordoñez Nov 11 '14 at 01:11