Frage

[Solved Thanks] I developed the code below in C++ to solve linear equations utilizing the Gauss-Seidel method, but I seem to have a problem at run-time when filling the arrays that I can't figure out. Here's my code...

#include<stdio.h>
int main(void)
{
    float a[10][10],b[10],x[10],y[10];
    int n=0,m=0,i=0,j=0;
    printf("Enter size of 2d array(Square matrix) : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("Enter values no. %d %d :",i,j);
            scanf("%f",&a[i][j]);
        }
    }
    printf("\nEnter Values to the right side of equation\n");
    for(i=0;i<n;i++)
    {
            printf("Enter values no. %d :",i,j);
            scanf("%f",&b[i]);
    }
    printf("Enter initial values of x\n");
    for(i=0;i<n;i++)
    {
        printf("Enter values no. %d :",i);
        scanf("%f",&x[i]);
    }
    printf("\nEnter the no. of iteration : " );
    scanf("%d",&m);
    while(m>0)
    {
        for(i=0;i<n;i++)
        {
            y[i]=(b[i]/a[i][i]);
            for(j=0;j<n;j++)
            {
                if(j==i)
                    continue;
                y[i]=y[i]-((a[i][j]/a[i][i])*x[j]);
                x[i]=y[i];
            }
            printf("x%d = %f    ",i+1,y[i]);
        }
        printf("\n\n");
        m--;
    }
    return 0;
War es hilfreich?

Lösung

You don't allocate the second dimension for the EquationHolder. Since is a 2D matrix you have to allocate the second dimension also. Change your double for loop to the following:

float ** EquationHolder=new float *[3];
for (int i=0; i<NumEquations; i++)
{
    EquationHolder[i] = new float[3];
    cout<<"Please Enter The Information Of Equation ("<<i+1<<")...\n";
    for (int j=0; j<NumEquations; j++)
    {
        cout<<"X"<<j+1<<": ";
        cin>>EquationHolder[i][j];
    }
}

However I would recommend to use a std::vector<std::vector<double>> instead of C raw arrays its much safer.

Andere Tipps

The first thing:

allocate storage for all arrays:

int NumEquations=3;
// Equation Holder...
float ** EquationHolder= new float *[3];
for (int i=0; i<NumEquations; i++)
{
    EquationHolder[ i] = new float[3];
     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    cout<<"Please Enter The Information Of Equation ("<<i+1<<")...\n";
    for (int j=0; j<NumEquations; j++)
    {
        cout<<"X"<<j+1<<": ";
        cin>>EquationHolder[i][j];
    }
}

Additionally there were errors in main procedure:

#include <iostream>
using namespace std;

void GaussSeidel(int Iterations, float **EquationHolder, 
                                   float *EquationResultHolder) {
    int InitialGuess = 0;
    float x1 = 0, x2 = 0, x3 = 0;
    while (InitialGuess < Iterations) {
        x1 = (1 / EquationHolder[0][0])* (EquationResultHolder[0]-
                ((EquationHolder[0][1]) * x2)-((EquationHolder[0][2]) * x3));
        x2 = (1 / EquationHolder[1][1])*(EquationResultHolder[1]-
                ((EquationHolder[1][0]) * x1)-((EquationHolder[1][2]) * x3));
        x3 = (1 / EquationHolder[2][2])*((EquationResultHolder[2]-
               ((EquationHolder[2][0]) * x1)-((EquationHolder[2][1]) * x2)));
        InitialGuess += 1;
        cout << "---------------------Iteration #" << InitialGuess 
                                     << "---------------------" << std::endl;
        cout << "X1: " << x1 << "\t" << x2 << "\t" << x3 << std::endl;
    }
}

usage:

int main() {
    int NumEquations = 3;
    // Equation Holder...
    float ** EquationHolder = new float *[3];
    for (int i = 0; i < NumEquations; i++) {
        EquationHolder[ i] = new float[3];
        cout << "Please Enter The Information Of Equation(" << i + 1 << ")...\n";
        for (int j = 0; j < NumEquations; j++) {
            cout << "X" << j + 1 << ": ";
            cin >> EquationHolder[i][j];
        }
    }
    //... as before

    for ( int i = 0; i < NumEquations; i++) {   // deallocate storage
        delete [] EquationHolder[ i];
    }
    delete [] EquationHolder;

    return 0;
}

output:

(...)

Please Enter The Required Iterations: 9

---------------------Iteration #1---------------------

X1: 4 -2 1.42857

---------------------Iteration #2---------------------

X1: 4.14286 -2.83333 2.10204

(...)

---------------------Iteration #9---------------------

X1: 3.81631 -3.03054 2.36438

You are not initializing the rows of EquationHolder. Add this:

float ** EquationHolder = new float *[3];
for(int i=0; i<NumEquations; i++)     // ADD
    EquationHolder[i] = new float[3]; // ADD

Besides, I recommend you using double instead of float (double is much more precise, and less vulnerable to numeric error).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top