hello. i have been studying C++ and i programmed the following. can any one convert this in C language please it is urgent.
# include <iostream>
# include <iomanip>
# include <windows.h>
#include <conio.h>
using namespace std;
void pw (int,int);
void swap( double&,double&);
void main()
{
double arr1[10][10],arr2[10][10],arr3[10][10],d,d1,d2,d3,X,Y,Z,x1,x2,x3;
int i,j,k,l,r1,r2,c1,c2;
char x,y;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14); // All the arrays are declared of DOUBLE TYPE
start:
cout<<"\t\t\t -------------------------------\n\t\t\t MATRIX CALCULATOR\n\t\t\t -------------------------------\n\n";
cout<<"WHAT MATRIX OPERATION DO YOU WANT TO PERFORM : \n\n\n"; // Gives you the options...
cout<<"1. ADDITION\n\n2. SUBTRACTION\n\n3. MULTIPLICATION\n\n4. DETERMINANT\n\n5. EQUATION SOLVER\n\n6. ECHELON FORM\n\n7. INVERSE OF A MATRIX\n\n8. ADJOINT OF MATRIX"<<endl<<endl;
x=getch();
if (x!='1' && x!='2' && x!='3' && x!='4' && x!='5' && x!='6' && x!='7' && x!='8')
{
cout<<"\n\n\nOOooooopSSsss!!!\n\nWrong Input...!!!";
goto end;
}
if(x=='1')
{
system("CLS");
cout<<"\t\t\t -------------------------------\n\t\t\t ADDITION OF MATRICES\n\t\t\t -------------------------------\n\n";
}
if(x=='2')
{ system("CLS");
cout<<"\t\t\t -------------------------------\n\t\t\t SUBTRACTION OF MATRICES\n\t\t\t -------------------------------\n\n";
}
if(x=='3')
{
system("CLS");
cout<<"\t\t\t -------------------------------\n\t\t\t MULTIPLICATION OF MATRICES\n\t\t\t -------------------------------\n\n";
}
if (x=='4')
{
system("CLS");
goto det;
}
if (x=='5')
{ system("CLS");
int e;
cout<<"\t\t\t -------------------------------\n\t\t\t EQUATION SOLVER\n\t\t\t -------------------------------\n\n";
cout<<"Select the order of the equation: \n";
cout<<"\n\n\n1. Solve two simultaneous Equations\n\n2. Solve three simultaneous equations\n\n";
cin>>e;
if (e==1)
{
cout<<"Ax + By = C\n\nDx + Ey = F\n\n\n"; // In this option, simultaneous equations can be solved upto the order of 3.
// Functions of SWAP has also been used here..
cout<<"Enter the value of A: ";
cin>>arr1[0][0];
cout<<"Enter the value of B: ";
cin>>arr1[0][1];
cout<<"Enter the value of C: ";
cin>>arr1[0][2];
cout<<"Enter the value of D: ";
cin>>arr1[1][0];
cout<<"Enter the value of E: ";
cin>>arr1[1][1];
cout<<"Enter the value of F: ";
cin>>arr1[1][2];
d=arr1[0][0]*arr1[1][1]-arr1[0][1]*arr1[1][0];
swap (arr1[0][0],arr1[0][2]); // Interchange values
swap (arr1[1][0],arr1[1][2]);
d1=arr1[0][0]*arr1[1][1]-arr1[0][1]*arr1[1][0];
swap (arr1[0][0],arr1[0][2]); // Returned
swap (arr1[1][0],arr1[1][2]);
swap (arr1[0][1],arr1[0][2]);
swap (arr1[1][1],arr1[1][2]); //Interchanged
d2=arr1[0][0]*arr1[1][1]-arr1[0][1]*arr1[1][0];
swap (arr1[0][1],arr1[0][2]);
swap (arr1[1][1],arr1[1][2]); //Returned
X=d1/d; Y=d2/d;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),13);
cout<<"Please Wait..!!!\n";
pw(70,80);
cout<<"\n\nTHe value of x = "<<X<<endl;
cout<<"\n\nTHe value of y = "<<Y<<endl;
goto end;
}
else if (e==2)
{
cout<<"Ax + By = Cz= D\n\nEx + Fy + Gz= H\n\nIx + Jy + Kz= L\n\n\n";
cout<<"Enter the value of A: ";
cin>>arr1[0][0];
cout<<"Enter the value of B: ";
cin>>arr1[0][1];
cout<<"Enter the value of C: ";
cin>>arr1[0][2];
cout<<"Enter the value of D: ";
cin>>arr1[0][3];
cout<<"Enter the value of E: ";
cin>>arr1[1][0];
cout<<"Enter the value of F: ";
cin>>arr1[1][1];
cout<<"Enter the value of G: ";
cin>>arr1[1][2];
cout<<"Enter the value of H: "; //Solving three simultaneous Equations
cin>>arr1[1][3];
cout<<"Enter the value of I: ";
cin>>arr1[2][0];
cout<<"Enter the value of J: ";
cin>>arr1[2][1];
cout<<"Enter the value of K: ";
cin>>arr1[2][2];
cout<<"Enter the value of L: ";
cin>>arr1[2][3];
d=arr1[0][0]*(arr1[1][1]*arr1[2][2]-arr1[1][2]*arr1[2][1])-arr1[0][1]*(arr1[2][2]*arr1[1][0]-arr1[1][2]*arr1[2][0])+arr1[0][2]*(arr1[2][1]*arr1[1][0]-arr1[1][1]*arr1[2][0]);
swap(arr1[0][0],arr1[0][3]);
swap(arr1[1][0],arr1[1][3]); // value of 1st column and 4th column are intercganged..
swap(arr1[2][0],arr1[2][3]);
d1=arr1[0][0]*(arr1[1][1]*arr1[2][2]-arr1[1][2]*arr1[2][1])-arr1[0][1]*(arr1[2][2]*arr1[1][0]-arr1[1][2]*arr1[2][0])+arr1[0][2]*(arr1[2][1]*arr1[1][0]-arr1[1][1]*arr1[2][0]);
swap(arr1[0][0],arr1[0][3]);
swap(arr1[1][0],arr1[1][3]);
swap(arr1[2][0],arr1[2][3]); // The Upper values are replaced..
swap(arr1[0][1],arr1[0][3]);
swap(arr1[1][1],arr1[1][3]);
swap(arr1[2][1],arr1[2][3]); //Intercganged
d2=arr1[0][0]*(arr1[1][1]*arr1[2][2]-arr1[1][2]*arr1[2][1])-arr1[0][1]*(arr1[2][2]*arr1[1][0]-arr1[1][2]*arr1[2][0])+arr1[0][2]*(arr1[2][1]*arr1[1][0]-arr1[1][1]*arr1[2][0]);
swap(arr1[0][1],arr1[0][3]);
swap(arr1[1][1],arr1[1][3]); //Replaced
swap(arr1[2][1],arr1[2][3]);
swap(arr1[0][2],arr1[0][3]);
swap(arr1[1][2],arr1[1][3]);
swap(arr1[2][2],arr1[2][3]); //Interchanged
d3=arr1[0][0]*(arr1[1][1]*arr1[2][2]-arr1[1][2]*arr1[2][1])-arr1[0][1]*(arr1[2][2]*arr1[1][0]-arr1[1][2]*arr1[2][0])+arr1[0][2]*(arr1[2][1]*arr1[1][0]-arr1[1][1]*arr1[2][0]);
swap(arr1[0][2],arr1[0][3]);
swap(arr1[1][2],arr1[1][3]);
swap(arr1[2][2],arr1[2][3]); //Replaced
X=d1/d;
Y=d2/d;
Z=d3/d;
cout<<"Please Wait..!!!\n";
pw(70,80);
cout<< "\n\n\nThe value of x = "<<X<<endl;
cout<< "The value of y = "<<Y<<endl;
cout<< "The value of z = "<<Z<<endl;
goto end;
}
else
{
cout<<"\n\nSorry!!!\n Wrong Input.";
goto start;
}
}
if(x=='6')
{ system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
cout<<"\t\t\t -------------------------------\n\t\t\t ECHELON FORM\n\t\t\t -------------------------------\n\n";
cout<<"Enter the ORDER of the MATRIX: "<<endl;
cin>>r1;
cin>>c1;
if(r1>3 || c1>3)
{
cout<<"Sorry!!!\nThis programs solves the matrices maximum upto the order of 3 by 3.\n\n\n";
goto start;
}
for(i=0;i<r1;i++)
{
cout<<"Enter the elements of "<<i+1<<" row : "<<endl;
for(j=0; j<c1; j++)
{
cin>>arr1[i][j]; // Finds the Echelon form for the matrices upto 3x3 order using row operations...
}
}
cout<<"The MATRIX is: "<<endl<<endl<<endl;
for(i=0;i<r1;i++)
{
cout<<"|";
for(j=0; j<c1; j++)
{
cout<<arr1[i][j]<<setw(10);
}
cout<<setw(-10)<<"|"<<endl;
}
if(arr1[0][0]==0)
{
swap(arr1[0][0],arr1[2][0]);
swap(arr1[0][1],arr1[2][1]);
swap(arr1[0][2],arr1[2][2]);
}
X=arr1[0][0];
for(j=0;j<c1;j++)
{
arr1[0][j]=arr1[0][j]/X;
}
for(i=1;i<r1;i++)
{
if (i==1)
{
x1=arr1[1][0];
for(j=0;j<c1;j++)
{
arr1[i][j]=arr1[i][j]+
(arr1[0][j]*(-x1));
}
}
if(i==2)
{
x2=arr1[2][0];
for(j=0;j<c1;j++)
{
arr1[i][j]=arr1[i][j]+(arr1[0][j]*(-x2));
}
}
}
if(arr1[1][1]==0)
{
swap(arr1[1][0],arr1[2][0]);
swap(arr1[1][1],arr1[2][1]);
swap(arr1[1][2],arr1[2][2]);
}
else
{
Y=arr1[1][1];
for(j=0;j<c1;j++)
{
arr1[1][j]=arr1[1][j]/Y;
}
for(i=2;i<r1;i++)
{
x3=arr1[2][1];
for(j=0;j<c1;j++)
{
arr1[i][j]=arr1[i][j]+(arr1[1][j]*(-x3));
}
}
}
cout<<"\t\t\t Please Wait While Calculating!\n";
pw(70,80);
cout<<"\nThe ECHELON FORM of the MATRIX is: "<<endl<<endl<<endl;
for(i=0;i<r1;i++)
{
cout<<"|";
for(j=0; j<c1; j++)
{
cout<<arr1[i][j]<<setw(10);
}
cout<<setw(-10)<<"|"<<endl;
}
goto end;
}
if (x=='7')
{ system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
cout<<"\t\t\t -------------------------------\n\t\t\t INVERSE OF A MATRIX\n\t\t\t -------------------------------\n\n";
cout<<"Enter the ORDER of the MATRIX: "<<endl;
cin>>r1;
cin>>c1;
if(r1>2 || c1>2)
{
cout<<"Sorry!!!\nThis programs finds the inverse of the matrices upto the order of 2x2.\n\n\n";
goto start;
}
for(i=0;i<r1;i++)
{
cout<<"Enter the elements of "<<i+1<<" row : "<<endl;
for(j=0; j<c1; j++) //Finds The inverse of a 2x2 matrix using adjoint and determinant..
{
cin>>arr1[i][j];
}
}
cout<<"The MATRIX is: "<<endl<<endl<<endl;
for(i=0;i<r1;i++)
{
cout<<"|";
for(j=0; j<c1; j++)
{
cout<<arr1[i][j]<<setw(10);
}
cout<<setw(-10)<<"|"<<endl;
}
d=arr1[0][0]*arr1[1][1]-arr1[0][1]*arr1[1][0];
swap(arr1[0][0],arr1[1][1]);
arr1[0][1]=-arr1[0][1];
arr1[1][0]=-arr1[1][0];
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);
cout<<"\t\t\t Please Wait..!!!\n";
pw(70,80);
cout<<"\nThe INVERSE of the given matrix is: \n\n\n";
for(i=0;i<r1;i++)
{
cout<<"|";
for(j=0; j<c1; j++)
{
cout<<arr1[i][j]/d<<setw(15);
}
cout<<setw(-15)<<"|"<<endl;
}
goto end;
}
if(x=='8')
{ system("CLS");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
cout<<"\t\t\t -------------------------------\n\t\t\t ADJOINT OF A MATRIX\n\t\t\t -------------------------------\n\n";
cout<<"Enter the ORDER of the MATRIX: "<<endl;
cin>>r1;
cin>>c1;
if(r1>2 || c1>2)
{
cout<<"Sorry!!!\nThis programs finds the ADJOINT of the matrices upto the order of 2x2.\n\n\n";
goto start;
}
for(i=0;i<r1;i++)
{
cout<<"Enter the elements of "<<i+1<<" row : "<<endl;
for(j=0; j<c1; j++) //Finds The inverse of a 2x2 matrix using adjoint and determinant..
{
cin>>arr1[i][j];
}
}
cout<<"The MATRIX is: "<<endl<<endl<<endl;
for(i=0;i<r1;i++)
{
cout<<"|";
for(j=0; j<c1; j++)
{
cout<<arr1[i][j]<<setw(10);
}
cout<<setw(-10)<<"|"<<endl;
}
swap(arr1[0][0],arr1[1][1]);
arr1[0][1]=-arr1[0][1];
arr1[1][0]=-arr1[1][0];
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);
cout<<"\t\t\t Please Wait..!!!\n";
pw(70,80);
cout<<"\nThe ADJOINT of the given matrix is: \n\n\n";
for(i=0;i<r1;i++)
{
cout<<"|";
for(j=0; j<c1; j++)
{
cout<<arr1[i][j]<<setw(10);
}
cout<<setw(-10)<<"|"<<endl;
}
goto end;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14);
cout<<"Enter the ORDER of the FIRST MATRIX: "<<endl; // These are the common statements for addition, subtraction and multiplication..
cin>>r1;
cin>>c1;
if(r1>10 || c1>10)
{
cout<<"Sorry!!!\nThis programs solves the matrices maximum upto the order of 10 by 10.\n\n\n";
goto start;
}
for(i=0;i<r1;i++)
{
cout<<"Enter the elements of "<<i+1<<" row : "<<endl;
for(j=0; j<c1; j++)
{
cin>>arr1[i][j];
}
}
cout<<"Enter the ORDER of the SECOND MATRIX: "<<endl;
cin>>r2;
cin>>c2; //Taking input for the two matrices
if(r2>10 || c2>10)
{
cout<<"Sorry!!!\nThis programs solves the matrices maximum upto the order of 10 by 10.\n\n\n";
goto start;
}
for(k=0;k<r2;k++)
{
cout<<"Enter the elements of "<<k+1<<" row : "<<endl;
for(l=0; l<c2; l++)
{
cin>>arr2[k][l];
}
}
cout<<"The FIRST MATRIX is: "<<endl<<endl<<endl;
for(i=0;i<r1;i++)
{
cout<<"|";
for(j=0; j<c1; j++)
{
cout<<arr1[i][j]<<setw(9);
}
cout<<setw(-9)<<"|"<<endl;
}
cout<<"The SECOND MATRIX is : "<<endl<<endl<<endl;
for(k=0;k<r2;k++)
{
cout<<"|";
for(l=0; l<c2; l++)
{
cout<<arr2[k][l]<<setw(9);
}
cout<<setw(-9)<<"|"<<endl;
//Giving the output of the matrices..
}
if(x=='1')
{
if (r2!=r1 || c2!=c1)
{
cout<<"\n\nOOooppS!!!!\n\nThese matrices are not suitable for addition.\n\n\n";
goto start;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),9);
cout<<"\t\t\t Please Wait..!!!\n";pw(70,80);
cout<<"\n\nThe sum of the two matrices is: "<<endl<<endl;
for(i=0;i<r1;i++)
{
cout<<"|";
for(j=0;j<c1;j++)
{
arr3[i][j]=arr1[i][j]+arr2[i][j];
cout<<arr3[i][j]<<setw(10);
}
cout<<setw(-10)<<"|"<<endl;
}
goto end;
}
else if(x=='2')
{
if(r1!=r2 || c1!=c2)
{
cout<<"Sorry!!! \nThese matrices are not suitable for Subtraction.\n\n\n"<<endl;
goto start; // If the order of the matrices is not same...
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),13);
cout<<"\t\t\t Please Wait..!!!\n";
pw(70,80);
cout<<"\n\nThe difference of the two matrices is: "<<endl<<endl;
for(i=0;i<r1;i++)
{
k=i; //Taking difference of the two matrices...
cout<<"|";
for(j=0;j<c1;j++)
{
l=j;
arr3[i][j]=arr1[i][j]-arr2[k][l];
cout<<arr3[i][j]<<setw(9);
}
cout<<setw(-9)<<"|"<<endl;
}
}
goto end;
}
else if (x=='3')
{ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);
if(c1!=r2)
{
cout<<"\n\nSORRY!!!\nThese matrices are not suitable for multiplication.\n\n\n"<<endl;
goto start;
}
else
{
for(i=0;i<r1;i++)
{
for(k=0;k<c2;k++)
{
arr3[i][k]=0;
}
}
for(i=0;i<r1;i++) //Using three nested FOR loops for the general condition of multiplication..
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++) // Here c1=r2 (condition for multiplication)
{
arr3[i][k]=arr3[i][k]+(arr1[i][j]*arr2[j][k]);
}
}
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3);
cout<<"\t\t\t Please Wait..!!!\n";
pw(70,80);
cout<<"\n\nThe PRODUCT of these TWO MATRICES is :\n\n\n";
for(i=0;i<r1;i++)
{
cout<<"|";
for(k=0;k<c2;k++)
{
cout<<arr3[i][k]<<setw(9);
}
cout<<setw(-9)<<"|"<<endl;
}
}
goto end;
}
det:
{ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),9);
cout<<"\t\t\t DETERMINANT SOLVER \n\n\n"<<endl;
cout<<"ENTER THE ORDER OF THE MATRIX: ";
cin>>r1;
c1=r1;
if (r1>4 || r1<1)
{
cout<<"This program calculates the determinant upto the order of 4 x4. "<<endl;
goto start;
}
else
{
for(i=0;i<r1;i++)
{
cout<<"Enter the elements of "<<i+1<<" row : "<<endl;
for(j=0; j<c1; j++)
{
cin>>arr1[i][j];
}
}
cout<<"The given matrix is: "<<endl;
for(i=0;i<r1;i++)
{
cout<<"|";
for(j=0; j<c1; j++)
{
cout<<arr1[i][j]<<setw(9);
}
// Calculates the determinant upto the order of 4x4, which a calculator can't do..
cout<<setw(-9)<<"|"<<endl;
}
if (r1==1)
{ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
cout<<"\t\t\t Please Wait..!!!\n";
pw(70,80);
cout<<"\nThe determinant of the given matrix = "<<arr1[0][0]<<endl;
}
if(r1==2)
{
d=arr1[0][0]*arr1[1][1]-arr1[0][1]*arr1[1][0];
cout<<"\t\t\t Please Wait..!!!\n";
pw(70,80);
cout<<"\n\nThe determinant of the given matrix = "<<d<<endl;
}
if (r1==3)
{
d=arr1[0][0]*(arr1[1][1]*arr1[2][2]-arr1[1][2]*arr1[2][1])-arr1[0][1]*(arr1[2][2]*arr1[1][0]-arr1[1][2]*arr1[2][0])+arr1[0][2]*(arr1[2][1]*arr1[1][0]-arr1[1][1]*arr1[2][0]);
cout<<"\t\t\t Please Wait..!!!\n";
pw(70,80);
cout<<"\nThe determinant of the given matrix = "<<d<<endl;
}
if (r1==4)
{
d = arr1[0][0]*(arr1[1][1]*(arr1[2][2]*arr1[3][3]-arr1[2][3]*arr1[3][2])-arr1[1][2]*(arr1[2][1]*arr1[3][3]-arr1[2][3]*arr1[3][1])+arr1[1][3]*(arr1[2][1]*arr1[3][2]-arr1[2][2]*arr1[3][1]))
-arr1[0][1]*(arr1[1][0]*(arr1[2][2]*arr1[3][3]-arr1[2][3]*arr1[3][2])-arr1[1][2]*(arr1[2][0]*arr1[3][3]-arr1[2][3]*arr1[3][0])+arr1[1][3]*(arr1[2][0]*arr1[3][2]-arr1[2][2]*arr1[3][0]))
+arr1[0][2]*(arr1[1][0]*(arr1[2][1]*arr1[3][3]-arr1[2][3]*arr1[3][1])-arr1[1][1]*(arr1[2][0]*arr1[3][3]-arr1[2][3]*arr1[3][0])+arr1[1][3]*(arr1[2][0]*arr1[3][1]-arr1[2][1]*arr1[3][0]))
-arr1[0][3]*(arr1[1][0]*(arr1[2][1]*arr1[3][2]-arr1[2][2]*arr1[3][1])-arr1[1][1]*(arr1[2][0]*arr1[3][2]-arr1[2][2]*arr1[3][0])+arr1[1][2]*(arr1[2][0]*arr1[3][1]-arr1[2][1]*arr1[3][0]));
cout<<"\t\t\t Please Wait..!!!\n";
pw(70,80);
cout<<"\nThe determinant of the given matrix = "<<d<<endl;
}
}
}
end:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14);
cout<<"\n\n\nDo you want to use the program again??\n";
cout<<" Yes\n No\n\n";
y=getch();
if( y=='y' || y=='Y')
{ system("CLS");
goto start;
}
else if(y=='n' || y=='N')
{
exit(0);
}
else
{
cout<<"Wrong input.\n\n";
}
system("pause");
}
void swap( double & x, double & y)
{
double temp = x;
x=y;
y=temp;
}
void pw (int a,int b)
{
for(int i=0;i<70;i++)
{
cout<<"*";
Sleep(80);
}
}