15,610,088 members
See more:
For the domajhuli function, when I give an input, it prints the correct answer, but the second time, it prints something else for the same input.

Please tell me what the problem is?

What I have tried:

#include <iostream>
#include <cmath>
#define Size 3
#define N 2
using namespace std;

void semajholi();
float determinan( float matrix[Size][Size], int n) ;
void domajholi();

int main()
{
int c;
cout<<"che moadelei hal konam 2/3 majholi ? "<<endl;
cin>>c;

switch(c)
{
case 2:
domajholi();
break;
case 3:
semajholi();
break;
}

}

void semajholi()
{

char ch1 = 'x';
char ch2 = 'y';
char ch3 = 'z';

float Mat[Size][Size];
float Mat_m[Size];

float mat_x[Size][Size];
float mat_y[Size][Size];
float mat_z[Size][Size];

cout<<"zaraib ra vard kon : "<<endl;

for(int i=0 ;i<Size;i++)
for(int j=0;j<Size;j++)
cin>>Mat[i][j];

cout<<"malomat ra vared kon : "<<endl;
for(int i=0 ;i<Size;i++)
cin>>Mat_m[i];

//tashkil matris majhulat

for(int i=0 ;i<Size;i++)
{
for(int j=0;j<Size;j++)
{
if(j==0)
mat_x[i][j]=Mat_m[i];
else
mat_x[i][j]=Mat[i][j];

if(j == 1)
mat_y[i][j]=Mat_m[i];
else
mat_y[i][j]=Mat[i][j];

if(j == 2)
mat_z[i][j]=Mat_m[i];
else
mat_z[i][j]=Mat[i][j];

}
}
//peyda kardan jvb

float det  = determinan(Mat,Size);
if(det != 0)
{
float detx = determinan(mat_x,Size);
float dety = determinan(mat_y,Size);
float detz = determinan(mat_z,Size);

cout<<endl<<endl;

cout<<ch1<<" = "<<detx/det<<endl;
cout<<ch2<<" = "<<dety/det<<endl;
cout<<ch3<<" = "<<detz/det<<endl;

}

else
}
float determinan( float matrix[Size][Size], int n)
{
float det = 0;
for(int i = 0; i < Size; i++)
det +=  (matrix[0][i] * (matrix[1][(i+1)%3] * matrix[2][(i+2)%3] - matrix[1][(i+2)%3] * matrix[2][(i+1)%3]));

return det;
}
void domajholi()
{

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 det;
///////////////////////////////////////////
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];
}
}

det = Zqtrasli - Zqtrfei;
maxosdet = (1) / (det)  ;
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<=2 ; j++)
{

cout<<varonmat[i][j] <<endl;
}
/////////////////////////////////////////////////////
for(int i = 1; i<= 2; i++)
for( int j = 1; j<= 1; j++)
for(int k = 1; k<=2; k++)
{
jvb[i][j] += varonmat[i][k] * malom[k][j];
}
////////////////////////////////////////////////////
cout<<"x = "<<jvb[1][1]<<endl;
cout<<"y = "<<jvb[2][1]<<endl;

}
Posted
Updated 24-Aug-21 1:55am
Patrice T 22-Jun-21 15:40pm
What input ?
jeron1 22-Jun-21 16:13pm
One issue, in the domajholi() function, you declare

int matzarb[2][2];

then you do...
for(int i=1 ; i<=2 ; i++)
{
cout<<"Enter zaraib moadele " << i<<" : "<<endl;
for(int j= 1 ; j <=2 j++)
{
cin >> matzarb[i][j];
.
.
.
}

When i or j is equal to 2 you will have a problem. The only valid indexes here are 0 and 1. This type of thing is probably an issue in several places.
Joe Woodbury 24-Jun-21 1:44am
First, this is C++; declare variables just before using them (in the right scope).

Second, arrays are zero based, not one based.

## Solution 1

Having a problem in this piece of code particularily and the same in some others of your loops:
C++
for(int i=1 ; i<=2 ; i++)
{
cout<<"Enter zaraib moadele " << i<<" : "<<endl;
for(int j=1 ; j<=2 ; j++)
cin>>matzarb[i][j];

}

You are running out of ranges. C++ indexes are zero based, so in this piece of code you are running from index 2 to 3

Correct solution is to be zero based
C++
for(int i = 0 ; i < 2 ; i++) //see starting from 0 and condition < instead of <=
{
//i + 1 means showing it as 1 based
cout<<"Enter zaraib moadele " << (i + 1)<<" : "<<endl;
for(int j = 0 ; j < 2 ; j++)
cin>>matzarb[i][j];

}

Do it in all your loops.

v8