15,879,326 members
See more:
This code solves the two-unknown equation by the matrix method

What I have tried:

```#include <iostream>
using namespace std;

int main()
{

int matzarb[2][2];
int malom[2][1];
int Zqtrasli = 1;
int Zqtrfei  = 1;
float  maxosdet;
float varonmat[2][2];
float jvb[2][1];
float temp;
float temp1;
///////////////////////////////////////////
for(int i=1 ; i<=2 ; i++)
{
cout<<"Enter zaraib moadele " << i<<" : "<<endl;
for(int j=1 ; j<=2 ; j++)
cin>>matzarb[i][j];
}
////////////////////////////////////////
cout<<"Enter  malomat : "<<endl;
for(int i=1 ; i<=2 ; i++)
for(int j=1 ; j<=1 ; j++)
{
cout<<"("<<i<<","<<j<<") : ";
cin>>malom[i][j];
}
cout<<endl<<endl;
///////////////////////////////////////////////
for(int i=1 ; i<=2 ; i++)
for(int j=1 ; j<=2 ; j++)
{if(i==j)
{Zqtrasli *= matzarb[i][j];}
else{
Zqtrfei *= matzarb[i][j];}}
maxosdet = (1) / (Zqtrasli - Zqtrfei)  ;
cout<<maxosdet<<endl;
//////////////////////////////////////////////////
for(int i=1 ; i<=2 ; i++)
for(int j=1 ; j<=2 ; j++)
{if(i + j == 3)
{ matzarb[i][j] = (-1) * matzarb[i][j];}
else if(i==1 && j==1)
{
temp = matzarb[i][j];
matzarb[i][j]=matzarb[i+1][j+1];
matzarb[i+1][j+1]=temp;}
varonmat[i][j] = maxosdet * matzarb[i][j];
}
///////////////////////////////////////////////////
for(int i = 1; i<= 2; i++)
for( int j = 1; j<= 1; j++)
for(int k = 1; k<=2; k++)
{
jvb[i][j] += matzarb[i][k] * malom[k][j];}
////////////////////////////////////////////////////
cout<<"x = "<<jvb[1][1]<<endl;
cout<<"y = "<<jvb[2][1]<<endl;
return 0;
}
```
Posted
Updated 15-Jun-21 10:33am
jeron1 15-Jun-21 10:28am
Replace 2 with n. Have the user enter n, then dynamically allocate (new) your arrays based on n. Don't forget to delete the allocated memory when you are done using it.

A quick glance at you code shows,
int matzarb[2][2];

then later shows
for(int i = 1; i<= 2; i++)
for( int j = 1; j<= 1; j++)
for(int k = 1; k<=2; k++)
{
jvb[i][j] += matzarb[i][k] * malom[k][j];}
Formatting aside, any access to matzarb where any of the indexes is 2 is going to give incorrect results and/or crash. The largest index is n-1, indexing is zero based.

## Solution 1

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

And to be honest, that code is pretty poor - to the point where I don't want to even try working with it: the indentation is all over the place, it's undocumented, full of magic numbers, the curly brackets that are there are all over the place, there is no separation of concerns, ...

## Solution 2

Quote:
How can I write the code of n equation n unknown in C ++

Mostly the same as with any other programming languages, either you find a library that do the job, either you create necessary code.
General solution: as for any problem, the higher your level of understanding, the better you will be able to craft an efficient solution.
- search for documentation on topic : System of linear equations - Wikipedia[^]
It speaks of different solving methods.
- You choose 1 of them, and you can start programming.