code: #include <stdio.h> #include <stdlib.h> #define ROWS 3 #define COLS 3 void
ID: 3573009 • Letter: C
Question
code:
#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 3
void display(double[ROWS][COLS]);
int main(){
double mat[ROWS][COLS] = { {1./2, 1, 0},
{0, 2./3, 0},
{-1./2, -1, 2./3} };
display(mat);
return 0;
}
void display(double nums[][COLS]) {
//problem with declaring array subscripts with double, so changed to int
int rowNum, colNum;
for (rowNum = 0.; rowNum < ROWS; rowNum++)
{
for (colNum = 0.; colNum < COLS; colNum++)
printf("%1.1f ", nums[rowNum][colNum]);
printf(" ");
}
}
Explanation / Answer
*********************************program.cpp*************************************************
#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 3
void display(double[ROWS][COLS]);
int main(){
double mat[ROWS][COLS] = { {1./2, 1, 0},
{0, 2./3, 0},
{-1./2, -1, 2./3} };
display(mat);
return 0;
}
void display(double mat[][COLS]) {
float determinant;
int i,j;
for(i=0;i<3;i++)
determinant = determinant + (mat[0][i]*(mat[1][(i+1)%3]*mat[2][(i+2)%3] - mat[1][(i+2)%3]*mat[2][(i+1)%3]));
printf(" Inverse of matrix is: ");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f ",((mat[(i+1)%3][(j+1)%3] * mat[(i+2)%3][(j+2)%3]) - (mat[(i+1)%3][(j+2)%3]*mat[(i+2)%3][(j+1)%3]))/ determinant);
printf(" ");
}
}
*********************************output********************************************************************
Inverse of matrix is:
2.00 -0.00 1.50
-3.00 1.50 0.00
0.00 0.00 1.50
--------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.