lab 2 start
This commit is contained in:
29
Lab2/main.cpp
Normal file
29
Lab2/main.cpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/**
|
||||||
|
* @file main.cpp
|
||||||
|
* @author Trevor Barnes (barnestr@msoe.edu)
|
||||||
|
* @brief
|
||||||
|
* @version 1.0
|
||||||
|
* @date 2022-03-22
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2022
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include "row.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int length;
|
||||||
|
cout << "Enter desired length for row matrix:" << endl;
|
||||||
|
cin >> length;
|
||||||
|
Row row1(length);
|
||||||
|
for(int i = 0; i < length; i++){
|
||||||
|
cout << "before seg fault" << endl;
|
||||||
|
// Will use access operator instead
|
||||||
|
cout << row1.row_data[i] << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
25
Lab2/makefile
Normal file
25
Lab2/makefile
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# lab2 Makefile
|
||||||
|
|
||||||
|
CC = g++
|
||||||
|
CFLAGS = -c -MMD
|
||||||
|
LFLAGS =
|
||||||
|
# Change w/ every new project
|
||||||
|
SOURCES = main.cpp row.cpp
|
||||||
|
OBJECTS = $(SOURCES:.cpp=.o)
|
||||||
|
# Change w/ every new project
|
||||||
|
EXECUTABLE = lab2
|
||||||
|
|
||||||
|
all: $(EXECUTABLE) $(SOURCES)
|
||||||
|
|
||||||
|
$(EXECUTABLE): $(OBJECTS)
|
||||||
|
$(CC) $(LFLAGS) -o $@ $(OBJECTS)
|
||||||
|
|
||||||
|
-include *.d
|
||||||
|
|
||||||
|
%.o:%.cpp
|
||||||
|
$(CC) $(CFLAGS) $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(EXECUTABLE)
|
||||||
|
rm -f $(OBJECTS)
|
||||||
|
rm -f *.d
|
||||||
68
Lab2/row.cpp
Normal file
68
Lab2/row.cpp
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#include<iostream>
|
||||||
|
#include "row.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// parameterized constructor
|
||||||
|
Row::Row(unsigned int length){
|
||||||
|
cout << "-Row Constructor-" << endl; // TODO: temp
|
||||||
|
// Set matrix length private variable to passed in length
|
||||||
|
this->length = length;
|
||||||
|
// Create new array in heap for row_data
|
||||||
|
this->row_data = new double[length];
|
||||||
|
// Clear all values in new array to 0
|
||||||
|
for(int i = 0; i < length; i++) {
|
||||||
|
cout << "-Row Constructor-" << endl; // TODO: temp
|
||||||
|
this->row_data[i] = 0.0;
|
||||||
|
}
|
||||||
|
// finish
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy constructor
|
||||||
|
Row::Row(const Row& from){
|
||||||
|
cout << "-Row Copy Constructor-" << endl; // TODO: temp
|
||||||
|
// New row matrix gets length from previous matrix
|
||||||
|
this->length = from.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] = from.row_data[i];
|
||||||
|
}
|
||||||
|
// finish
|
||||||
|
}
|
||||||
|
|
||||||
|
// destructor
|
||||||
|
Row::~Row(){
|
||||||
|
cout << "-Row Destructor-" << endl; // TODO: temp
|
||||||
|
// Check for valid length then free the heap memory
|
||||||
|
if(this->length > 0 ) {
|
||||||
|
delete[] row_data;
|
||||||
|
}
|
||||||
|
//finish
|
||||||
|
}
|
||||||
|
|
||||||
|
// access operator (const)
|
||||||
|
double Row::operator[](unsigned int column) const{
|
||||||
|
//finish
|
||||||
|
double result;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// access operator (non-const)
|
||||||
|
double& Row::operator[](unsigned int column){
|
||||||
|
//finish
|
||||||
|
double result;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// assignment operator
|
||||||
|
Row& Row::operator= (const Row& rhs){
|
||||||
|
//finish
|
||||||
|
Row result(1);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// clear row data
|
||||||
|
void Row::clear(){
|
||||||
|
//finish
|
||||||
|
}
|
||||||
51
Lab2/row.h
Normal file
51
Lab2/row.h
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#ifndef row_h
|
||||||
|
#define row_h
|
||||||
|
class Row{
|
||||||
|
public:
|
||||||
|
/* Parameterized constructor
|
||||||
|
* Takes in length and creates a row matrix with values cleared
|
||||||
|
* to zero
|
||||||
|
*/
|
||||||
|
Row(unsigned int length);
|
||||||
|
|
||||||
|
/* Copy constructor
|
||||||
|
* Create a new row matrix with the same size and values as the
|
||||||
|
* from matrix
|
||||||
|
*/
|
||||||
|
Row(const Row& from);
|
||||||
|
|
||||||
|
/* Destructor
|
||||||
|
* Correctly delete any heap memory
|
||||||
|
*/
|
||||||
|
~Row();
|
||||||
|
|
||||||
|
/* Access operator (const version)
|
||||||
|
* Allow access to row matrix data
|
||||||
|
* Should return an exception if column is too large
|
||||||
|
*/
|
||||||
|
double operator[](unsigned int column) const;
|
||||||
|
|
||||||
|
/* Access operator (non const version)
|
||||||
|
* Allow access to row matrix data
|
||||||
|
* Should return an exception if column is too large
|
||||||
|
*/
|
||||||
|
double& operator[] (unsigned int column);
|
||||||
|
|
||||||
|
/* Assignment operator
|
||||||
|
* 1. Check if two sides are the same object
|
||||||
|
* 2. Delete the current row matrix
|
||||||
|
* 3. Create a new row matrix with the same size and values as
|
||||||
|
* the rhs matrix
|
||||||
|
*/
|
||||||
|
Row& operator= (const Row& rhs);
|
||||||
|
|
||||||
|
/* Clear all data values to zero
|
||||||
|
*/
|
||||||
|
void clear();
|
||||||
|
private:
|
||||||
|
// Row matrix data
|
||||||
|
double * row_data;
|
||||||
|
// Size of row matrix
|
||||||
|
unsigned int length;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user