please help me whats the error in the program the code using
please help me
what\'s the error in the program
the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HM2_me
{
class node
{
public int[,] magicsq = new int[5, 5];
public static int generated;
public int heuristic = 12;
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max)
{
lock (syncLock)
{ // synchronize
return random.Next(min, max);
}
}
public node()
{
int g = 1;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
magicsq[i, j] = g++;
}
}
for (int i = 0; i < 25; ++i)
{
Random random = new Random();
int a, b, c, d, e;
a = RandomNumber(0, 4);
b = RandomNumber(0, 4);
c = RandomNumber(0, 4);
d = RandomNumber(0, 4);
while (a == c && b == d)
{
c = RandomNumber(0, 5);
d = RandomNumber(0, 5);
}
e = magicsq[c, d];
magicsq[c, d] = magicsq[a, b];
magicsq[a, b] = e;
}
if (magicsq[0, 0] + magicsq[1, 0] + magicsq[2, 0] + magicsq[3, 0] + magicsq[4, 0] == 65) --heuristic;
if (magicsq[0, 1] + magicsq[1, 1] + magicsq[2, 1] + magicsq[3, 1] + magicsq[4, 1] == 65) --heuristic;
if (magicsq[0, 2] + magicsq[1, 2] + magicsq[2, 2] + magicsq[3, 2] + magicsq[4, 2] == 65) --heuristic;
if (magicsq[0, 3] + magicsq[1, 3] + magicsq[2, 3] + magicsq[3, 3] + magicsq[4, 3] == 65) --heuristic;
if (magicsq[0, 4] + magicsq[1, 4] + magicsq[2, 4] + magicsq[3, 4] + magicsq[4, 4] == 65) --heuristic;
if (magicsq[0, 0] + magicsq[0, 1] + magicsq[0, 2] + magicsq[0, 3] + magicsq[0, 4] == 65) --heuristic;
if (magicsq[1, 0] + magicsq[1, 1] + magicsq[1, 2] + magicsq[1, 3] + magicsq[1, 4] == 65) --heuristic;
if (magicsq[2, 0] + magicsq[2, 1] + magicsq[2, 2] + magicsq[2, 3] + magicsq[2, 4] == 65) --heuristic;
if (magicsq[3, 0] + magicsq[3, 1] + magicsq[3, 2] + magicsq[3, 3] + magicsq[3, 4] == 65) --heuristic;
if (magicsq[4, 0] + magicsq[4, 1] + magicsq[4, 2] + magicsq[4, 3] + magicsq[4, 4] == 65) --heuristic;
if (magicsq[0, 0] + magicsq[1, 1] + magicsq[2, 2] + magicsq[3, 3] + magicsq[4, 4] == 65) --heuristic;
if (magicsq[4, 0] + magicsq[3, 1] + magicsq[2, 2] + magicsq[1, 3] + magicsq[0, 4] == 65) --heuristic;
++generated;
}
}
class Program
{
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max)
{
lock (syncLock)
{ // synchronize
return random.Next(min, max);
}
}
public static void cout(node a)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write(a.magicsq[i, j] + \" \");
}
Console.WriteLine(\" \");
}
}
public static node Swap(node a)
{
Random random = new Random();
int v, b, c, d, e;
v = RandomNumber(0, 4);
b = RandomNumber(0, 4);
c = RandomNumber(0, 4);
d = RandomNumber(0, 4);
while (v == c && b == d)
{
c = RandomNumber(0, 4);
d = RandomNumber(0, 4);
}
e = a.magicsq[c, d];
a.magicsq[c, d] = a.magicsq[v, b];
a.magicsq[v, b] = e;
++node.generated;
return a;
}
public static int calc_hu(int[,] a)
{
int heuristic = 12;
if (a[0, 0] + a[1, 0] + a[2, 0] + a[3, 0] + a[4, 0] == 65) --heuristic;
if (a[0, 1] + a[1, 1] + a[2, 1] + a[3, 1] + a[4, 1] == 65) --heuristic;
if (a[0, 2] + a[1, 2] + a[2, 2] + a[3, 2] + a[4, 2] == 65) --heuristic;
if (a[0, 3] + a[1, 3] + a[2, 3] + a[3, 3] + a[4, 3] == 65) --heuristic;
if (a[0, 4] + a[1, 4] + a[2, 4] + a[3, 4] + a[4, 4] == 65) --heuristic;
if (a[0, 0] + a[0, 1] + a[0, 2] + a[0, 3] + a[0, 4] == 65) --heuristic;
if (a[1, 0] + a[1, 1] + a[1, 2] + a[1, 3] + a[1, 4] == 65) --heuristic;
if (a[2, 0] + a[2, 1] + a[2, 2] + a[2, 3] + a[2, 4] == 65) --heuristic;
if (a[3, 0] + a[3, 1] + a[3, 2] + a[3, 3] + a[3, 4] == 65) --heuristic;
if (a[4, 0] + a[4, 1] + a[4, 2] + a[4, 3] + a[4, 4] == 65) --heuristic;
if (a[0, 0] + a[1, 1] + a[2, 2] + a[3, 3] + a[4, 4] == 65) --heuristic;
if (a[4, 0] + a[3, 1] + a[2, 2] + a[1, 3] + a[0, 4] == 65) --heuristic;
return heuristic;
}
static void Main(string[] args)
{
DateTime begin = DateTime.Now;
Console.WriteLine(\"******************************\");
Console.WriteLine(\"***BEGINING OF SEARCH***\");
start:
//Console.WriteLine(\"START OVER!!\");
node[] all = new node[25];
node[] original = new node[25];
for (int i = 0; i < 25; ++i)
original[i] = new node();
for (int i = 0; i < 25; ++i)
{
if (original[i].heuristic == 0)
{
Console.WriteLine(\"******************************\");
Console.WriteLine(\"FOUND A SOLUTION!\ \" + \"***************\ \" + \"THE MAGIC SEQUARE IS : \");
Program.cout(original[i]);
Console.WriteLine(\"***************\ NuMER OF GENERATED NODES IS : \" + node.generated);
Console.WriteLine(\"***************\ THE TIME IT TOOK TO MAKE A MAGIC SQUARE IS : {0}\", DateTime.Now.Subtract(begin));
Console.WriteLine(\"******************************\");
goto Exit;
}
}
node[] chiled = new node[25];
for (int i = 0; i < 25; ++i)
chiled[i] = new node();
Loop:
for (int f = 0; f < 25; ++f)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
chiled[f].magicsq[i, j] = original[f].magicsq[i, j];
}
}
}
for (int i = 0; i < 25; ++i)
chiled[i] = Program.Swap(chiled[i]);
for (int i = 0; i < 25; ++i)
chiled[i].heuristic = Program.calc_hu(chiled[i].magicsq);
for (int i = 0; i < 25; ++i)
{
if (original[i].heuristic == 0)
{
Console.WriteLine(\"******************************\");
Console.WriteLine(\"FOUND A SOLUTION!\ \" + \"***************\ \" + \"THE MAGIC SEQUARE IS : \");
Program.cout(original[i]);
Console.WriteLine(\"***************\ NuMER OF GENERATED NODES IS : \" + node.generated);
Console.WriteLine(\"***************\ THE TIME IT TOOK TO MAKE A MAGIC SQUARE IS : {0}\", DateTime.Now.Subtract(begin));
Console.WriteLine(\"******************************\");
goto Exit;
}
}
for (int i = 0; i < 25; ++i)
{
all[i] = original[i];
// all[5 + i] = chiled[i];
}
for (int f = 0; f < 25; ++f)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
all[f].magicsq[i, j] = original[f].magicsq[i, j];
all[5 + f].magicsq[i, j] = chiled[f].magicsq[i, j];
}
}
}
Array.Sort(all, delegate(node x, node y) { return x.heuristic.CompareTo(y.heuristic); });
for (int f = 0; f < 25; ++f)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
original[f].magicsq[i, j] = all[f].magicsq[i, j];
}
}
}
for (int i = 0; i < 25; ++i)
original[i].heuristic = Program.calc_hu(original[i].magicsq);
if (node.generated % 20000 == 0)
goto start;
else
goto Loop;
//***************************************************
Console.WriteLine(\"THIS IS ORIGINALS\");
foreach (node s in original)
Program.cout(s);
//***************************************************
Console.WriteLine(\"THIS IS CHILDS\");
foreach (node s in chiled)
Program.cout(s);
Console.WriteLine(\"THE TIME IT TOOK TO MAKE A MAGIC SQUARE IS : {0}\", DateTime.Now.Subtract(begin));
Exit: ;
Console.Read();
}
}
}
Solution
As per your screenshots error is thrown of array out of range on the line :
all[5 + f].magicsq[i, j] = chiled[f].magicsq[i, j];
As you can see that, you have initialzed :
node[] all = new node[25];
so that it has allocated only 25 bit of space for all variable.
Now your loop start to initialize f=0 and in this line : all[5 + f].magicsq[i, j] = chiled[f].magicsq[i, j]; , when f become more than 20 than it will become all[26], that will be beyond the array range.
So that\'s why this error is coming into the program.
All you can do is update your array length to more than 25 so that you no need to change your entire logic.
If you have any further query than let me know.





