arrays - Program returns incorrect values (C++) -


i've wrote simple code play around 2 dimensional arrays, aim of code being solve 2 linear simultaneous equations using matrix method (expressing coefficients in square matrix, computing inverse, multiplying inverse output of each of equations find results of 2 variables). there no warnings when code compiles, so, have no idea problem be.

#include <iostream> using namespace std;  double determinant(double parametermatrix[2][2]) {     return parametermatrix[1][1] * parametermatrix[2][2] - parametermatrix[1][2] *     parametermatrix[2][1]; }  void invertmatrix (double parametermatrix[2][2], double inversematrix[2][2]) {     double parameterdeterminant = determinant(parametermatrix);      inversematrix[1][1] = parametermatrix[2][2] / parameterdeterminant;     inversematrix[1][2] = - parametermatrix[1][2] / parameterdeterminant;     inversematrix[2][1] = - parametermatrix[2][1] / parameterdeterminant;     inversematrix[2][2] = parametermatrix[1][1] / parameterdeterminant; }  int main() {     double resultvector[2];     double coefficientmatrix[2][2];      cout << "enter equations of lines, of form ax+by=c" << endl;      cout << "a = "; cin >> coefficientmatrix[1][1];     cout << "b = "; cin >> coefficientmatrix[1][2];     cout << "c = "; cin >> resultvector[1];      cout << "a = "; cin >> coefficientmatrix[2][1];     cout << "b = "; cin >> coefficientmatrix[2][2];     cout << "c = "; cin >> resultvector[2]; cout << endl << endl;      double inversecoefficientmatrix[2][2];     invertmatrix(coefficientmatrix, inversecoefficientmatrix);      double x = inversecoefficientmatrix[1][1] * resultvector[1] + inversecoefficientmatrix[1][2] * resultvector[2];     double y = inversecoefficientmatrix[2][1] * resultvector[1] + inversecoefficientmatrix[2][1] * resultvector[2];      cout << "the lines intersect @ point (" << x << ", " << y << ")" << endl;     return 0; } 

if try simple example, e.g, x + 2y = 3 , 3x + 2y = 1, should return x = -1 , y = 2, values returns -0.5 , 0.1875. i've been inserting lines code print out values of @ each stage, , i've came conclusion determinant function changing value of resultvector, have no idea why, appreciated.

in c/c++ array indices start 0 not 1. double a[2] a[0] , a[1]. there no a[2]. (well there not yours)


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -