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