Cześć wszystkim!
Program ogólnie mi działał i zaliczyłem nim ćwiczenie na zajęciach. (Jakimś cudem zwracał poprawne wartości.)
Aczkolwiek sam już też nie wiem, chat-GPT-4 też się załamał. Najwidoczniej jest jakiś problem z alokacją pamięci, mimo wykorzystania wszędzie wskaźników.
W programie tworzę Macierze, na które składają się elementy typu Wektor, na którego z kolei składają się elementy typu 'double'. Tak więc dodawanie Macierzy to wielokrotne dodawanie Wektorów (a których, to po prostu dodawanie doubli).
Zwracane wartości elementów Wektorów to albo zera albo jakieś losowe liczby wykładnicze.
Będę mega wdzięczny za komentarz weterana programowania, mój wykładowca to chyba ponad 80-letni facet i ciężko mu idzie rozumienie mojego problemu.
Embacandero - Compiler Name: TDM-GCC 9.2.0 64-bit Release zwraca:
(12, 14, 25) (11, 22, 99)
(10, 20, 30) (10, 20, 30)
(10, 20, 30) (11, 22, 99)
(10, 20, 30) (12, 14, 25)
(1.20896e-316, 0, 0) (0, 0, 0)
(1.20808e-316, 8.61746e-317, 1.20777e-316) (-2.95957e-44, 0, 0)
A Visual Studio Code z g++ (Rev4, Built by MSYS2 project) 13.1.0 zwraca nawet mniej:
(12, 14, 25) (11, 22, 99)
(10, 20, 30) (0, 0, 0)
Macierz M2:
(0, 0, 0) (11, 22, 99)
(10, 20, 30) (12, 14, 25)
Macierz M3 (suma M1 i M2):
(9.85394e-312, 9.85395e-312, 0) (
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdexcept>
using namespace std;
class Wektor
{
double x, y, z;
public:
Wektor() { x = y = z = 0.0; } // konstruktor bezargumentowy
Wektor(double x_, double y_, double z_) // konstruktor z argumentami
{
x = x_;
y = y_;
z = z_;
}
Wektor operator+(const Wektor &) const;
friend ostream &operator<<(ostream &wyj, const Wektor &vv);
friend istream &operator>>(istream &wej, Wektor &vv);
};
Wektor Wektor::operator+(const Wektor &v2) const
{
Wektor v3;
v3.x = x + v2.x;
v3.y = y + v2.y;
v3.z = z + v2.z;
return v3;
}
ostream &operator<<(ostream &wyj, const Wektor &vv)
{
wyj << "(" << vv.x << ", " << vv.y << ", " << vv.z << ")";
return wyj;
}
istream &operator>>(istream &wej, Wektor &vv)
{
wej >> vv.x >> vv.y >> vv.z;
return wej;
}
// %%%%%%%%%% KLASA MATRIX %%%%%%%%%%
class Matrix
{
Wektor **M; // prywatny wskaznik do wskaznika
int row, col;
public:
Matrix(int, int); // konstruktor
~Matrix(); // destruktor
int RowSize() { return row; }
int ColSize() { return col; }
Wektor &elem(int, int);
void operator=(Wektor);
Matrix operator+(const Matrix &) const;
friend ostream &operator<<(ostream &, Matrix &);
};
Matrix::Matrix(int New_row, int New_col)
{
row = New_row;
col = New_col;
M = new Wektor *[row];
if (!M)
{
throw bad_alloc(); // Rzuć wyjątek, gdy brakuje pamięci
}
for (int i = 0; i < row; i++)
{
M[i] = new Wektor[col];
if (!M[i])
{
throw bad_alloc();
}
for (int j = 0; j < col; j++)
{
Wektor V;
M[i][j] = V;
}
}
}
Matrix::~Matrix()
{
for (int i = 0; i < row; i++)
delete[] M[i];
delete[] M;
}
Wektor &Matrix::elem(int rw, int cl)
{
if (rw < 0 || rw >= row || cl < 0 || cl >= col)
{
throw out_of_range("ERROR: Row or column out of size in 'elem()'");
}
return M[rw][cl];
}
void Matrix::operator=(Wektor a)
{
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
M[i][j] = a;
}
Matrix Matrix::operator+(const Matrix &N) const
{
if (row != N.row || col != N.col)
{
throw invalid_argument("Macierze mają różne rozmiary");
}
Matrix S(row, col);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
S.M[i][j] = M[i][j] + N.M[i][j];
return S;
}
ostream &operator<<(ostream &os, Matrix &A)
{
int rw = A.RowSize();
int cl = A.ColSize();
os << "\n";
for (int i = 0; i < rw; i++)
{
for (int j = 0; j < cl; j++)
os << A.elem(i, j) << "\t";
os << "\n";
}
return os;
}
main()
{
Matrix M1(2, 2), M2(2, 2), M3(2, 2), M4(2, 2);
Wektor V1(12, 14, 25), V2(11, 22, 99), V3(10, 20, 30), V4(0, 0, 0);
M1.elem(0, 0) = V1;
M1.elem(0, 1) = V2;
M1.elem(1, 0) = V3;
M1.elem(1, 1) = V3;
M2.elem(0, 0) = V3;
M2.elem(0, 1) = V2;
M2.elem(1, 0) = V3;
M2.elem(1, 1) = V1;
M3 = M1 + M2;
cout << M1;
cout << M2;
cout << M3;
}