Emergency Please Help

I have been asked by my computer teacher to prepare the source code for a magic square in C++. I tried writing the program; din't get any errors compiling too... but while execution, the compiler just hangs. I think it's because my program is too inefficient and requires an impossible amount of memory. Please read and help!
-------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>

void generate(int[][10], int);
int check(int[],int[],int,int,int);

void main()
{ clrscr();
cout<<"WELCOME TO THE MAGIC SQUARE GENERATOR!"<<endl;
int m[10][10], n;
cout<<"Enter order of magic square to be generated <=10:";
cin>>n;

generate(m,n);
getch();
}

void generate(int m[][10], int n)
{ int mval, srow[10], scol[10], sld,srd;
int a=0,b=0;

do
{ randomize();
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
m[i][j]=random(n*n);

for(;a<n; a++)
for(j=0; j<n; j++)
srow[a]+=m[a][j];

for(;b<n; b++)
for(j=0; j<n; j++)
scol[b]+=m[j][b];

for(i=0;i<n; i++)
for(j=0;j<n;j++)
{ if(i==j)
srd+=m[i][j];
else if(i+j==(n-1))
sld+=m[i][j];
}

mval=check(srow,scol,srd,sld,n);
}while(mval==0);

cout<<endl<<"Magic square is:"<<endl;
for(int i=0; i<n; i++)
{ for(int j=0; j<n; j++)
cout<<setw(4)<<m[i][j];
cout<<endl;
}
}


int check(int rs[], int cs[], int rd, int ld, int n)
{ int fr=0;

for(int i=0;i<n && fr==0; i++)
{ if((rs[0]!=rs[i]) || (rs[0]!=cs[i]))
fr++;
}

if (!fr)
{ if((rs[0]==rd) && (rs[0]==ld))
return 1;
else
return 0;
}

else
return 0;
}

-------------------------------------------------

Please note that I know C++ just a little more than basic. This is my 2nd year with it in school. I've done upto structures and classes.

any help is appreciated. thanks!
Do you mean it hangs during the running of the program or that the compiler stops compiling?

Please use code tags as it makes it a bit easier to read, but from a quick look I'm wondering on the following,

If it compiles I would guess to check that the check() function does actually get to the line that returns 1; (set a breakpoint there)
If you always return 0 the program will be stuck in an infinate loop at
1
2
  mval=check(srow,scol,srd,sld,n);
}while(mval==0);
Last edited on
Thanks for your interest Moooce.

the program does compile, but when I run it, it asks for the order of the magic square. then blurp! nothing happens! my c++ just gets stuck there. I have to manually end it.

the check() function accepts the srow[], scol[], srd and sld parameters which are arrays for the sum of rows, sum of columns, sum of right and left diagonals resp. the function is to check if they are all equal. If they are, it returns 1.

mval=check(srow,scol,srd,sld,n);
}while(mval==0);


so, if value returned is 0, the loop is to repeated. the random no. generator should run again and again until all sums are equal and check() function returns 1.

yep, I can see what's meant to happen, just suggesting that you can tell if the logic and values stored in your arrays are as you are expecting.

If you place a break point on the return 1 line you'll know if it gets there.
You could change the return 0s to return 1s to see if the program exits (all be it with the wrong result)
Or better still, use the debugger to step through the code and see if what is happening is what you intended.

If you try the last sugestion, I think you may find some variables are not holding numbers that you may be expecting.

I would confirm, but It fails to compile for me (missing variables and I don't know what exactly happens with randomize() and random() ) but I could guess.
Last edited on
Topic archived. No new replies allowed.