I have a function that takes a two-dimensional array as an input and prints it. I also have another function that will be used to determine the cofactor of a matrix, but when I pass a two-dimensional array to this function and use the previous print function on this array the values are now different.
The issue occurs in the function cofactor, which calls print_matrix. print_matrix works fine when called in the main function and the array values don't change when it is passed to the transpose function, so something is going wrong in the cofactor function.
I have tried defining the array in two different ways which are seen commented out in the main function and neither has worked. I have included my whole code in case some of the other functions are causing issues and the output is below the code, apologies if it is too long.
Is there something obvious I am missing?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 3
void print_matrix(int n, double a[N][N]){
int i;
printf("The original matrix is\n");
for(i=0; i<n; i ){
printf("%2.1f %2.1f %2.1f\n",a[i][0], a[i][1], a[i][2]);
}
}
void transpose(double a[N][N]){
int i;
int j;
double b[N][N];
for(i=0; i<N; i ){
for(j=0; j<N; j ){
b[j][i]=a[i][j];
a[i][j]=b[i][j];
}
}
printf("The original matrix transposed is\n");
for(i=0; i<N; i ){
printf("%2.1f %2.1f %2.1f\n",b[i][0], b[i][1], b[i][2]);
}
}
double det2(double a[2][2]){
double det2 = a[0][0]*a[1][1] - a[0][1]*a[1][0];
printf("The determinant of the 2x2 matrix = %f\n", det2);
return det2;
}
double cofactor(int i, int j, double c[N][N]){
double cij;
cij = pow(-1,(i j))* (c[0][0]*c[1][1] - c[0][1]*c[1][0]);
print_matrix(3, c);
return cij;
}
int main( void ){
int n = 3;
double a[N][N] = {{1.0,2.0,3.0},{4.0,5.0,6.0},{7.0,8.0,9.0}};
double b[2][2] = {{1,2},{3,4}};
/*a[0][0] = 1.0;
a[1][0] = 2.0;
a[2][0] = 3.0;
a[0][1] = 4.0;
a[1][1] = 5.0;
a[2][1] = 6.0;
a[0][2] = 7.0;
a[1][2] = 8.0;
a[2][2] = 9.0;*/
print_matrix(n,a);
printf("\n");
transpose(a);
printf("\n");
det2(b);
printf("\n");
cofactor(3, 3, a);
return 0;
}
Output:
The original matrix is
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
The original matrix transposed is
1.0 4.0 7.0
2.0 5.0 8.0
3.0 6.0 9.0
The determinant of the 2x2 matrix = -2.000000
The original matrix is
1.0 52959050172107956000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0 52958183844494710000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0
2.0 5.0 0.0
3.0 6.0 9.0
CodePudding user response:
Use 2 passes to transpose
void transpose(double a[N][N]){
int i;
int j;
double b[N][N];
for(i=0; i<N; i ){
for(j=0; j<N; j ){
b[j][i]=a[i][j];
}
}
for(i=0; i<N; i ){
for(j=0; j<N; j ){
a[i][j]=b[i][j];
}
}
Others approaches allow for a one pass and a single temporary double. Hint: walk the diagonals and swap.
CodePudding user response:
Your transpose() function is incorrect, which you can observe by having it print the elements of its argument a instead of those of its local array b (or by using print_matrix() to print a).
Here:
for(i=0; i<N; i ){ for(j=0; j<N; j ){ b[j][i]=a[i][j]; a[i][j]=b[i][j]; } }
you seem to be attempting both to make a transposed copy in b (for reasons unknown) and to transpose the original matrix, but note that a[i][j]=b[i][j] will in some cases read an element from b that has not yet been assigned a value. It is an unlucky happenstance that the iteration order chosen happens to produce the expected result in b.
You could approach the transposition two ways:
Make the full transpose in
bfirst, then, separately, copy the result toa, orTranspose
awithout an intermediate matrix by use of a loop nest that swaps each pair of elements once, using a standard three-way swap:for (i = 1; i < N; i ) { for (j = 0; j < i; j ) { double temp = a[i][j]; a[i][j] = a[j][i]; a[j][i] = temp; } }By handling only index pairs having
j < i, that produces exactly and only the required swaps, once each.
