# Thread: What's wrong with this?

1. ## What's wrong with this?

Code:
```#include<iostream>
#include<string>
using namespace std;
int main(){
int a[3][3];
int b[3][3]={{0,0,0},{0,0,0},{0,0,0}};
int x1,x2,x3,y1,y2,y3,Matrica;
for(int i=1;i<4;i++){
for(int j=1;j<4;j++){
cout<<"Molim unesite element A ["<<i<<"]["<<j<<"]"<<endl;
cin>>a[i][j];
cout<<endl;
}
}

for(int i=1;i<2;i++){
for(int j=1;j<4;j++){
b[1][1]=b[1][1]+(a[i][j]*a[j][i]);
}
}
for(int i=1;i<2;i++){
for(int j=1;j<4;j++){
b[1][2]=b[1][2]+(a[i][j]*a[j][i+1]);
}
}
for(int i=1;i<2;i++){
for(int j=1;j<4;j++){
b[1][3]=b[1][3]+(a[i][j]*a[j][i+2]);
}
}

for(int i=2;i<3;i++){
for(int j=1;j<4;j++){
b[2][1]=b[2][1]+(a[i][j]*a[j][i-1]);
}
}
for(int i=2;i<3;i++){
for(int j=1;j<4;j++){
b[2][2]=b[2][2]+(a[i][j]*a[j][i]);
}
}
b[2][3]=0;
for(int i=2;i<3;i++){
for(int j=1;j<4;j++){
b[2][3]=b[2][3]+(a[i][j]*a[j][i+1]);
}
}
b[3][1]=0;
for(int i=3;i<4;i++){
for(int j=1;j<4;j++){
b[3][1]=b[3][1]+(a[i][j]*a[j][i-2]);
}
}
b[3][2]=0;
for(int i=3;i<4;i++){
for(int j=1;j<4;j++){
b[3][2]=b[3][2]+(a[i][j]*a[j][i-1]);
}
}
b[3][3]=0;
for(int i=3;i<4;i++){
for(int j=1;j<4;j++){
b[3][3]=b[3][3]+(a[i][j]*a[j][i]);
}
}

x1=b[1][1]*b[2][2]*b[3][3];
x2=b[1][2]*b[2][3]*b[3][2];
x3=b[1][3]*b[2][2]*b[3][3];
y1=-(b[1][3]*b[2][2]*b[3][1]);
y2=-(b[1][2]*b[2][3]*b[3][2]);
y3=-(b[1][3]*b[2][2]*b[3][3]);
Matrica=x1+x2+x3+(y1)+(y2)+(y3);
cout<<endl<<Matrica<<endl;
if(Matrica>0){
cout<<"    "<<b[1][2]<<"    "<<b[1][3]<<endl;
cout<<"           "<<b[2][3]<<endl;
cout<<"                  "<<endl;
}
else if(Matrica<0){
cout<<"                 "<<endl;
cout<<b[2][1]<<"          "<<endl;
cout<<b[3][1]<<" "<<b[3][2]<<"    "<<endl;
}
else if(Matrica==0){
cout<<endl<<b[1][1]<<"          "<<endl;
cout<<"    "<<b[2][2]<<"    "<<endl;
cout<<"          "<<b[3][3]<<endl;
}
system("pause");
return 0;
}```
This one works...

But this one...

Code:
```#include<iostream>
#include<string>
using namespace std;
int main(){
int a[3][3];
int b[3][3]={{0,0,0},{0,0,0},{0,0,0}};
int x1,x2,x3,y1,y2,y3,Matrica;
for(int i=1;i<4;i++){
for(int j=1;j<4;j++){
cout<<"Molim unesite element A ["<<i<<"]["<<j<<"]"<<endl;
cin>>a[i][j];
cout<<endl;
}
}

for(int i=1;i<2;i++){
for(int j=1;j<4;j++){
b[1][1]=b[1][1]+(a[i][j]*a[j][i]);
}
}
for(int i=1;i<2;i++){
for(int j=1;j<4;j++){
b[1][2]=b[1][2]+(a[i][j]*a[j][i+1]);
}
}
for(int i=1;i<2;i++){
for(int j=1;j<4;j++){
b[1][3]=b[1][3]+(a[i][j]*a[j][i+2]);
}
}

for(int i=2;i<3;i++){
for(int j=1;j<4;j++){
b[2][1]=b[2][1]+(a[i][j]*a[j][i-1]);
}
}
for(int i=2;i<3;i++){
for(int j=1;j<4;j++){
b[2][2]=b[2][2]+(a[i][j]*a[j][i]);
}
}

for(int i=2;i<3;i++){
for(int j=1;j<4;j++){
b[2][3]=b[2][3]+(a[i][j]*a[j][i+1]);
}
}

for(int i=3;i<4;i++){
for(int j=1;j<4;j++){
b[3][1]=b[3][1]+(a[i][j]*a[j][i-2]);
}
}

for(int i=3;i<4;i++){
for(int j=1;j<4;j++){
b[3][2]=b[3][2]+(a[i][j]*a[j][i-1]);
}
}

for(int i=3;i<4;i++){
for(int j=1;j<4;j++){
b[3][3]=b[3][3]+(a[i][j]*a[j][i]);
}
}

x1=b[1][1]*b[2][2]*b[3][3];
x2=b[1][2]*b[2][3]*b[3][2];
x3=b[1][3]*b[2][2]*b[3][3];
y1=-(b[1][3]*b[2][2]*b[3][1]);
y2=-(b[1][2]*b[2][3]*b[3][2]);
y3=-(b[1][3]*b[2][2]*b[3][3]);
Matrica=x1+x2+x3+(y1)+(y2)+(y3);
cout<<endl<<Matrica<<endl;
if(Matrica>0){
cout<<"    "<<b[1][2]<<"    "<<b[1][3]<<endl;
cout<<"           "<<b[2][3]<<endl;
cout<<"                  "<<endl;
}
else if(Matrica<0){
cout<<"                 "<<endl;
cout<<b[2][1]<<"          "<<endl;
cout<<b[3][1]<<" "<<b[3][2]<<"    "<<endl;
}
else if(Matrica==0){
cout<<endl<<b[1][1]<<"          "<<endl;
cout<<"    "<<b[2][2]<<"    "<<endl;
cout<<"          "<<b[3][3]<<endl;
}
system("pause");
return 0;
}```
Notice that in the second example there is no seting the b[][] again to zero.
The program gets a matrix (?)with elements that user enters.
Than it multiplay's(?) that matrix with itself.
Matriux on squaire.
Than it gets the resyult of the matrix and if the value of matrix is bigger than 0, it displays the upper triangle elements of the matrix,
for example:
2 3
1

-if it's smaller,it displays the lower triangle,exmpl.:

2
2 4

-and if the result of the matrix is equal to zero(0) than it displays the diagonal values,ex.:

2
3
7

But somethings wrong with that b[2][3]and all next becouse they get starting value of couple of millions rather than the value that I set them, and that is zero,0.

I solve the problem but with sting again every b after [2][2].

Why?

2. As I can see, you use indexes out of range. Arrays are indexed from 0 not from 1.

3. As I can see, you use indexes out of range. Arrays are indexed from 0 not from 1.