Magic Square Help

Hi, I created a magic square program to allow for a variable input of an odd value. However, I am getting the error
ISO C++ forbids variable-size array `magic' for line 9. If someone could please give me a nudge in the right direction to resolve it, that would be great.

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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  int n;
  cout << "Please enter an odd number for you matrix dimensions: " << endl;
  cin >> n;
  int magic[n][n];

  for(int r = 0; r < n; r++)
  {
    for(int c = 0; c < n; c++)
    {
      magic[r][c] = 0;
    }
  }
  int i = n-1;
  int j = n/2;

  for(int k = 1; k <= n*n;)
  {
    i -= 1;
    j -= 1;
    if (i==1 && j ==0)
    {
      j++;
      i += 2;
    }
    else if(i==0)
    {
      i = n;
    }
    else if(j==0)
    {
      j = n;
    }
    else if(magic[i-1][j-1] !=0)
    {
      j++;
      i +=2;
    }
    magic[i-1][j-1] = k;
  }
  for(i = (n-1); i>-1; i--)
  {
    for(i = (n-1); i>-1; i--)
    {
      cout << magic[i][j];
    }
  cout << endl;
  }
  return 0;
}
Last edited on
closed account (48bpfSEw)
Dynamic allocations are dangerous I heared e.g. in applications for airplanes. The risk is too high for an unexpected exception of low memory. Therefor the developer of such apps avoid using dynamic memory allocations and use instead static reserved memory allocation.

In your case what do you think about the idea to reserve a maximum array and delimit the users input to this maximum borders?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const int MAX = 100;
int magic[MAX][MAX];

int n=0;

while (true) {
    cout << "Please enter an odd number for you matrix dimensions (0: exit): " << endl;
    cin >> n;

    if (n == 0) {
      cout << "good bye!" << endl;
      exit(-1);
      }

    else if (n > MAX -1)
       cout << "too high!" << endl;
    else if (n < 0)
       cout << "too low!" << endl;
    else {
       cout << "thank you!" << endl;
       break;
       }
   }

Anyone who uses C++ in a life-critical system or who designs a life-critical system in such a way that human input is passed directly to it without first being sanitized by a less-critical component should probably be fired, anyway.

In your case what do you think about the idea to reserve a maximum array and delimit the users input to this maximum borders?
It's a waste of resources and effort. A process that deliberately allocates more memory than it needs in a multitasking system is misbehaving.
closed account (48T7M4Gy)
The risk is too high for an unexpected exception of low memory.

Aside from experienced and capable software system designers who could foresee and handle this, I think Bertrand Meyer and design by contract followers would have a lot to say to counter that bald statement.

An unexpected exception error almost seems to be a contradiction in terms unless it means someone ripped the case open and stole some RAM. Even then if there is a reasonable expectation that such an exceptional event could occur the owners could perhaps tie a vicious guard dog to the box. :)
closed account (48bpfSEw)
I heard additionally that such apps of life-critical system are running twice (or more times) and a judge component compares the results of the calculations.

However what if the half of the results are "false" (caused by damage memory) and the other half are "true" or the judge component is knocked out?

Then there is only one option left: plug in and pray! ^^

Security is an illussion like bug-free applications or perfect design.
Topic archived. No new replies allowed.