0% found this document useful (0 votes)
27 views5 pages

#Include #Include: Double While

This C code provides functions to calculate the determinant of 2x2 and 3x3 matrices. It prompts the user to input the values of the matrices, then displays the matrices. For a 2x2 matrix, it directly calculates the determinant as the product of diagonal elements minus the product of off-diagonal elements. For a 3x3 matrix, it calculates the determinant using the Laplace expansion method of summing diagonal cofactors multiplied by their corresponding matrix elements. It prints the calculated determinant value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

#Include #Include: Double While

This C code provides functions to calculate the determinant of 2x2 and 3x3 matrices. It prompts the user to input the values of the matrices, then displays the matrices. For a 2x2 matrix, it directly calculates the determinant as the product of diagonal elements minus the product of off-diagonal elements. For a 3x3 matrix, it calculates the determinant using the Laplace expansion method of summing diagonal cofactors multiplied by their corresponding matrix elements. It prints the calculated determinant value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <limits>
int main()
{
std::cout<<"This program is designed to test two linear equations. \n";
std::cout<<"In order to best solve the system, \n";
std::cout<<"equations will be in the form of a*x + b*y = c. \n";
std::cout<<"and d*x + e*y =f. \n";
std::cout<<"Please enter an integer for a, b, and c. \n";
double a, b, c, d, e, f;
while ((std::cout << "Enter a.")
&& !(std::cin >> a))
{
std::cout << "That's not a number ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter b.")
&& !(std::cin >> b))
{
std::cout << "That's not a number ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter c.")
&& !(std::cin >> c))
{
std::cout << "That's not a number ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout<<"Equation 1 is "<<a<<"x +"<<b<<"y ="<<c;
std::cout<<"Please enter an integer for d, e, and f. \n";
while ((std::cout << "Enter d.")

ode to find determinant of a matrix


C program to calculate determinant of a matrix

#include<stdio.h>
int main(){
int a[3][3],i,j;
int determinant=0;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1]
[(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2]
[(i+1)%3]));
printf("\nDeterminant of matrix is:
%d",determinant);
return 0;
}

C code for Determinant of 2X2 matrix:


#include<stdio.h>
int main(){
int a[2][2],i,j;
long determinant;
printf("Enter the 4 elements of matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];
printf("\nDeterminant of 2X2 matrix:
%ld",determinant);
}

return 0;

Enter the 4 elements of matrix: 4


8
3
9
The matrix is
4
3

8
9

Determinant of 2X2 matrix: 12


C code for Determinant of 3X3 matrix:

#include<stdio.h>
int main(){
int a[3][3],i,j;
long determinant;

printf("Enter the 9 elements of matrix: ");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
determinant = a[0][0]*((a[1][1]*a[2][2]) - (a[2]
[1]*a[1][2])) -a[0][1]*(a[1][0]*a[2][2] - a[2]
[0]*a[1][2]) + a[0][2]*(a[1][0]*a[2][1] - a[2]
[0]*a[1][1]);
printf("\nDeterminant of 3X3 matrix:
%ld",determinant);
}

return 0;

Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8

9
The matrix is
1
4
7

2
5
8

3
6
9

Determinant of 3X3 matrix: 0

You might also like