-
Array Help?
This is my 2nd post (different problem this time) and with the last question I made, you guys answered me very well, so here I am again.
This time, I have to make a 6x6 "Magic Square"using arrays. I first made the program for a 3x3 and it works perfectly!
BUT, when I just doubled the stuff and tried to make it 6x6 it just doesn't work.. it goes straight to the "Press Any Key to Continue" and doesn't do anything.
The code may look tricky, but it's really not, there's a lot of copy/pasting in there for just copying the same function over and over:
O, BTW, if you don't wanna help that's ok. I'm just confused why it doesn't work, so if you have some free time to debug then awesome!
Code:
#include<iostream.h>
#include<time.h>
#include<stdlib.h>
void assignrow1();
void checkrow1();
void assignrow2();
void checkrow2();
void assignrow3();
void checkrow3();
void assignrow4();
void checkrow4();
void assignrow5();
void checkrow5();
void assignrow6();
void checkrow6();
void checkcol1();
void checkcol2();
void checkcol3();
void checkcol4();
void checkcol5();
void checkcol6();
int col1num1, col1num2, col1num3, col1num4, col1num5, col1num6;
int col2num1, col2num2, col2num3, col2num4, col2num5, col2num6;
int col3num1, col3num2, col3num3, col3num4, col3num5, col3num6;
int col4num1, col4num2, col4num3, col4num4, col4num5, col4num6;
int col5num1, col5num2, col5num3, col5num4, col5num5, col5num6;
int col6num1, col6num2, col6num3, col6num4, col6num5, col6num6;
int row1[6];
int row2[6];
int row3[6];
int row4[6];
int row5[6];
int row6[6];
int row1num1, row1num2, row1num3, row1num4, row1num5, row1num6;
int row2num1, row2num2, row2num3, row2num4, row2num5, row2num6;
int row3num1, row3num2, row3num3, row3num4, row3num5, row3num6;
int row4num1, row4num2, row4num3, row4num4, row4num5, row4num6;
int row5num1, row5num2, row5num3, row5num4, row5num5, row5num6;
int row6num1, row6num2, row6num3, row6num4, row6num5, row6num6;
int counter=0;
int main()
{
srand(time(0));
assignrow1();
cout<<row1num1<<row1num2<<row1num3<<row1num4<<row1num5<<row1num6<<endl;
cout<<row2num1<<row2num2<<row2num3<<row2num4<<row2num5<<row2num6<<endl;
cout<<row3num1<<row3num2<<row3num3<<row3num4<<row3num5<<row3num6<<endl;
cout<<row4num1<<row4num2<<row4num3<<row4num4<<row4num5<<row4num6<<endl;
cout<<row5num1<<row5num2<<row5num3<<row5num4<<row5num5<<row5num6<<endl;
cout<<row6num1<<row6num2<<row6num3<<row6num4<<row6num5<<row6num6<<endl;
cout<<"It took "<<counter<<" tries."<<endl;
return 0;
}
void assignrow1()
{
counter=counter+1;
for (int x=0;x<6;x=x++)
{
row1[x]=rand()%36+1;
}
checkrow1();
}
void checkrow1()
{
row1num1=row1[0];
row1num2=row1[1];
row1num3=row1[2];
row1num4=row1[3];
row1num5=row1[4];
row1num6=row1[5];
if(row1num1+row1num2+row1num3+row1num4+row1num5+row1num6==36)
{
assignrow2();
}
else
{
assignrow1();
}
}
void assignrow2()
{
for (int y=0;y<6;y=y++)
{
row2[y]=rand()%36+1;
}
checkrow2();
}
void checkrow2()
{
row2num1=row2[0];
row2num2=row2[1];
row2num3=row2[2];
row2num4=row2[3];
row2num5=row2[4];
row2num6=row2[5];
if(row2num1+row2num2+row2num3+row2num4+row2num5+row2num6==36)
{
assignrow3();
}
else
{
assignrow2();
}
}
void assignrow3()
{
for (int z=0;z<6;z=z++)
{
row3[z]=rand()%36+1;
}
checkrow3();
}
void checkrow3()
{
row3num1=row3[0];
row3num2=row3[1];
row3num3=row3[2];
row3num4=row3[3];
row3num5=row3[4];
row3num6=row3[5];
if(row3num1+row3num2+row3num3+row3num4+row3num5+row3num6==36)
{
assignrow4();
}
else
{
assignrow3();
}
}
void assignrow4()
{
for (int a=0;a<6;a=a++)
{
row4[a]=rand()%36+1;
}
checkrow4();
}
void checkrow4()
{
row4num1=row4[0];
row4num2=row4[1];
row4num3=row4[2];
row4num4=row4[3];
row4num5=row4[4];
row4num6=row4[5];
if(row4num1+row4num2+row4num3+row4num4+row4num5+row4num6==36)
{
assignrow5();
}
else
{
assignrow4();
}
}
void assignrow5()
{
for (int b=0;b<6;b=b++)
{
row5[b]=rand()%36+1;
}
checkrow5();
}
void checkrow5()
{
row5num1=row5[0];
row5num2=row5[1];
row5num3=row5[2];
row5num4=row5[3];
row5num5=row5[4];
row5num6=row5[5];
if(row5num1+row5num2+row5num3+row5num4+row5num5+row5num6==36)
{
assignrow6();
}
else
{
assignrow5();
}
}
void assignrow6()
{
for (int c=0;c<6;c=c++)
{
row6[c]=rand()%36+1;
}
checkrow6();
}
void checkrow6()
{
row6num1=row6[0];
row6num2=row6[1];
row6num3=row6[2];
row6num4=row6[3];
row6num5=row6[4];
row6num6=row6[5];
if(row6num1+row6num2+row6num3+row6num4+row6num5+row6num6==36)
{
checkcol1();
}
else
{
assignrow6();
}
}
void checkcol1()
{
col1num1=row1num1;
col1num2=row2num1;
col1num3=row3num1;
col1num4=row4num1;
col1num5=row5num1;
col1num6=row6num1;
if(col1num1+col1num2+col1num3+col1num4+col1num5+col1num6==36)
{
checkcol2();
}
else
{
assignrow1();
}
}
void checkcol2()
{
col2num1=row1num2;
col2num2=row2num2;
col2num3=row3num2;
col2num4=row4num2;
col2num5=row5num2;
col2num6=row6num2;
if(col2num1+col2num2+col2num3+col2num4+col2num5+col2num6==36)
{
checkcol3();
}
else
{
assignrow1();
}
}
void checkcol3()
{
col3num1=row1num3;
col3num2=row2num3;
col3num3=row3num3;
col3num4=row4num3;
col3num5=row5num3;
col3num6=row6num3;
if(col3num1+col3num2+col3num3+col3num4+col3num5+col3num6==36)
{
checkcol4();
}
else
{
assignrow1();
}
}
void checkcol4()
{
col4num1=row1num4;
col4num2=row2num4;
col4num3=row3num4;
col4num4=row4num4;
col4num5=row5num4;
col4num6=row6num4;
if(col4num1+col4num2+col4num3+col4num4+col4num5+col4num6==36)
{
checkcol5();
}
else
{
assignrow1();
}
}
void checkcol5()
{
col5num1=row1num5;
col5num2=row2num5;
col5num3=row3num5;
col5num4=row4num5;
col5num5=row5num5;
col5num6=row6num5;
if(col5num1+col5num2+col5num3+col5num4+col5num5+col5num6==36)
{
checkcol6();
}
else
{
assignrow1();
}
}
void checkcol6()
{
col6num1=row1num6;
col6num2=row2num6;
col6num3=row3num6;
col6num4=row4num6;
col6num5=row5num6;
col6num6=row6num6;
if(col6num1+col6num2+col6num3+col6num4+col6num5+col6num6==36)
{
}
else
{
assignrow1();
}
}
-
>> for (int x=0;x<=6;x=x+1)
Array indexes go from 0 to size - 1. Your loop goes from 0 to 6, but you are working with an array of size 6. This could cause your crash.
Also note that most C or C++ programmers use ++x instead of x=x+1. Either will work, but if you've learned the ++x notation you might as well use that instead.
Finally, <iostream.h> is not standard, so if you'd like people to help debug your code it might not compile on their compilers. Use <iostream> if you can instead, it is newer and standard and so it will work on all standards conformant compilers.
-
Thanks, Daved.
I changed all of the loops to
Code:
for (int x=0;x<6;x=x++)
And I changed the
to
But I don't see any change. :(
Were you saying that if I made an array of 5, like "int test[5];", the first character in the array would be at "test[0]" ?
I know C++ does something like that with the random # generator...
<iostream> doesn't work for me without the .h, but it is probably because I'm using Microsoft Visual C++ version 6.0 from '98. I'm too cheap to upgrade.
I changed it anyway so that people with the different version can use the code easier.
-
If you were wondering, here is the code for the 3x3 "Magic Square" and it works perfectly I think:
Code:
#include<iostream>
#include<time.h>
#include<stdlib.h>
void assignrow1();
void checkrow1();
void assignrow2();
void checkrow2();
void assignrow3();
void checkrow3();
void checkcol1();
void checkcol2();
void checkcol3();
int col1num1;
int col1num2;
int col1num3;
int col2num1;
int col2num2;
int col2num3;
int col3num1;
int col3num2;
int col3num3;
int row1[3];
int row2[3];
int row3[3];
int row1num1;
int row1num2;
int row1num3;
int row2num1;
int row2num2;
int row2num3;
int row3num1;
int row3num2;
int row3num3;
int counter=0;
int main()
{
// srand(time(0));
assignrow1();
cout<<row1num1<<row1num2<<row1num3<<endl;
cout<<row2num1<<row2num2<<row2num3<<endl;
cout<<row3num1<<row3num2<<row3num3<<endl;
cout<<"It took "<<counter<<" tries."<<endl;
return 0;
}
void assignrow1()
{
counter=counter+1;
for (int x=0;x<=3;x=x+1)
{
row1[x]=rand()%9+1;
}
checkrow1();
}
void checkrow1()
{
row1num1=row1[1];
row1num2=row1[2];
row1num3=row1[3];
if(row1num1+row1num2+row1num3==9)
{
//cout<<row1num1<<row1num2<<row1num3<<endl;
assignrow2();
}
else
{
assignrow1();
}
}
void assignrow2()
{
for (int y=0;y<=3;y=y+1)
{
row2[y]=rand()%9+1;
}
checkrow2();
}
void checkrow2()
{
row2num1=row2[1];
row2num2=row2[2];
row2num3=row2[3];
if(row2num1+row2num2+row2num3==9)
{
//cout<<row2num1<<row2num2<<row2num3<<endl;
assignrow3();
}
else
{
assignrow2();
}
}
void assignrow3()
{
for (int z=0;z<=3;z=z+1)
{
row3[z]=rand()%9+1;
}
checkrow3();
}
void checkrow3()
{
row3num1=row3[1];
row3num2=row3[2];
row3num3=row3[3];
if(row3num1+row3num2+row3num3==9)
{
//cout<<row3num1<<row3num2<<row3num3<<endl;
//cout<<endl;
checkcol1();
}
else
{
assignrow3();
}
}
void checkcol1()
{
col1num1=row1num1;
col1num2=row2num1;
col1num3=row3num1;
if(col1num1+col1num2+col1num3==9)
{
checkcol2();
}
else
{
assignrow1();
}
}
void checkcol2()
{
col2num1=row1num2;
col2num2=row2num2;
col2num3=row3num2;
if(col2num1+col2num2+col2num3==9)
{
checkcol3();
}
else
{
assignrow1();
}
}
void checkcol3()
{
col3num1=row1num3;
col3num2=row2num3;
col3num3=row3num3;
if(col3num1+col3num2+col3num3==9)
{
}
else
{
assignrow1();
}
}
-
>> Were you saying that if I made an array of 5, like "int test[5];", the first character in the array would be at "test[0]" ?
Yes. Your changes are correct. I didn't run your code so I don't know if that was your problem or not, but either way your original code was wrong and your updates are right (assuming you did them correctly).
>> <iostream> doesn't work for me without the .h, but it is probably because I'm using Microsoft Visual C++ version 6.0 from '98.
It works in VC++ 6, you just need to specify the std namespace. The easiest way to do this for a small program is to add using namespace std; to the top of the file under the #includes. The best way to do this is to use std::cout and std::cin and std::endl instead of without the std::. Either one should be fine for this program.
-
-
BTW, instead of x=x++, it should just be ++x (or x++ is ok too).
-
Do you know why there was no problem with the 3x3 even with the little problem(s) you showed me?
-
You just got lucky with your 3x3 matrix. The errors I pointed cause undefined behavior, meaning you could get a crash, or it could appear to work now but give bad results later.
I see the problem with the 6x6 version. You are assigning random numbers from 1 to 36 into 6 variables, right? Then in checkrow1 you are checking to see if the 6 random variables add up to 36. If they don't, then you are calling assignrow1 again. The chances of the 6 variables adding up to 36 is very small since there are six of them in that range [1,36]. What is happening is that code keeps going back an forth between assignrow1 and checkrow1 without ever getting numbers that add up to 36.
Eventually you get a stack overflow. You are not allowed to keep calling functions deeper and deeper or you run out of stack space.
The solution is to use a loop inside assignrow1 that calls checkrow1 and tries again if checkrow1 returns false (assuming you know how to return a value from a function). This will avoid calling nested functions back and forth.
Of course, that will just make the code run, I think you might want to rethink your design that attempts to get 6 numbers that add up to 36.
-
Some suggestions:
1. Make row a 6x6 array (row[6][6]).
2. Get rid of every single variable "rowxnumy" and replace with row[x-1][y-1].
3. Get rid of assignrow1, assignrow2, etc. and replace with assignrow(int i) where you tell it which row to assign. Same with checkcol1, etc.
Those things could reduce your code size by 80% I'd imagine.
-
Thanks guys. Your suggestions should keep me busy for a while, so I'll get back to you later :)
-
So wait... can you or can you not do something like this:
Code:
if(grid[3][1]+grid[3][2]==4)
?
-
Yes, if grid is an array of integers.
-
Ok, thanks... dunno but it was giving me problems earlier so I just did
Code:
variable1=grid[1];
variable2=grid[2];
and then
Code:
if(variable1+variable2==4)
The 2nd worked but when I added grid[1] and grid[2] it didn't work?? Maybe I was doing it wrong
by 2nd I meant the code I just posted above.. that worked
Thinking back down, I think I was doing it completely wrong, so uhhh
here's another question:
Can you select a chunk of code and comment it with the keyboard?
-