Hello guys I have been staring at this code for an hour and cant get it to work. My for loop on the bottom is not working. For some reason the array values keep resetting (value[row][col1]. This supposed to be Gauss-Seidel method so basically its an iterative method that relies on updated array values. Please help!

#include <iostream>

using namespace std;


int main()
{
 int row,col,i,col1;
    float array[3][4];
    float value[3][4];
    float x;
    x = 1;
    i = 0;

   //Set values for the matrix and output into DOS display
    for(row=0;row<3;row++)
    {
        for(col=0;col<4; col++)
        {array[row][col]=x;
        if(i<3)
        {cout<< array[row][col]<< " ";
        x=x*1.5+2;
        i++;}

        else{cout<<array[row][col]<<endl;
        cout<<endl;
        i = 0;
        }
        }
    }

    cout << endl;
    cout << endl;

    //resetting parameters
    i = 0;
col1 = 0;

//assign values for matrix
for(row=0;row<3;row++)
    {
        for(col=0;col<4; col++)
        {
        if(i<3)
        {
        value[row][col1] = 0;
        i++;

        cout << value[row][col1]<< " ";
        col1++;

        }

            else{

                cout << array[row][col] <<endl;
                i = 0;
                col1 = 0;

        }
    }
    }
// determine value of elements


for(i=0;i<2;i++)
{row = 0;
col = 3;
col1 = 0;
value[row][col1] = (array[row][col]-value[row][col1+1]*array[row][col1+1]-value[row][col1+2]*array[row][col1+2])/array[row][col1];

row++;
col1++;
value[row][col1] = (array[row][col]-value[row][col1-1]*array[row][col1-1]-value[row][col1+1]*array[row][col1+1])/array[row][col1];

row++;
col1++;
value[row][col1] = (array[row][col]-value[row][col1-1]*array[row][col1-1]-value[row][col1-2]*array[row][col1-2])/array[row][col1];

}

cout<< endl;
cout<< endl;
cout<< value[0][0]<<endl;
cout<< value[1][1]<<endl;
cout<< value[2][2]<<endl;

cin.get();

}

Recommended Answers

All 4 Replies

btw it doesnt matter what I set the i condition to for(i=0,i<200,i++) yields the same result as for(i=0,i<2,i++). Any ideas?

for(i=0;i<2;i++)
{row = 0;
col = 3;
col1 = 0;
value[row][col1] = (array[row][col]-value[row][col1+1]*array[row][col1+1]-value[row][col1+2]*array[row][col1+2])/array[row][col1];

row++;
col1++;
value[row][col1] = (array[row][col]-value[row][col1-1]*array[row][col1-1]-value[row][col1+1]*array[row][col1+1])/array[row][col1];

row++;
col1++;
value[row][col1] = (array[row][col]-value[row][col1-1]*array[row][col1-1]-value[row][col1-2]*array[row][col1-2])/array[row][col1];

}

This doesn't make sense. You are setting the values of row and col1 each loop to be back at 0.
So the loop will do the exact same thing every time. It will set these values each loop:
-loop start
row=0,col1=0
value[0][0]
row=1,col1=1
value[1][1]
row=2, col1=2
value[2][2]
-end loop

Because you reset the row and col1 variables back to 0 each loop, it will just do this exact same thing all the time.

The best thing to do is to define the row and col1 variables before you enter the loop.

Also, you aren't using a 2d array correctly.
You use the position 0,0 1,1 2,2
What about the rest?

0,0   0,1   0,2  0,3
1,0   1,1   1,2  1,3 
2,0   2,1   2,2  2,3

I'm not extremely familiar with the Gauss-Seidel method, but typically you will want to use a setup like this when working with a matrix:

for(int row = 0; row < rowMax; row++){
   for(int col = 0; col < colMax; col++){
      matrix[row][col] = blah;
   }
}

Most C++ text books have a section on working with matrices, and discuss the row/column and column/row methods of iterating.

OK maybe if i show how gauss seidel works with a working example then you can understand what im trying to do and how i need to set up the syntax so it can do that.

a + b = 5
a + 2b = 8

therefore the matrix would be:

[1 1 5]
[1 2 8]

Assume that the starting value of a and b is 0 and keep iterating until the answer the solution converges to the real answer for example:

a + 1*(0) = 5 => a = 5
1*(5) + 2*b = 8 => b = 1.5
a + 1*1.5 = 5 => a = 3.5
1*(3.5) + 2*b = 8 => b = 2.25
a + 1*(2.25) = 5 +> a = 2.75

if you keep iterating this you will eventually arive to the answer b = 3 a = 2, (try it in excel if you would like)

now im trying to do this with a for loop so yes I want row and col1 to keep resetting to 0 but only the marker not the actual value inside of the array. So basically after each for loop I want the array elements to save their values and keep reassigning new values to value[row][col1] after each iteration thus converging closer and closer to the answer as you saw in the example i just showed. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.