lab2 progress
This commit is contained in:
@@ -14,16 +14,60 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
// Test Setup
|
||||||
int length;
|
int length;
|
||||||
cout << "Enter desired length for row matrix:" << endl;
|
cout << "Enter the desired length for the matrix constructor test:" << endl;
|
||||||
cin >> length;
|
cin >> length;
|
||||||
|
|
||||||
|
cout << "Test - Constructor / Access Operator" << endl;
|
||||||
|
// Test: Constructor
|
||||||
Row row1(length);
|
Row row1(length);
|
||||||
for(int i = 0; i < length; i++){
|
|
||||||
cout << "before seg fault" << endl;
|
// Test: Access Operator (non const)
|
||||||
// Will use access operator instead
|
try{
|
||||||
//cout << row1.row_data[i] << " ";
|
for(int i = 0; i < length; i++){
|
||||||
|
cout << row1[i] << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
cout << row1[1] << endl;
|
||||||
|
//row1[1] = 12.18;
|
||||||
|
cout << row1[1] << endl;
|
||||||
|
cout << row1[length+1] << endl; // Might change
|
||||||
|
}catch(out_of_range){
|
||||||
|
cout << "Out of Range Exception Successfully Caught" << endl;
|
||||||
|
}
|
||||||
|
cout << "Test - Constructor/AccessOperator: PASS" << endl;
|
||||||
|
|
||||||
|
cout << "Test - Copy Constructor/AccessOperator(const)" << endl;
|
||||||
|
// Test: Copy Constructor
|
||||||
|
const Row row1Copy(row1);
|
||||||
|
cout << "before seg";
|
||||||
|
// Test: Access Operator (const)
|
||||||
|
try{
|
||||||
|
for(int i = 0; i < length; i++){
|
||||||
|
cout << row1Copy[i] << " ";
|
||||||
|
}
|
||||||
|
cout << row1Copy[1];
|
||||||
|
cout << row1Copy[length+1]; // Might change
|
||||||
|
}catch(out_of_range){
|
||||||
|
cout << "Out of Range Exception Successfully Caught" << endl;
|
||||||
|
}
|
||||||
|
cout << "Test - Copy Constructor / Access Operator (const): PASS" << endl;
|
||||||
|
|
||||||
|
// Test: Assignment Operator
|
||||||
|
Row row2(length*2);
|
||||||
|
row2 = row1;
|
||||||
|
for(int i = 0; i < length*2; i++){
|
||||||
|
cout << row2[i] << " ";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
|
|
||||||
|
// Test: Clear
|
||||||
|
row2.clear();
|
||||||
|
for(int i = 0; i < length*2; i++){
|
||||||
|
cout << row2[i] << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
37
Lab2/row.cpp
37
Lab2/row.cpp
@@ -4,29 +4,26 @@ using namespace std;
|
|||||||
|
|
||||||
// parameterized constructor
|
// parameterized constructor
|
||||||
Row::Row(unsigned int length){
|
Row::Row(unsigned int length){
|
||||||
cout << "-Row Constructor-" << endl; // TODO: temp
|
|
||||||
// Set matrix length private variable to passed in length
|
// Set matrix length private variable to passed in length
|
||||||
this->length = length;
|
this->length = length;
|
||||||
// Create new array in heap for row_data
|
// Create new array in heap for row_data
|
||||||
this->row_data = new double[length];
|
this->row_data = new double[length];
|
||||||
// Clear all values in new array to 0
|
// Clear all values in new array to 0
|
||||||
for(int i = 0; i < length; i++) {
|
clear();
|
||||||
cout << "-Row Constructor-" << endl; // TODO: temp
|
|
||||||
this->row_data[i] = 0.0;
|
|
||||||
}
|
|
||||||
// finish
|
// finish
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy constructor
|
// copy constructor
|
||||||
Row::Row(const Row& from){
|
Row::Row(const Row& from){
|
||||||
cout << "-Row Copy Constructor-" << endl; // TODO: temp
|
|
||||||
// New row matrix gets length from previous matrix
|
// New row matrix gets length from previous matrix
|
||||||
this->length = from.length;
|
this->length = from.length;
|
||||||
// Create new array in heap with new length
|
// Create new array in heap with new length
|
||||||
this->row_data - new double[this->length];
|
this->row_data - new double[this->length];
|
||||||
// Copy all row_data values over to new array
|
// Copy all row_data values over to new array
|
||||||
for(int i = 0; i < this->length; i ++) {
|
for(int i = 0; i < this->length; i++) {
|
||||||
this->row_data[i] = from.row_data[i];
|
cout << "before seg fault" << endl;
|
||||||
|
cout << this->row_data[i] << endl;
|
||||||
|
this->row_data[i] = from[i];
|
||||||
}
|
}
|
||||||
// finish
|
// finish
|
||||||
}
|
}
|
||||||
@@ -38,7 +35,6 @@ Row::~Row(){
|
|||||||
if(this->length > 0 ) {
|
if(this->length > 0 ) {
|
||||||
delete[] row_data;
|
delete[] row_data;
|
||||||
}
|
}
|
||||||
//finish
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// access operator (const)
|
// access operator (const)
|
||||||
@@ -59,12 +55,27 @@ double& Row::operator[](unsigned int column){
|
|||||||
|
|
||||||
// assignment operator
|
// assignment operator
|
||||||
Row& Row::operator= (const Row& rhs){
|
Row& Row::operator= (const Row& rhs){
|
||||||
//finish
|
if(&rhs != this){
|
||||||
Row result(1);
|
// Delete the current row matrix
|
||||||
return result;
|
if(length > 0){
|
||||||
|
delete[] this->row_data;
|
||||||
|
}
|
||||||
|
// New row matrix gets length from previous matrix
|
||||||
|
this->length = rhs.length;
|
||||||
|
// Create new array in heap with new length
|
||||||
|
this->row_data - new double[this->length];
|
||||||
|
// Copy all row_data values over to new array
|
||||||
|
for(int i = 0; i < this->length; i ++) {
|
||||||
|
this->row_data[i] = rhs.row_data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Return address of the Row
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear row data
|
// clear row data
|
||||||
void Row::clear(){
|
void Row::clear(){
|
||||||
//finish
|
for(int i = 0; i < length; i++) {
|
||||||
|
this->row_data[i] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user