Ok, new to C++ and my first assignment is to covert C code to C++, I'm getting pretty frustrated and have been looking at the code for the last two days not truly understanding how to do it. The teacher gave us a program in C which I have an ok understanding of from last quarter and wants us to change it into C++ code. The entire code in C is in one main and he wants us to change it into a class CMatrix and have 4 member functions in it. There is also the issue of pointers and pointer array and passing them that has gotten me so confused. Please help!
This is the initial C Code
/* MAGIC SQUARE - An NxN matrix containing values from 1 to N*N that are */
/* ordered so that the sum of the rows, columns, and the major diagonals */
/* are all equal. There is a particular algorithm for odd integers, and */
/* this program constructs that matrix, up to 13 rows and columns. This */
/* program also adds the sums of the row, columns, and major diagonals. */
#include <stdio.h>
main()
{
int input; /* User defined integer */
int loc[14][14]; /* Array holding all */
/* input*input values. */
int row; /* Determines row of matrix */
int col; /* Determines col of matrix */
int value; /* Integer between 1 and */
/* input*input */
int otherdiag; /* Total of one matrix diagonal*/
/* */
/* Print introduction of what this program is all about. */
/* */
printf("\nMagic Squares: This program produces an NxN matrix where\n");
printf("N is some positive odd integer. The matrix contains the \n");
printf("values 1 to N*N. The sum of each row, each column, and \n");
printf("the two main diagonals are all equal. Not only does this \n");
printf("program produces the matrix, it also computes the totals for\n");
printf("each row, column, and two main diagonals.\n");
printf("\nBecause of display constraints, this program can work with\n");
printf("values up to 13 only.\n\n");
printf("Enter a positive, odd integer (-1 to exit program):\n");
while (scanf("%d",&input) == 1)
{
/* */
/* If input = -1, then exit program. */
/* */
if (input == -1)
break;
/* */
/* Validity check for input: Must be a positive odd integer < 13. */
/* */
if (input <= 0)
{
printf("Sorry, but the integer has to be positive.\n");
printf("\nEnter a positive, odd integer (-1 to exit program):\n");
continue;
}
if (input > 13)
{
printf("Sorry, but the integer has to be less than 15.\n");
printf("\nEnter a positive, odd integer (-1 to exit program):\n");
continue;
}
if (input%2 == 0)
{
printf("Sorry, but the integer has to be odd.\n");
printf("\nEnter a positive, odd integer (-1 to exit program):\n");
continue;
}
/* */
/* Initialize matrix, row, col, and otherdiag */
/* */
for (row = 0; row <= input; row++) /* Initialize matrix with */
for (col = 0; col <= input; col++) /* all zeroes. */
loc[row][col] = 0; /* Values will reside within */
/* rows 1 to input*input and */
/* columns 1 to input*input. */
/* Row totals will reside in */
/* loc[row][0], where row is */
/* the row number, while the */
/* column totals will reside */
/* in loc[0][col], where col */
/* is the column number. */
row = 1; /* First value gets to sit on */
col = input/2 + 1; /* 1st row, middle of matrix.*/
otherdiag = 0;
/* */
/* Loop for every value up to input*input, and position value in matrix*/
/* */
for (value = 1; value <= input*input; value++)
{ /* Loop for all values. */
if (loc[row][col] > 0) /* If some value already */
{ /* present, then */
row += 2; /* move down 1 row of prev. */
if (row > input) /* If exceeds side, then */
row -= input; /* go to other side. */
col--; /* move left 1 column. */
if (col < 1) /* If exceeds side, then */
col = input; /* go to other side. */
}
loc[row][col] = value; /* Assign value to location. */
/* */
/* Add to totals */
/* */
loc[0][col] += value; /* Add to its column total. */
loc[row][0] += value; /* Add to its row total. */
if (row == col) /* Add to diagonal total if */
loc[0][0] += value; /* it falls on the diagonal. */
if (row+col == input+1) /* Add to other diagonal if */
otherdiag += value; /* it falls on the line. */
/* */
/* Determine where new row and col are */
/* */
row--;
if (row < 1) /* If row exceeds side then */
row = input; /* goto other side. */
col++;
if (col > input) /* If col exceeds side then */
col = 1; /* goto other side. */
} /* End of getting all values. */
/* */
/* Print out the matrix with its totals */
/* */
printf("\nThe number you selected was %d",input);
printf(", and the matrix is:\n\n");
for (row = 1; row <=input; row++) /* Loop: print a row at a time*/
{
printf(" "); /* Create column for diag.total*/
for (col = 1; col <=input; col++)
printf("%5d",loc[row][col]); /* Print values found in a row*/
printf(" = %5d\n",loc[row][0]); /* Print total of row. */
}
/* */
/* Print out the totals for each column, starting with diagonal total. */
/* */
for (col = 0; col <=input; col++) /* Print line separating the */
printf("-----"); /* value matrix and col totals*/
printf("\n%5d",otherdiag); /* Print out the diagonal total*/
for (col = 1; col <=input; col++)
printf("%5d",loc[0][col]); /* Print out the column totals*/
printf(" %5d\n",loc[0][0]); /* Print out the other diagonal*/
/* total */
printf("\nEnter a positive, odd integer (-1 to exit program):\n");
} /* End of while input>-1 loop */
printf("\nBye bye!\n");
}
And this is what I have done so far. Alot of stuff i've comented out are stuff I've tried but doesn't seem to work. So far I just called the Init (to initialize the matrix) directly from the loop but I understand that i'm suppose to make the input into a pointer and get it from the main which i've tried doing alot and I dont kown how to get it to work
#include <iostream>
#include <limits>
#include <cctype>
using namespace std;
class CMatrix
{
public:
void GetData();
void Init(int input);
void Calculate();
void Display();
private:
int* pInput;
int input;
// int** pnLoc;
int** loc;
int value;
int otherdiag;
};
void CMatrix::GetData()
{
int input = 0;
for(;;) //infinite for loop
{
cout << "\nEnter a positive,";
cout << " odd integer (-1 to exit program):\n";
cin >> input;
// pInput = &input;
// cout << "Pointer " << *pInput << "\n" << input;
//if numbers entered, enter the block
if (cin.good()){
if (input == -1){
break;
}
else if (input > 15){
cout << "Sorry, but the integer has";
cout << " to be less than 15.\n";
}
else if (input%2 == 0){
cout << "Sorry, but the integer has to be odd.\n";
}
Init(input);
}//end if cin.good()
else if (!isdigit(input))
cout <<"Only digits are allowed!\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}//end for loop
}
void CMatrix::Init(int input)
{
int row, col;
// cout << "\n" << input;
int **loc = 0;
for (row = 0; row <= input; row++)
loc[row] = new int[input];
for (row = 0; row <= input; row++)
for (col = 0; col <= input; col++)
*(loc[row] + col) = col;
row = 1;
col = input/2 + 1;
otherdiag = 0;
}
void CMatrix::Calculate()
{
*pInput = input;
int row, col;
/* */
/* Loop for every value up to input*input, and position value in matrix*/
/* */
for (value = 1; value <= input*input; value++)
{ /* Loop for all values. */
if (loc[row][col] > 0) /* If some value already */
{ /* present, then */
row += 2; /* move down 1 row of prev. */
if (row > input) /* If exceeds side, then */
row -= input; /* go to other side. */
col--; /* move left 1 column. */
if (col < 1) /* If exceeds side, then */
col = input; /* go to other side. */
}
loc[row][col] = value; /* Assign value to location. */
/* */
/* Add to totals */
/* */
loc[0][col] += value; /* Add to its column total. */
loc[row][0] += value; /* Add to its row total. */
if (row == col) /* Add to diagonal total if */
loc[0][0] += value; /* it falls on the diagonal. */
if (row+col == input+1) /* Add to other diagonal if */
otherdiag += value; /* it falls on the line. */
/* */
/* Determine where new row and col are */
/* */
row--;
if (row < 1) /* If row exceeds side then */
row = input; /* goto other side. */
col++;
if (col > input) /* If col exceeds side then */
col = 1; /* goto other side. */
} /* End of getting all values. */
// cout << *pInput << "\n" << input;
delete [] loc;
}
void CMatrix::Display()
{
*pInput = input;
cout << "\nThe number you selected was " << input << ", and the matrix is:\n\n";
for (int row = 1; row <=input; row++) /* Loop: print a row at a time*/
{
cout << " "; /* Create column for diag.total*/
for (int col = 1; col <=input; col++)
cout << loc[row][col]; /* Print values found in a row*/
cout << " = " << loc[row][0] << "\n"; /* Print total of row. */
}
/* */
/* Print out the totals for each column, starting with diagonal total. */
/* */
for (int col = 0; col <=input; col++) /* Print line separating the */
cout << "-----"; /* value matrix and col totals*/
cout << otherdiag; /* Print out the diagonal total*/
for (int col = 1; col <=input; col++)
cout << loc[0][col]; /* Print out the column totals*/
cout << loc[0][0] << "\n"; /* Print out the other diagonal*/
/* total */
cout << "\nEnter a positive, odd integer (-1 to exit program):\n";
}
int main()
{
// CMatrix *pInput = new CMatrix;
CMatrix Homework;
Homework.GetData();
// Homework.Init();
Homework.Calculate();
Homework.Display();
}