I have created a program using the apmatrix class to declare 2 dimensional arrays. How could I declare 2d arrays using the vector header file or any other standard header. here is my code...if its needed, probably dont need the whole thing but if your curious.
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <apmatrix.h>
#include <apvector.h>
using namespace std;
const int MAX = 4,
FILL = 0;
void getSquare (apmatrix <int> &);
int sumRow (const apmatrix<int> &, int &);
bool rowAdd (const apmatrix<int> &, int);
bool columnAdd (const apmatrix<int> &, int);
bool diagAdd (const apmatrix<int> &, int);
bool unique (const apmatrix<int> &);
void print (const apmatrix<int> &);
main()
{
int row_sum;
apmatrix <int> square(MAX, MAX);
getSquare (square);
sumRow (square, row_sum);
if (
rowAdd (square, row_sum) &&
columnAdd (square, row_sum) &&
diagAdd (square, row_sum) &&
unique (square)
)
{
print (square);
cout<< endl
<< "All rows, columns, and diagonals add up to: "
<< row_sum << endl;
}
else
{
print (square);
cout<< endl
<< "Input is not a magic square...";
}
return 0;
}
void getSquare
(apmatrix <int> &array)
//assign locations for the magic square, from a file
{
ifstream infile ("Lesson18.txt");
for (int row = FILL; row < MAX; row++)
for (int col = FILL; col < MAX; col++)
infile >> array[row][col];
}
int sumRow
(const apmatrix<int> &square, int &row)
//calculate the sum of the first row
{
row = FILL;
for (int col = FILL; col < MAX; col++)
row += square[FILL][col];
return row;
}
bool rowAdd
(const apmatrix<int> &square, int row_sum)
//do all rows add up to row_sum?
{
int add = FILL;
bool stop = FILL;
for (int row = FILL; row < MAX; row++)
{
add = FILL;
for (int col = FILL; col < MAX; col++)
add += square[row][col];
if (add != row_sum)
stop = true;
}
if (stop) return false;
else return true;
}
bool columnAdd
(const apmatrix<int> &square, int row_sum)
//do all columns add up to row_sum?
{
int add = FILL;
bool stop = FILL;
for (int col = FILL; col < MAX; col++)
{
add = FILL;
for (int row = FILL; row < MAX; row++)
add += square[row][col];
if (add != row_sum)
stop = true;
}
if (stop) return false;
else return true;
}
bool diagAdd
(const apmatrix<int> &square, int row_sum)
//do all diagonals add up to row_sum?
{
int add = FILL;
bool stop = FILL;
for (int col = FILL; col < MAX; col++) //do first diagonal
add += square[col][col];
if (add != row_sum)
stop = true;
add = FILL; //reset add for next calculation
int row = MAX; //needs to start at max to find 2nd diagonal
for (int col = FILL; col < MAX; col++) //do second diagonal
{
row--;
add += square[row][col];
}
if (add != row_sum)
stop = true;
if (stop) return false;
else return true;
}
bool unique
(const apmatrix<int> &square)
//is each number different, within the correct range?
{
const int VECTOR_MAX = MAX * MAX + 1;
apvector<int> countNum (VECTOR_MAX, FILL);
int singlenum;
bool stop = FILL;
for (int row = FILL; row < MAX; row++)
for (int col = FILL; col < MAX; col++)
{
singlenum = square[row][col];
countNum[singlenum]++;
}
for (int start = FILL + 1; start < VECTOR_MAX; start++)
if ((countNum[start] == FILL) ||
(countNum[start] > 1))
stop = true;
if (stop) return false;
else return true;
}
void print
(const apmatrix<int> &array)
//print 2d array to the screen
{
for (int row = FILL; row < MAX; row++)
{
for (int col = FILL; col < MAX; col++)
cout<< setw(4) << array[row][col];
cout<< endl;
}
}
/***************************
OUTPUT:
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
All rows, columns, and diagonals add up to: 34
****************************
16 3 2 13
5 11 10 8
9 7 7 12
4 15 15 1
Input is not a magic square...
***************************/