Odd magic Square problems-struggling with it

i've been struggling with it for the past week, i've done all of the coding, but it keeps giving me errors, any insights would be really appreciated.

( Sorry admins, i didn't know which section this topic should be posted to, so i posted it twice )

Here's my code :

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdlib>
#include<time.h>
using namespace std;
int size(bool flag)
{
int test;
if(flag)
{
do
{
test=(int)(double(rand())/double(RAND_MAX)*14.0+3.0);
}while(test%2==0);
return test;
}
else

return 7;
}
void fillandprint()
{
int n;
bool flg=true;
n=size(flg);
int a[19][19];
int raw,column,l;
l=n*n;

int index;
raw=0;
column=(n/2);
a[raw][column]=1;
index=1;


int i=0;
while(i<l-1)
{
if(raw==0)
{

a[raw][column]=(index+1);
raw=n-1;
column++;
index++;
i++;
}

else if(column==(n-1))
{
a[raw][column]=(index+1);
raw++;
column=0;
index++;
i++;
}
else if(a[raw][column]!=0)
{
raw++;
index=index-1;

}



else
{
a[raw][column]=(index+1);
raw--;
column++;
i++;
}



}
for(int r=0;r<n;r++)
{
for(int k=0;k<n;k++)
{


cout<<a[r][k]<<" ";
if(k==n-1)
{
cout<<endl;
}
}

}
return;
}




int main()
{
fillandprint();
return(0);
}
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdlib>
#include<time.h>
using namespace std;
int size(bool flag)
{
   int test;
   if(flag)
   {
      do
         {
            test=(int)(double(rand())/double(RAND_MAX)*14.0+3.0);
         }while(test%2==0);
      return test;
   }
   else
      return 7;
}

void fillandprint()
{ 
   int n;
   bool flg=true;
   n=size(flg);
   int a[19][19];
   int raw,column,l;
   l=n*n;

   int index;
   raw=0;
   column=(n/2);
   a[raw][column]=1;
   index=1;


   int i=0;
   while(i<l-1)
      {
         if(raw==0)
            {
               a[raw][column]=(index+1);
               raw=n-1;
               column++;
               index++;
               i++;
            }
         else if(column==(n-1))
            {
               a[raw][column]=(index+1);
               raw++;
               column=0;
               index++; 
               i++;
            }
         else if(a[raw][column]!=0)
            {
               raw++;
               index=index-1;
            }
         else
            {
                a[raw][column]=(index+1);
                raw--;
                column++;
                i++;
            }
      }

   for(int r=0;r<n;r++)
      {
         for(int k=0;k<n;k++)
            {
               cout<<a[r][k]<<" ";
               if(k==n-1)
                  {
                     cout<<endl;
                  }
            }
      }
   return;
}

int main()
{
   fillandprint();
   return(0);
}


Sorry but but something that big is a headache to read without code tags
Can you post the errors you are getting?
Hey dude, i don't know what do you mean exactly by "tags" .. anyway the program is just crashing, maybe i am getting out of range or something. but if you pay close attention to the code, i don't think there are any logical errors, right?
Firstly, your program isn't crashing its just not staying open for you to see the output - put cin.get(); after line 87 and it will pause the program to see the result. When you do this you'll see that theres a problem lol.

You're size() function is confusing. In fillandprint() you create the bool variable 'flg' and set it to true and then when you call you're size() function with flg as an argument, you check to see if it is true.

Also, the rand() function is of type int. You have cast rand() and (rand_max) to double, and then cast everything back to int. Change t to: test=rand()/(RAND_MAX)*14+3;

Just to clarfy, is the program supposed to add two columns of random numbers and then print them?
Thanks mate, that might help.

No i am doing the odd magic square, i don't know if you're familiar with the siamese method. So i am just declaring a fixed 19X19 array so i wouldn't have to go through looping for an undetermined ( random ) number.

I'll check it out.
The problem is not caused by the random number generator, and not the printing section.. it must be something with the array ( like exceeding the range or something ) .. i'm still trying .
Could you explain what the game is? you might find that theres a better method of doing it but i'm not familiar with what you're doing.
http://www.codeproject.com/KB/recipes/Magic_Square.aspx

go there, check it out, it explains it. This is the easiest method you could use for ODD magic squares.
Topic archived. No new replies allowed.