Click to See Complete Forum and Search --> : C++ help
red frost
June 12th, 2002, 11:49 PM
I need help on code for a n X n square matrix i have the code for the specification file and some for the class declaration but now i'm kinda of stuck
SquareMatrix ADT Specification
Structure: A N x N square integer matrix
Operations:
MakeEmpty(int n)
Function: Initializes the size of the matrix n x n and set the values to zero
Precondition: n is less than or equal to 50
Postcondition: Matrix contains all zero values
StoreValue(int I, int j, int value)
Function: Stores value into the I,jth position in the matrix
Precondition: Matrix has been initialized and are the same size
Postcondition: value has been stored in I, jth position of the matrix
Add(SquareMatrixType one, SquareMatrixType result)
Function: Adds self and matrix one and stores the result in result
Precondition: one and two have been initialized and are the same size
Postcondition: result = self + two
Subtract(SquareMatrixType one, SquareMatrixType result)
Function: Subtracts one from self and stores the result in result
Precondition: self and one have been initialized and are the same size
Postcondition: result = self – two
Print
Function: Prints the matrix on the screen
Precondition: Matrix has been initialized
Postcondition: The values in the matrix have been printed by row on the screen
Copy(SquareMatrixType one, SquareMatrixType two)
Function: Copies two into one
Precondition: two has been initialized
Postcondition: one = two
class SquareMatrixType
{
public:
void MakeEmpty(int n);
void StoreValue(int I, int j, int value);
void Add(SquareMatrixType one, SquareMatrixType result);
void Subtract(SquareMatrixType one, SquareMatrixType result);
void Print();
void Copy(SquareMatrixType one, SquareMatrixType two);
private:
int items [Max_Rows][Max_Cols];
int numRows;
int numCols;
jfaust
June 13th, 2002, 09:13 AM
So, I'm guessing you're taking Data Structures, and as a summer course nonetheless. A friend of mine just finished this project.
Well, I'd suggest trying it further and then ask specific questions if you have any. I really hope nobody here will do your homework for you.
I suggest trying to implement these functions yourself. If you get stuck with them, or have problems later, then come back and ask for help. No one is going to write these for you.
With that said, the MakeEmpty(), StoreValue(), and Print() functions are extremely trivial to implement. You should be able to do those very easily by yourself before you start on Add(), Copy() and Subtract().
Also, don't forget to write a constructor for your class since you will probably want to initialize the matrix somehow when you create it.
private:
int matrix[MAX_SIZE-1][MAX_SIZE-1];
};
#endif
.cpp file
#include "SquareMatrix.h"
SquareMatrix::SquareMatrix(const int matrixArray[][MAX_SIZE-1])
{
for (int row = 0; row < MAX_SIZE; row++)
{
for (int col = 0; col < MAX_SIZE; col++)
{
matrix[row][col] = matrixArray[row][col];
}
}
}
void SquareMatrix::MakeEmpty(int n)
{
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
matrix[row][col] = 0;
}
}
}
void SquareMatrix::StoreValue(int i, int j, int value)
{
matrix[i][j] = value;
}
void SquareMatrix::Add(SquareMatrix a, SquareMatrix b)
{
}
void SquareMatrix::Subtract(SquareMatrix a, SquareMatrix b)
{
}
void SquareMatrix::Copy(SquareMatrix a, SquareMatrix b)
{
}
First off, how do my constructor and first 2 functions look?
I am pretty sure I did these right.
Now for add and subtract I am sure it will be in the form on main as c = c.Add(a, b)
but I am not sure how to do this in the actual .cpp clas file. I am passing in two instances but since I am not returning anything how is that value going to get stored? Or maybe I do not understand the concept of the add.
Does it mean to add two storage values in the one SquareMatrix, or add all storage values in two seperate SquareMatrixes....?
This actually is not a homework problem for myself.....my teacher just gives projects. However I like to try and answer each question....usually small programs....at the end of chapters because it helps me remember things.
Thanks in advance for anybodys input
dcjr84
October 4th, 2006, 08:11 PM
A few things...
1) You could make your constructor much more simple. The idea behind a constructor really is to just initialize the object's instance variables with meangingful values. I think in your case it will be simple enough to just set all the array elements to zero so that they are initialized and do not contain some garbage values. So just call the MakeEmpty() function in your constructor.
Note: you don't need to pass anything to the constructor in this case because it is being called for a specific object which you have already created.
SquareMatrix::SquareMatrix()
{
MakeEmpty();
}
Now, you could override the copy constructor to take an array as an argument so you can copy one matrix into another, but you have to write your own Copy() function anyways it seems.
2) Don't pass any arguments to the MakeEmpty function. The number of row and columns of your matrix are data members of your class, and already accessible by all member functions of your class.
void SquareMatrix::MakeEmpty()
{
for (int row = 0; row < MAX_SIZE; row++)
{
for (int col = 0; col < MAX_SIZE; col++)
{
matrix[row][col] = 0;
}
}
}
In your case, you are working with only square matrices ( which is probably best because they are the easiest to work with for most operations ). MAX_SIZE is the number of rows and columns.
3) Check for out of bounds error in your StoreValue() function. You need to make sure that a person doesn't try to assign a value to some index of the array which is not valid, such as 100 or something.
void SquareMatrix::StoreValue(int i, int j, int value)
{
if ( i < MAX_SIZE && j < MAX_SIZE )
matrix[ i ][ j ] = value;
else
cout << "You have entered an incorrect index! " << endl;
}
4) Adding and subtracting matrices is easy. You just add/subtract the corresponding elements of each matrix.
Now for add and subtract I am sure it will be in the form on main as c = c.Add(a, b)
You should do it this way.
c = a.Add( b );
where c is a temporary object to store the result of the addition/subtraction
a is the first matrix
b is the second matrix passed to the a object's Add() function
But to do this, you will need to overload the assignment operator ( = ) because C++ does not support copying of standard arrays. Meaning when you assign the result of adding/subtracting the two arrays into object c, you need to specify how to copy that array into the new object. It's not difficult to do so, but it needs to be done.
Change your class definition to reflect these changes.
It would also probably be a good idea to make the array dynamic, so you could specify how big the matrix should be at run time. Right now, the rows and columns are locked at 50, and cannot be changed during run-time. But I am not sure if you have gotten that far in your studies yet.
Have fun :)
slude
October 4th, 2006, 08:41 PM
Thank you very much for your help with the constructor....now that you mentioned that I remembered doing that for other programs...i.e set functions.
However there is one flaw in your suggestion. You cannot assign values to ints in .h files unless it is done this way
private:
int MAX_SIZE = 50;
int matrix[MAX_SIZE][MAX_SIZE];
};
#endif
However my question was still not answered............what exactly do they mean by add() and subtract() functions. ADD AND SUBTRACT WHAT?
Lastly
It would also probably be a good idea to make the array dynamic, so you could specify how big the matrix should be at run time. Right now, the rows and columns are locked at 50, and cannot be changed during run-time.
I have no idea what that means but the word dynamic rings a bell. Do not really remember what it means though...ill look it up
But if someone could please help me understand what the add() and subtract() functions mean I would be very greatful.
Thanks.
dcjr84
October 4th, 2006, 08:48 PM
Here is a web link (http://etap.org/demo/algebra2/instruction2tutor.html) with more info on how to do add/subtract matrices if you need it. It is really easy.
Another website for adding and subtracting (http://www.purplemath.com/modules/mtrxadd.htm) matrices
Hopefully you can see the similarities between adding and subtracting matrices, and adding and subtracting 2-dimensional arrays which abstractly represent matrices.
However there is one flaw in your suggestion. You cannot assign values to ints in .h files unless it is done this way
You are correct. That was my mistake. The data members cannot be initialized in their declaration in the class. This must be done in the constructor, or as a global variable. You can leave it the way you have it now, it's not a big deal.
It helps a little bit if you have taken Linear Algebra when working with matrices. Any more questions, feel free to post again :)
slude
October 5th, 2006, 01:46 AM
finally finished this.......thought i would just post the final touches....thanks agiain for the help
class SquareMatrix
{
public:
void MakeEmpty(int n);
// Pre: n is less than or equal to 50.
// Post: n has been stored into size.
void StoreValue(int i, int j, int value);
// Pre: i and j are less than or equal to size - 1.
// Post: matrix[i][j] = value.
void Add(SquareMatrix two, SquareMatrix result);
// Post: result = self + two.
void Subtract(SquareMatrix two, SquareMatrix result);
// Post: result = self - two.
void Print();
// Post: The values in self have been printed by row on
// the screen.
void Copy(SquareMatrix two);
// Post: self = two
private:
int size;
int matrix[50][50];
};
#endif
#include "SquareMatrix.h"
#include <iostream>
using namespace std;
void SquareMatrix::MakeEmpty(int n)
{
size = n;
for (int row = 0; row < size; row++)
for (int col = 0; col < size; col++)
matrix[row][col] = 0;
}
void SquareMatrix::StoreValue(int i, int j, int value)
{
matrix[i][j] = value;
}
void SquareMatrix::Add(SquareMatrix two, SquareMatrix result)
{
int row, col;
for (row = 0; row < size; row++)
for (col = 0; col < size; col++)
result.matrix[row][col] = matrix[row][col] +
two.matrix[row][col];
}
void SquareMatrix::Subtract(SquareMatrix two, SquareMatrix result)
{
int row, col;
for (row = 0; row < size; row++)
for (col = 0; col < size; col++)
result.matrix[row][col] = matrix[row][col] -
two.matrix[row][col];
}
void SquareMatrix::Print()
{
int row, col;
for (row = 0; row < size; row++)
for (col = 0; col < size; col++)
cout << matrix[row][col] << " ";
}
void SquareMatrix::Copy(SquareMatrix two)
{
int row, col;
for (row = 0; row < size; row++)
for (col = 0; col < size; col++)
matrix[row][col] = two.matrix[row][col];
}
I don't see why you are passing a value to make empty. You already know the number of rows and columns of the matrix you are emptying. It is always 50. No need to have a size variable. You were better off using the global constant variable MAX_SIZE in place of 50 like you had before. The size of the matrix cannot be changed once the program is run in your case, it must be known at compile-time, so there is no need for a private data member called size.
However, if you insist on having one so you don't have to re-use the number 50 a million times, initialize it in your constructor and not some other function. This is the purpose of the constructor.
Also, you still have no bounds checking in StoreValue(). This is necessary to ensure you don't write values out of bounds of the array. Don't do this in the main() function of your code, do it right here in the function of your class. This is the whole idea behind data encapsulation in classes: data and functions which are bound together into a single entity.
The const corrections and reference additions made above are also needed. Objects passed to Add() / Subtract() / Copy ()functions which are not modified should be const, and all objects passed should be by reference ( & ) in this case so that a temporary copy of the object is not made when you call the function, modified, and then destroyed afterwards. I would go on to explain more of why you should pass objects by reference, and what it means, but I am tired :(
The print() function should also be const, since it does not modify anything.
#include "SquareMatrix.h"
#include <iostream>
using namespace std;
SquareMatrix::SquareMatrix()
{
MakeEmpty();
size = 50;
}
void SquareMatrix::MakeEmpty()
{
for (int row = 0; row < size; row++)
for (int col = 0; col < size; col++)
matrix[row][col] = 0;
}
void SquareMatrix::StoreValue(int i, int j, int value)
{
SquareMatrix();
void MakeEmpty();
void StoreValue(int i, int j, int value);
// Pre: i and j are less than or equal to size - 1.
// Post: matrix[i][j] = value.
void Add(const SquareMatrix & two, SquareMatrix & result);
// Post: result = self + two.
void Subtract(const SquareMatrix & two, SquareMatrix & result);
// Post: result = self - two.
void Print() const;
// Post: The values in self have been printed by row on
// the screen.
void Copy(const SquareMatrix & two);
// Post: self = two
private:
int size;
int matrix[50][50];
};
#endif
Have fun :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.