i am trying to make a C program using cramers rule using 3 variables and 3 equat
ID: 3552357 • Letter: I
Question
i am trying to make a C program using cramers rule using 3 variables and 3 equations. I need help why the third variable says it isnt initialized and allow the the determinents to be calculated. also i need help making a menu allowing the user to make a start the program and enter the new variables or to exit the program.
This is what i have so far:
#include <stdio.h>
#include<stdlib.h>
int main (void)
{
int r,c;
double a[3][3],b[3];
double x1a, x2a, x3a, x1, x2, x3, d;
printf(" Solving for 3 Equations- 3 unknowns ");
for (r=0; r<3; r++)
{
for (c=0; c<3;c++)
{
printf("Enter a [%d][%d]:" ,r,c);
scanf_s("%lf", &a[r][c]);
}
d=(a[0][0]*a[1][1]*a[2][2])+(a[0][1]*a[1][2]*a[2][0]) +(a[0][2]*a[1][0]*a[2][1])-(a[0][2]*a[1][1]*a[2][0])-(a[0][0]*a[1][2]*a[2][1])-(a[0][1]*a[1][0]*a[2][2]);
x1a=(b[0]*a[1][1]*a[2][2])+(a[0][1]*a[1][2]*b[2])+(a[0][2]*b[1]*a[2][1])-(a[0][2]*a[1][1]*b[2])-(b[0]*a[1][2]*a[2][1])-(a[0][1]*b[1]*a[2][2]);
x2a=(a[0][0]*b[1]*a[2][2])+(b[0]*a[1][2]*a[2][0]) +(a[0][2]*a[1][0]*b[2])-(a[0][2]*b[1]*a[2][0])-(a[0][0]*a[1][2]*b[2])-(b[0]*a[1][0]*a[2][2]);
x3a=(a[0][0]*a[1][1]*b[2])+(a[0][1]*b[1]*a[2][0])+(b[0]*a[1][0]*a[2][1])-(b[0]*a[1][1]*a[2][0])-(a[0][0]*b[1]*a[2][1])-(a[0][1]*a[1][0]*b[2]);
x1=x1a/d; x2=x2a/d; x3=x3a/d;
printf(" X1=%lf, X2=%lf, X3=%lf ",x1,x2,x3);
system("pause");
return 0;
}}
Explanation / Answer
/*
* Desc: This program uses Cramer's Rule to find the solution of a
* system of three linear equations in three variables.
*/
#include <stdio.h>
int det3(int a[3][3]);
int main(void)
{
int A[3][3];
int B[3];
printf("This program uses Cramer's Rule to solve a linear system.
Enter each of 3 linear equations as four integers separated by space.
For example, x - 2y + 3z = 4 should be entered as 1 -2 3 4");
printf(" Enter equation 1: ");
scanf("%i %i %i %i", &A[0][0], &A[0][1], &A[0][2], &B[0]);
printf("Enter equation 2: ");
scanf("%i %i %i %i", &A[1][0], &A[1][1], &A[1][2], &B[1]);
printf("Enter equation 3: ");
scanf("%i %i %i %i", &A[2][0], &A[2][1], &A[2][2], &B[2]);
/*Finding determinants*/
int detx[3][3] = {{B[0],A[0][1],A[0][2]},{B[1],A[1][1],A[1][2]},
{B[2],A[2][1],A[2][2]}};
int dety[3][3] = {{A[0][0],B[0],A[0][2]},{A[1][0],B[1],A[1][2]},
{A[2][0],B[2],A[2][2]}};
int detz[3][3] = {{A[0][0],A[0][1],B[0]},{A[1][0],A[1][1],B[1]},
{A[2][0],A[2][1],B[2]}};
/* Code that determines if the system has a unique solution */
if(det3(A)!=0)
printf(" System has a unique solution ( %f, %f, %f)",
(float)det3(detx)/det3(A), (float)det3(dety)/det3(A), (float)det3(detz)/det3(A));
else
printf(" System does not have a unique solution because determinant is 0");
return 0;
}
int det3(int a[3][3])
{
return (a[0][0]*a[1][1]*a[2][2])-(a[0][0]*a[1][2]*a[2][1]),
+(a[0][1]*a[1][2]*a[2][0])-(a[0][1]*a[1][0]*a[2][2]),
+(a[0][2]*a[1][0]*a[2][1])-(a[0][2]*a[1][1]*a[2][0]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.