//This is the file SqrMatrix.cpp

 

#include "adtsquare4.h"

SqrMatrix::SqrMatrix()

{

     //This is the default constructor.

     //Pre:

     //Post:  all entries in SqrMatrix are 0

     size = 50;

     MakeEmpty(size);

}

SqrMatrix::SqrMatrix(int s)

{

     // A constructor

     //Pre:

     //Post:  SqrMatrix is of size s and all entries

     //       in SqrMatrix are 0

     size = s;

     MakeEmpty(size);

}

 

void SqrMatrix::MakeEmpty(int x)

{

     //sets all entries of the SqrMatrix to 0

     //Pre:

     //Post: All entries of the SqrMatrix = 0

     int i,j;

if (x > size)

     {

          cerr<<"Attempt to set entries that do not exist";

          return;

          //if you wish the program to stop as a result of

          //error, use the exit command instead of return

     }

     for (i = 0; i < x; i++)

          for (j = 0; j < x; j++)

              array[i][j] = 0;

}

 

void SqrMatrix::StoreValue(int i, int j, valuetype value)

{

     //Pre: SqrMatrix is initialized

     //     i and j are < size of the matrix

     //Post:  The entry in the ith row and jth column =    

     //       value

    

     //we can add error checking here to make sure

     //that i and j are not out of bounds

 

     array[i][j] = value;

}

 

void SqrMatrix::PrintMatrix()

{

     //Pre: SqrMatrix is initialized

     //Post: SqrMatrix is printed out

    

     for (int i = 0; i < size; i++)

     {

          for (int j = 0; j < size; j++)

              cout<<array[i][j]<<' ';

        cout<<endl;

     }

}

void SqrMatrix::AddMatrix(SqrMatrix sqr)

{

     //Pre: SqrMatrix and sqr are intialized

     //Post:  The final value of SqrMatrix is the

     //       sum of SqrMatrix and sqr

    

     for (int i = 0; i < size; i++)

          for (int j = 0; j < size; j++)

               array[i][j] = array[i][j] + sqr.array[i][j];

}

 

//This file is SqrMatrix.h

 

#include<iostream>

#include<fstream>

using namespace std;

typedef int valuetype;

 

class SqrMatrix

{

public:

    

     SqrMatrix();

     //This is the default constructor.

     //Pre:

     //Post:  all entries in SqrMatrix are 0

     SqrMatrix(int s);

    // A constructor

     //Pre:

     //Post:  SqrMatrix is of size s and all entries

     //       in SqrMatrix are 0

     void MakeEmpty(int x);

     //Sets all entries of the SqrMatrix to 0

     //Pre:

     //Post: entries of x rows and colums of the SqrMatrix

     //      = 0  

     void StoreValue(int i, int j, int value);

     //Function: stores value into the [i][j] position

     //          of the square matrix

     //Pre: i and j are <= size of the matrix

     //Post:  The entry in the ith row and jth column = value

     void AddMatrix(SqrMatrix sqr);

     //Function: adds sqr to the matrix

     //Pre: SqrMatrix and sqr have been initialized

     void PrintMatrix();

     //Function:  Prints the matrix in rows and columns

     //Pre: Matrix is initialized

     //Post: SqrMatrix is printed out

     bool Compare(SqrMatrix sqr);

     //Function: Compares self with sqr

     //Pre: self and sqr are intialized

     //Post: return value is true if all entries are the same

private:

     int array[50][50];

     int size; //  holds the size of the SqrMatrix

 

};

 

//This is the data file

 

1 3 5

4 0 2

6 7 8

 

1 3 5

4 2 0

6 7 8

 

1 3 0

4 2 5

6 7 8

 

1 0 3

4 2 5

6 7 8

 

1 2 3

4 0 5

6 7 8

 

1 2 3

0 4 5

6 7 8

 

1 2 3

6 4 5

0 7 8