diff --git a/Oving 9/Oving 9/Dummy.cpp b/Oving 9/Oving 9/Dummy.cpp new file mode 100644 index 0000000..0e4e97a --- /dev/null +++ b/Oving 9/Oving 9/Dummy.cpp @@ -0,0 +1,39 @@ +#include "pch.h" +#include "Dummy.h" + + + +void dummyTest() +{ + Dummy a; + *a.num = 4; + Dummy b{ a }; + Dummy c; + c = a; + cout << "a: " << *a.num << endl; + cout << "b: " << *b.num << endl; + cout << "c: " << *c.num << endl; + + *b.num = 3; + *c.num = 5; + + cout << "a: " << *a.num << endl; + cout << "b: " << *b.num << endl; + cout << "c: " << *c.num << endl; + + cin.get(); + +} + +Dummy::Dummy(const Dummy & other) : num{nullptr} +{ + this->num = new int{}; + *num = *other.num; +} + +Dummy &Dummy::operator=(Dummy rhs) +{ + swap(num, rhs.num); + return *this; +} + diff --git a/Oving 9/Oving 9/Dummy.h b/Oving 9/Oving 9/Dummy.h new file mode 100644 index 0000000..4dbe1cb --- /dev/null +++ b/Oving 9/Oving 9/Dummy.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +using namespace std; + +class Dummy +{ +public: + int *num; + + Dummy() + { + num = new int{ 0 }; + } + + Dummy(const Dummy &other); + + Dummy &operator=(Dummy rhs); + + ~Dummy() + { + delete num; + } +}; + +void dummyTest(); + diff --git a/Oving 9/Oving 9/Fibonacci.cpp b/Oving 9/Oving 9/Fibonacci.cpp index ef027f8..827b017 100644 --- a/Oving 9/Oving 9/Fibonacci.cpp +++ b/Oving 9/Oving 9/Fibonacci.cpp @@ -3,5 +3,43 @@ void fillInFibonacciNumbers(int result[], int length) { + int lo = 0; + int hi = 1; + + int sum; + + for (int i = 0; i < length; ++i) + { + result[i] = lo; + sum = lo + hi; + lo = hi; + hi = sum; + } + +} + +void printArray(int arr[], int length) +{ + for (int i = 0; i < length; ++i) + { + cout << arr[i] << endl; + } +} + +void createFibonacci() +{ + int length; + + cout << "How many fibonacci numbers? "; + cin >> length; + + int *numbers = new int[length] {}; + + fillInFibonacciNumbers(numbers, length); + + printArray(numbers, length); + + delete[] numbers; + numbers = nullptr; } diff --git a/Oving 9/Oving 9/Fibonacci.h b/Oving 9/Oving 9/Fibonacci.h index 407e0f5..7528b57 100644 --- a/Oving 9/Oving 9/Fibonacci.h +++ b/Oving 9/Oving 9/Fibonacci.h @@ -1,5 +1,8 @@ #pragma once +#include + + using namespace std; void fillInFibonacciNumbers(int result[], int length); diff --git a/Oving 9/Oving 9/Matrix.cpp b/Oving 9/Oving 9/Matrix.cpp new file mode 100644 index 0000000..8838e95 --- /dev/null +++ b/Oving 9/Oving 9/Matrix.cpp @@ -0,0 +1,190 @@ +#include "pch.h" +#include "Matrix.h" +#include +#include +#include +#include + +using namespace std; +///////////// +// Private // +///////////// + +//Finds the longest number in the matrix +int Matrix::findLongestNumber() const +{ + int longest = 0; + if (isValid()) + { + int word; + ostringstream s; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + s << get(i, j); + word = s.str().length(); + s.str(""); + s.clear(); + if (word > longest) + longest = word; + } + } + } + return longest; +} + + +//////////// +// Public // +//////////// + +// Default +Matrix::Matrix() +{ + matrix = nullptr; + rows = 0, cols = 0; // Illegal size +} + +// General +Matrix::Matrix(const int nRows, const int nColumns) +{ + // To make cleanup easier + rows = nRows; + cols = nColumns; + + matrix = new double*[nRows] {}; // Create a list of pointers to array of ints + + for (int i = 0; i < nRows; ++i) + { + matrix[i] = new double[nColumns] {0}; // Fill the list with an array of ints + for (int j = 0; j < nColumns; j++) + { + matrix[i][j] = 0; + } + } +} + +// Identity-matrix +Matrix::Matrix(int nRows) : Matrix(nRows, nRows) +{ + for (int i = 0; i < nRows; i++) + { + matrix[i][i] = 1; // Set the default diagonal to 1 + } +} + +// Set-get-functions +double Matrix::get(int row, int col) const +{ + return matrix[row][col]; +} + +void Matrix::set(int row, int col, double value) +{ + matrix[row][col] = value; +} + +int Matrix::getRows() const +{ + return rows; +} + +int Matrix::getCols() const +{ + return cols; +} + +bool Matrix::isValid() const +{ + if (matrix == nullptr || rows * cols == 0) // If the size is 0 in either direction, then the matrix is invalid + return false; + return true; +} + +// Outsream-operator +ostream & operator<<(ostream & os, const Matrix & m) +{ + if (m.isValid()) + { + int width = m.findLongestNumber(); + for (int i = 0; i < m.rows; i++) + { + for (int j = 0; j < m.cols; j++) + { + os << ' ' << setw(width) << m.get(i, j); + } + os << endl; + } + return os; + } + // If the matrix is non-valid, print this + return os << "The matrix is in an invalid state" << endl; +} + +// Copy-constructor +Matrix::Matrix(const Matrix & rhs) : matrix{nullptr} +{ + this->rows = rhs.rows; // Copy size + this->cols = rhs.cols; // Copy size + this->matrix = new double*[rhs.rows] {}; // Create a list of pointers to array of ints + for (int i = 0; i < this->rows; ++i) + { + matrix[i] = new double[this->cols] {0}; // Allocate the right amount of memory + for (int j = 0; j < this->cols; j++) + { + this->matrix[i][j] = rhs.matrix[i][j]; // Copy all values + } + } +} + +// Copy-operator +Matrix & Matrix::operator=(Matrix rhs) +{ + swap(rows, rhs.rows); + swap(cols, rhs.cols); + swap(matrix, rhs.matrix); + return *this; +} + +// Add rhs to lhs (RightHandSide and LeftHandSide) +Matrix & Matrix::operator+=(const Matrix & rhs) +{ + if (this->isValid() && rhs.isValid() && this->rows == rhs.rows && this->cols == rhs.cols) + { + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + this->matrix[i][j] += rhs.get(i, j); + } + } + return *this; + } + this->matrix = nullptr; + return *this; +} + + + +// Binary-add-operator +Matrix operator+(Matrix lhs, const Matrix & rhs) +{ + lhs += rhs; + return lhs; +} + + +Matrix::~Matrix() +{ + if (isValid()) + { + for (int i = 0; i < rows; i++) + { + delete[] matrix[i]; // Delete all pointers to array of ints + } + delete[] matrix; // Delete all pointers to array of pointers + } + + matrix = nullptr; // Sets the matrix to an illegal state +} diff --git a/Oving 9/Oving 9/Matrix.h b/Oving 9/Oving 9/Matrix.h new file mode 100644 index 0000000..ea66eab --- /dev/null +++ b/Oving 9/Oving 9/Matrix.h @@ -0,0 +1,45 @@ +#pragma once +#include + +using namespace std; + + +class Matrix +{ + double **matrix; // Array of pointers to pointers + int rows, cols; + + int findLongestNumber() const; + +public: + // Constructors + Matrix(); // default + Matrix(const int nRows, const int nColumns); // Most common + explicit Matrix(int nRows); // Identity matrix + + // Set-get + double get(int row, int col) const; + void set(int row, int col, double value); + + int getRows() const; + int getCols() const; + + // Check if matrix is valid + bool isValid() const; + + // Out-operator + friend ostream& operator<<(ostream& os, const Matrix& m); + + // Copy-operators + Matrix(const Matrix &rhs); + Matrix &operator=(Matrix rhs); + + // Other operators + Matrix &operator+=(const Matrix &rhs); + friend Matrix operator+(Matrix lhs, const Matrix &rhs); + + + // Destructor + ~Matrix(); +}; + diff --git a/Oving 9/Oving 9/Oving 9.cpp b/Oving 9/Oving 9/Oving 9.cpp index 8150102..fb3cd8f 100644 --- a/Oving 9/Oving 9/Oving 9.cpp +++ b/Oving 9/Oving 9/Oving 9.cpp @@ -3,21 +3,119 @@ #include "pch.h" #include +#include +#include +#include + +#include "Fibonacci.h" +#include "Matrix.h" +#include "Dummy.h" using namespace std; int main() { - cout << "Hello World!\n"; + // createFibonacci(); + + /* + Matrix m1{ 10 }; + + Matrix m2{ m1 }; + + Matrix m3; + + Matrix m4{ 4, 5 }; + + m3 = m2; + + m1.set(0, 0, 2); + + m3.set(0, 1, 10); + + m2.set(0, 1, 42); + + cout << m1 << endl; + + cout << m2 << endl; + + cout << m3 << endl; + + m3 += m2; + + cout << m3 << endl; + + m3 += m4; + + cout << m3 << endl; + + */ + + Matrix A{ 2,2 }; + Matrix B{ 2,2 }; + Matrix C{ 2,2 }; + + A.set(0, 0, 1); + A.set(0, 1, 2); + A.set(1, 0, 3); + A.set(1, 1, 4); + + B.set(0, 0, 4); + B.set(0, 1, 3); + B.set(1, 0, 2); + B.set(1, 1, 1); + + C.set(0, 0, 1); + C.set(0, 1, 3); + C.set(1, 0, 1.5); + C.set(1, 1, 2); + + cout << A << endl; + cout << B << endl; + cout << C << endl; + + A += B + C; + + cout << A << endl; + cout << B << endl; + cout << C << endl; + +#pragma region Task 3 + + /* + Når dummy kjører vil det skrives ut: + + "a: 4" + "b: 4" + "c: 4" + + "a: 4" + "b: 3" + "c: 5" + + */ + + //dummyTest(); // Stemmer ikke overens med ovenfor + + /* Skriver ut + "a: 4" + "b: 4" + "c: 4" + + "a: 5" + "b: 5" + "c: 5" + */ + + /* 3c) + Etter kopikonstruktøren fungerer Dummy b + */ + + /* 3d) + Etter =-operatøren fungerer programmet som forventet + */ + +#pragma endregion + + return 0; } -// Run program: Ctrl + F5 or Debug > Start Without Debugging menu -// Debug program: F5 or Debug > Start Debugging menu - -// Tips for Getting Started: -// 1. Use the Solution Explorer window to add/manage files -// 2. Use the Team Explorer window to connect to source control -// 3. Use the Output window to see build output and other messages -// 4. Use the Error List window to view errors -// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project -// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file diff --git a/Oving 9/Oving 9/Oving 9.vcxproj b/Oving 9/Oving 9/Oving 9.vcxproj index b2892e5..301a142 100644 --- a/Oving 9/Oving 9/Oving 9.vcxproj +++ b/Oving 9/Oving 9/Oving 9.vcxproj @@ -151,11 +151,15 @@ + + + + Create diff --git a/Oving 9/Oving 9/Oving 9.vcxproj.filters b/Oving 9/Oving 9/Oving 9.vcxproj.filters index 5022b03..6998a7d 100644 --- a/Oving 9/Oving 9/Oving 9.vcxproj.filters +++ b/Oving 9/Oving 9/Oving 9.vcxproj.filters @@ -21,6 +21,12 @@ Header Files + + Header Files + + + Header Files + @@ -32,5 +38,11 @@ Source Files + + Source Files + + + Source Files + \ No newline at end of file