Problem mit Arrays und Rechenoperationen



  • Hi,

    ich wollte mir zum Üben ein Programm schreiben, welches Gleichungssysteme mit zwei Unbekannten lösen kann.

    Leider bekomm ich am Ende immer eine Fehlermeldung:

    Run-Time Check Failure #2 - Stack around the variable 'Koeffizentenmatrix' was corrupted.
    

    Außerdem wird die erste Variable nicht korrekt berechnet (Zeile 49):

    6x-4y=10
    4x+2y=44
    

    Jetzt kommt bei der ersten Ausgabe

    0+-7=-67.5
    4+ 2=44
    

    raus!

    Richtig wäre

    0+-7=-56
    4+ 2=44
    

    Hier der Code:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    	//Variablen werden deklariert
    	long double Teiler;
    	//Array wird erzeugt
    	long double Koeffizentenmatrix [3][2];
    
    	//Layout wird ausgeben
    	cout << "x1 + y1 = Cn1" << endl;
    	cout << "x2 + y2 = Cn2" << endl;
    
    	//User wird zur Eingabe veranlasst
    	cout << "x1: " << flush;
    	cin >> Koeffizentenmatrix[1][1];
    
    	cout << "y1: " << flush;
    	cin >> Koeffizentenmatrix[2][1];
    
    	cout << "Cn1: " << flush;
    	cin >> Koeffizentenmatrix[3][1];
    
    	cout << "x2: " << flush;
    	cin >> Koeffizentenmatrix[1][2];
    
    	cout << "y2: " << flush;
    	cin >> Koeffizentenmatrix[2][2];
    
    	cout << "Cn2: " << flush;
    	cin >> Koeffizentenmatrix[3][2];
    
    	cout << endl;
    
    	//Gleichungssystem wird ausgegeben
    	cout << Koeffizentenmatrix[1][1] << " + " << Koeffizentenmatrix[2][1] << " = " << Koeffizentenmatrix[3][1] << endl;
    	cout << Koeffizentenmatrix[1][2] << " + " << Koeffizentenmatrix[2][2] << " = " << Koeffizentenmatrix[3][2] << endl;
    	cout << endl;
    
    	//Erste Variable wird aufgelöst
    	//Kleinster gemeinsamer Teiler wird ermittelt
    	Teiler = (Koeffizentenmatrix[1][1]/Koeffizentenmatrix[1][2])*-1;
    	//Gesamte erste Zeile wird mit dem kleinsten Teiler (double Teiler) multipliziert
    	Koeffizentenmatrix[1][1] = Koeffizentenmatrix [1][1] + (Koeffizentenmatrix[1][2]*Teiler);
    	Koeffizentenmatrix[2][1] = Koeffizentenmatrix [2][1] + (Koeffizentenmatrix[2][2]*Teiler);
    	Koeffizentenmatrix[3][1] = Koeffizentenmatrix [3][1] + (Koeffizentenmatrix[3][2]*Teiler); //Hier wird das Ergebnis nicht korrekt berechnet
    
    	//Gleichungssystem wird ausgeben
    	cout << Koeffizentenmatrix[1][1] << " + " << Koeffizentenmatrix[2][1] << " = " << Koeffizentenmatrix[3][1] << endl;
    	cout << Koeffizentenmatrix[1][2] << " + " << Koeffizentenmatrix[2][2] << " = " << Koeffizentenmatrix[3][2] << endl;
    	cout << endl;
    
    	//Zweite Variabel wird aufgelöst
    	//Kleinster gemeinsamer Teiler wird ermittelt
    	Teiler = Koeffizentenmatrix[2][2]/Koeffizentenmatrix[2][1]*-1;
    	//Gesamte weite Zeile wird mit dem kleinsten Teiler (double Teiler) multipliziert
    	Koeffizentenmatrix[1][2] = Koeffizentenmatrix [1][2] + (Koeffizentenmatrix[1][1]*Teiler);
    	Koeffizentenmatrix[2][2] = Koeffizentenmatrix [2][2] + (Koeffizentenmatrix[2][1]*Teiler);
    	Koeffizentenmatrix[3][2] = Koeffizentenmatrix [3][2] + (Koeffizentenmatrix[3][1]*Teiler);
    
    	//Gleichungssystem wird ausgegeben
    	cout << Koeffizentenmatrix[1][1] << " + " << Koeffizentenmatrix[2][1] << " = " << Koeffizentenmatrix[3][1] << endl;
    	cout << Koeffizentenmatrix[1][2] << " + " << Koeffizentenmatrix[2][2] << " = " << Koeffizentenmatrix[3][2] << endl;
    	cout << endl;
    
    	//Gleichungssystem wird komplett ausgegeben
    	Koeffizentenmatrix[3][1] = Koeffizentenmatrix[3][1]/Koeffizentenmatrix[2][1];
    	Koeffizentenmatrix[2][1] = 1;
    	Koeffizentenmatrix[3][2] = Koeffizentenmatrix[3][2]/Koeffizentenmatrix[1][2];
    	Koeffizentenmatrix[1][2] = 1;
    
    	//Gleichungssystem wird ausgegeben
    	cout << Koeffizentenmatrix[1][1] << " + " << Koeffizentenmatrix[2][1] << " = " << Koeffizentenmatrix[3][1] << endl;
    	cout << Koeffizentenmatrix[1][2] << " + " << Koeffizentenmatrix[2][2] << " = " << Koeffizentenmatrix[3][2] << endl;
    	cout << endl;
    
    	system ("pause");
    	return 0;
    }
    

    Ist nur ein erste Entwurf, Funktionen für die Ausgabe o.ä mach ich später 😉

    MfG


  • Mod

    Arrayindizes fangen bei 0 an, deshalb zerstörst du dir mit Zugriffen auf Koeffizientenmatrix[3][2] den Stack.



  • 😮
    Peinlicher Anfängerfehler

    Danke 👍



  • Es gibt dann auch noch so etwas wie Schleifen und Funktionen. Versuche, all die Aktionen, die Du jetzt in main durchführst in kleinere Häppchen zu isolieren und diesen Häppchen (Funktionen) einen aussagekräftigen Namen zu geben -- beispielsweise je eine Funktion für eine bestimmte Art von Zeilenoperation.

    Gruß,
    SP


Anmelden zum Antworten