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();

        }
    }
}

13%, 7:08 PM As H hw2.doc Jordan university of Science and Technology Computer Engineering Department CPE 4800 HWW2 Notes: 1-Late home works will not be accepted. 2- Due date is 22/11/2016 at 11:00 Am. 3- Submit your source code file (cs) only and write your name as a comment inside the file. Work: Write a program to generate a 5x5 magic square using the Simulated algorithm. Your program should print the output like the following example: 2 25 17 5 13 21 9 Also print out the execution time off the code that finds the solution (don\'t include the inpuwoutput timc). A 5x5 magic square isa square that contains the numbers between 1 and 25 that has the property that all snows, columns. and diagonals sumato an equal va ue fi5(as you can see finom the above e). For your implementation consider the sum of the absolute examp difference between the sum of each row, column, and diagonal from 65 to be your heuristic function (fitness function). Notice that h(magic square) is cqual to zero. Also use the operalion of swapping two randkim num bers to be your action of generating a child for the curren node. Your program should start hy generating a randkam 5x5 square (note that numbers can\'t be repeated) and then start improving on this state until reaching a goalmagic square) by the guidance of the fitness function. Remember that simulated annealing algorithm starts by allowing bad moves with a given probability then lawers that prohability as we appraach the goal his probability should never reach zera ar your algorithm wi get stuck at a local minimum (Good Luck) 4:14 PM L

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.

 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
 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
 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
 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
 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
 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site