code runs but crashes after second value

Mar 3, 2017 at 6:57am
//Kenny Maldonado
//Lab 2

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

int numOfChoice, row, column;

int main()
{
cout << "Please enter an Odd number to create your magic square." << endl << endl;
cin >> numOfChoice;

while (numOfChoice % 2 == 0 || cin.fail())
{
cin.clear();
cin.ignore(60, '\n');
cout << endl << endl << "Error. You did not enter an odd Number." << endl << endl;
cin >> numOfChoice;
}

int ** magicSquare = new int *[numOfChoice];

for (int row = 0; row < numOfChoice; ++row)
{
magicSquare[row] = new int[numOfChoice];
}

for (int column = 0; column < numOfChoice; ++column)
{
for (int row = 0; row < numOfChoice; ++row);
{
magicSquare[row][column] = 0;
}
}
row = 0;
column = ((numOfChoice / 2));

for (int value = 1; value <= (numOfChoice)*(numOfChoice); value++)
{

magicSquare[row][column] = value;



column++;
row--;

if (row < 0 && column >= numOfChoice)
{
row += 2;
column--;
}

if (row < 0)
{
row = (numOfChoice - 1);
}

if (column >= numOfChoice)
{
column = 0;
}

if (magicSquare[row][column] != 0)
{
row += 2;
column--;
}

}

cout << endl << "After Entering an Odd Number, your Magic Square is:" << endl << endl;
for (int row = 0; row < numOfChoice; ++row)
{
for (int column = 0; column < numOfChoice; ++column)
{
cout << magicSquare[row][column] << ' ';
}
cout << endl << endl;
}
system("pause");
return 0;
}
Mar 3, 2017 at 8:05am
Please use code tags.

Presumably you are writing beyond array bounds. The most likely culprit is where you test if the square you want is occupied already. You have already done your "up and to the right" movement, as well as any "wrapping round". At this point you should be moving down relative to your original position. However, you won't get this because you have already done some wrapping.

Store your current values of row and column. Carry out any "up and right" and any wrapping. Test this cell, then refer to your original row, column if you need to move down to avoid an occupied cell.


Here's your code with as few corrections as I could get away with. You were also redefining row, column etc.


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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;


int main()
{
   int numOfChoice, row, column;                    // move into main; unnecessary global variables
   cout << "Please enter an Odd number to create your magic square." << endl << endl;
   cin >> numOfChoice;

   while (numOfChoice % 2 == 0 || cin.fail())
   {
      cin.clear();
      cin.ignore(60, '\n');
      cout << endl << endl << "Error. You did not enter an odd Number." << endl << endl;
      cin >> numOfChoice;
   }

   int ** magicSquare = new int *[numOfChoice];
   for ( row = 0; row < numOfChoice; ++row ) magicSquare[row] = new int[numOfChoice];

   for ( column = 0; column < numOfChoice; ++column)
   {
      for ( row = 0; row < numOfChoice; ++row) magicSquare[row][column] = 0;
   }
   row = 0;
   column = ((numOfChoice / 2));

   for ( int value = 1; value <= numOfChoice * numOfChoice; value++ )
   {
      magicSquare[row][column] = value;

      int oldrow = row;                         // store in case you need to recover them
      int oldcol = column;

      column++;                                 // move "up and right"
      row--;                                    //

      if ( row < 0) row = numOfChoice - 1;      // wrap if necessary
      if ( column >= numOfChoice ) column = 0;  //

      if (magicSquare[row][column] != 0)
      {
         row = oldrow + 1;
         column = oldcol;
      }
   }

   cout << endl << "After Entering an Odd Number, your Magic Square is:" << endl << endl;
   for (int row = 0; row < numOfChoice; ++row)
   {
      for (int column = 0; column < numOfChoice; ++column) cout << setw(4) << magicSquare[row][column] << ' ';
      cout << endl << endl;
   }
   //system("pause");
   return 0;
}


Please enter an Odd number to create your magic square.
5

After Entering an Odd Number, your Magic Square is:

  17   24    1    8   15 

  23    5    7   14   16 

   4    6   13   20   22 

  10   12   19   21    3 

  11   18   25    2    9 

Last edited on Mar 3, 2017 at 8:37am
Mar 3, 2017 at 10:23pm
THANK YOU!!
Topic archived. No new replies allowed.