0

I'm making a program that can calculate a square matrix multiplied by its own elements several times, and can print the elements.

In order to do so, I want to give a pointer of a array to a function that requires a double-pointer, but the array that I want to fill in the calculated elements becomes a NULL, so I'm at a loss.

I've tried to understand how the pointers work, and changed the array into an pointer of an array, or pointer to a pointer to an array, but I couldn't understand how it works.

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

#define row 5
#define col 5
#define max 5
void input_matrix(float [][col], int n);
void output_matrix(float [][col], int n);
void multiply_matrix(float [][col], float [][col], float [][col], int n);
void power_matrix(float (**)[max],float [][col], int n, int k);
float A[row][col];
float B[row][col];
float ak[row][col];
int i, j, k, l, n;

int main() {
    printf("What order square matrix do you want?\n");
    scanf("%d", &n);
    scanf("%d", &k);

    if(n > max) {
        fprintf(stderr, "Make it less than 5 digits.\n");
        return -1;
    }

    input_matrix(A, n);
    power_matrix(ak, A, n, k);
    output_matrix(A, n);

    return 0;
}

void input_matrix(float a[][col], int n) {
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            printf("Enter the element of row %d column %d.\n", i+1, j+1);
            scanf("%f", &a[i][j]);
        }
    }
}

void output_matrix(float a[][col], int n) {
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++) {
            printf("%f ", a[i][j]);printf("\n");
        }
    }
}

void multiply_matrix(float ak[][col], float a[][col], float b[][col], int n){
for(i=0;i<n;i++){
    for(j=0;j<n;j++){
        for(l=0;l<n;l++){
            ak[i][j]+=a[i][l] * b[l][j];
        }
    }
}

}

void power_matrix(float (**ak)[col],float a[][col], int n, int k){
float (*x)[max], (*y)[max];
switch(k){
    case 1:
        *ak=a;
        break;

    case 2:
        multiply_matrix(*ak, a, a, n);
        break;

    default:
        multiply_matrix(*ak, a, a, n);
        for(i=3;i<=k;i++){
            x=*ak;
            y=B;
            multiply_matrix(y, a, x, n);
            *ak=y;
        }
}
}

I expect the output to be like,

4.000000
4.000000
4.000000
4.000000

when the input is,

2 2
1
1
1
1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • If you want an array of arrays of arrays (for `power_matrix()`), why not `void power_matrix(float ak[][row][col],float a[][col], int n, int k)`. Please, note that only the most outer dim. of a function parameter of type might be undefined. Hence, you have to use `row` for the row dimension in this case. (There are actually no array parameters in functions -> the compiler converts it silently into a pointer type. Hence, the dimension is meaningless and might be left away. But that doesn't apply to that array element type which must be ful specified.) – Scheff's Cat Jun 01 '19 at 07:09
  • You will find a lot of similar questions with [google "c array function parameter site:stackoverflow.com"](https://www.google.com/search?q=c+array+function+parameter+site%3Astackoverflow.com). I couldn't decide to flag your question with one of them as duplicate... ;-) – Scheff's Cat Jun 01 '19 at 07:14
  • You manage to pass the matrix `A` correctly to all functions. What makes you think you should pass the matrix `ak` (which is declared exactly the same as `A`) differently for the `power_matrix` function? Especially since in the call you don't actually pass it differently? – Some programmer dude Jun 01 '19 at 07:18
  • 2
    Please, note also [SO: Is 2d array a double pointer?](https://stackoverflow.com/q/7586702/7478597) (Spoiler alert: NO.) – Scheff's Cat Jun 01 '19 at 07:19

0 Answers0