I am having some basic ADT matrix issues. Here is what my goal is:
Write a specification for the SquareMatrix ADT. Remember, an ADT specification should have function, precondition, postcondition, parameters (with types), and return values (with types) for each method. Include at least the following operations.
• MakeEmpty(n), which sets the first n rows and columns to 0 and can perform other initialization operations as needed
• StoreValue(i,j,value), which stores the given value into the [i,j] position of the matrix
• Add, which adds two matrices together. You should allow a parameter to store the result.
• Subtract, which subtracts one matrix from another. You show allow a parameter to store the result.
• Print, which outputs the matrix to the console. It is fine to use a simple format such as the one given in the SquareMatrix example above.
• Copy, which copies one matrix into another
You may assume integers for parameters (or floats if you prefer, but you do not have to worry about template types; simply choose int or float).
You should use the parameter (n) to MakeEmpty as the given size of the matrix. In other words, in testing, you can assume you will always make a call to MakeEmpty first, which sets the size of the matrix. For all cases where the user tries to perform operations that are invalid on a matrix (such as adding or subtracting matrices of different sizes), it’s fine to output an error and simply refuse to perform the operation. However, you should make sure you check for these cases and output an error!
You may assume that Add and Subtract take a parameter “result” that is a SquareMatrix where the result is stored. For Copy, you may assume that one matrix is copied into the other and overwritten. Make sure you document which one is overwritten!
Part 2: Class Declaration
Convert your specification to a C++ class declaration. Remember to comment it well! It should compile; make sure it does before moving on to Part 3.
Part 3: Implement the Member Functions
Implement the individual member functions from your SquareMatrix ADT. Make sure everything compiles and comments well. You should have class-level comments, member function/method/variable level comments, and in-line comments.
Part 4: Create a Driver
Now that you are done, are you sure it works? Create a Main function that will provide a driver or interface to your program. Your driver should hold three SquareMatrix datatypes referred to below as positions 1, 2, and 3. Hence you can test an add/subtract by creating a matrix in positions 1 and 2 and then adding them with the result in position 3.
The test driver should prompt the user in a simple menu fashion (in text on the console). There’s no need to get fancy here. A simple multiple choice input is fine (type the letter of the command you want). You can ask for parameters separately on a new line with a prompt or simply pass them in one line—it’s your choice. For example, either “a 1” or “a” then “1” is fine for the create process.
You should create three instances of SquareMatrix so you can test all the ADT functions. Your test driver should have at least the following capabilities.
a) Create new SquareMatrix and store in position 1, 2, or 3
b) MakeEmpty ‘n’ rows/columns from matrix position 1, 2, or 3
c) StoreValue i,j,value to the matrix in position 1, 2, or 3
d) Add matrices in one position to another.
e) Subtract matrices in one position from another.
f) Print the matrix in a particular position.
g) Copy one matrix into another.
Think about your Copy command. Should this be a shallow or a deep copy?
Part 5: Testing
Create a test plan and use the driver you created in Part 4 to test your ADT. Make sure you reference the sections in your textbook about test plans and use the examples to help you.
Execute your test plan and write up your results. Does everything work? If not, fix and retest. Submit your final test results, which includes any issues you are having with your program, and explain what tests you ran (describe your test plan).
First bit of code is my Header
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#ifndef mat_init_h
#define mat_init_h
using namespace std;
int row, col;
class matrix3x3
{
public:
matrix3x3();
void matkeEmpty();
void StoreValue(int row, int col, int value);
void Add(const matrix3x3 & two, matrix3x3 & result);
void Subtract(const matrix3x3 & two, matrix3x3 & result);
void Print() const;
void Copy(const matrix3x3 & two);
private:
int matSize;
int mat[50][50];
};
#endif
|
Here is the cpp... this is where i think my issue is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <conio.h>
#include "mat_Int.h"
using namespace std;
matrix3x3::matrix3x3()
{
//empty matrix
matkeEmpty();
//set matrix size to 50
matSize = 50;
}
void matrix3x3::matkeEmpty()
{
//empty row and col and set to 0
for (row = 0; row < matSize; row++)
//> initiate rows and cols
for (col = 0; col < matSize; col++)
mat[row][col] = 0; // set to 0
}
void matrix3x3::StoreValue(int row, int col, int Value)
{
//store values of the matrix
if (row < matSize)
if (col < matSize)
mat[row][col] = Value; //set values to row and col
}
void matrix3x3::Add(const matrix3x3 & two, matrix3x3 & result)
{
//add matrix 1 and 2
for (row = 0; row < matSize; row++)
//> initiate rows and cols
for (col = 0; col < matSize; col++)
result.mat[row][col] = mat[row][col] +
two.mat[row][col]; //function adds 2 matrices
}
int matrix3x3::Subtract(const matrix3x3 & two, matrix3x3 & result)
{
//subtract matrix 1 and 2
for (row = 0; row < matSize; row++)
//> initiate rows and cols
for (col = 0; col < matSize; col++)
result.mat[row][col] = mat[row][col] -
two.mat[row][col]; //function subtracts 2 matrices
}
void matrix3x3::Print() const
{
//print matrices
for (row = 0; row < matSize; row++)
//> initiate rows and cols
for (col = 0; col < matSize; col++)
cout << mat[row][col];
// >function prints the matrix
cout << " ";
}
void matrix3x3::Copy(const matrix3x3 & two)
{
//copy matrix 1 to matrix 2
for (row = 0; row < matSize; row++)
//> initiate rows and cols
for (col = 0; col < matSize; col++)
mat[row][col] = two.mat[row][col]; //copy function
}
int main()
{
matrix3x3 first, second, third, result;
cout << "first matrix : ";
first.matkeEmpty();
first.StoreValue(row, col, 1);
cout << endl << "second matrix : ";
second.matkeEmpty();
second.StoreValue(row, col, 2);
cout << endl << "third matrix : ";
third.matkeEmpty();
third.StoreValue(row, col, 3);
cout << endl << "add matrices 1 and 2 : ";
result = first + second;
result.Print();
cout << endl << "subtract matrices 2 and 3";
result = second - third;
result.Print();
cout << endl << "copy matrices : ";
first.Copy(first);
cout << " 1 now equals 2 ";
_getch();
return 0;
}
|
Please any help would be awesome.