Hey everyone, I'm having a bit of a problem with the addition code for my program. Sorry to say this but I ONLY need help with the ADDITION code, as I posted this question around on other forums and they complained and complained about my code, when i know it works perfectly fine.
#include <iostream>
usingnamespace std;
void displaymatrix (double a[4][5],int M,int N){
N--;
int n,m;
for (m=0;m<=M;m++)
{
for (n=0;n<=N;n++)
cout<<a[m][n]<<"\t";
cout<<"|\t";
cout<<a[m][n];
cout<<endl<<endl<<endl;}
cout<<endl<<endl<<endl<<endl;
}
int main()
{
double a[4][5];
int M=4-1;
int N=5-1;
a[0][0]=3;
a[0][1]=5;
a[0][2]=7;
a[0][3]=9;
a[0][4]=3;
a[1][0]=2;
a[1][1]=3;
a[1][2]=4;
a[1][3]=4;
a[1][4]=1;
a[2][0]=15;
a[2][1]=7;
a[2][2]=3;
a[2][3]=7;
a[2][4]=6;
a[3][0]=2;
a[3][1]=11;
a[3][2]=3;
a[3][3]=12;
a[3][4]=3;
displaymatrix (a,M,N);
/*Ordering Matrix:*/
int temporary[N];
for(int n=0;n<=N;n++)
for(int m=0;m<=M;m++)
if (a[m][n]==0)
temporary[n]=a[m][n];
a[m][n]=a[m+1][n];
/*Ordering Matrix*/
double scalar;/*Scalar "scales" each row by a certain ammount*/
for (int column=0;column<=2;column++)/*For All Columns*/
for (int row=column+1;row<=3;row++)/*For All Rows*/
{
scalar=a[column][column]/a[row][column];
for (int n=0;n<=N;n++)/*Multiplying All Columns of Row*/
a[row][n]*=scalar;
for (int n=0;n<=N;n++)/*Subtracting All Columns of Row*/
a[row][n]=a[row][n]-a[column][n];
}
displaymatrix (a,M,N);
double z=a[3][3];
cin.get();
}
The crux is the last couple of lines. Note how I included a triple for loop with each for having a comment on what it does. Note that this analytically solves the matrix. Its also fun to do the numerical approach of "Gauss-Seidel" iteration.
I imagine this would give you errors. You haven't declared result for starters (I imagine it should be an int) and both 'row' and 'col' are ints not pointer (or pointers to arrays for that matter) so you can't use [ ] on them.
they complained and complained about my code, when i know it works perfectly fine.
Works fine? It doesn't even compile. Which compiler are you using? It should have pointed out both the problems for you. My compiler agrees exactly with LBEaston and points out both the problems.
also you're writing (and reading) past the end of the array numbers[2][2] doesn't (shouldn't) exist. You're messing with random memory addresses with no nowing what they might contain.