0
\$\begingroup\$

I'm really inexperienced with cycles, so I want to make this code shorter. As far as I know, I've made a recursive cycle.

for (Pointers[0] = 0; Pointers[0] < 8; Pointers[0]++)
    if (AdjM[Pointers[0], VertStart + 0])
    {
        for (Pointers[1] = 0; Pointers[1] < 8; Pointers[1]++)
        {
            if (AdjM[Pointers[1], Pointers[0]])
                if (Pointers[1] == VertEnd)
                    AddPath(2);

            for (Pointers[2] = 0; Pointers[2] < 8; Pointers[2]++)
            {
                if (AdjM[Pointers[2], Pointers[1]])
                    if (Pointers[2] == VertEnd)
                        AddPath(3);

                for (Pointers[3] = 0; Pointers[3] < 8; Pointers[3]++)
                {
                    if (AdjM[Pointers[3], Pointers[2]])
                        if (Pointers[3] == VertEnd)
                            AddPath(4);

                    for (Pointers[4] = 0; Pointers[4] < 8; Pointers[4]++)
                    {
                        if (AdjM[Pointers[4], Pointers[3]])
                            if (Pointers[4] == VertEnd)
                                AddPath(5);

                        for (Pointers[5] = 0; Pointers[5] < 8; Pointers[5]++)
                        {
                            if (AdjM[Pointers[5], Pointers[4]])
                                if (Pointers[5] == VertEnd)
                                    AddPath(6);

                            for (Pointers[6] = 0; Pointers[6] < 8; Pointers[6]++)
                            {
                                if (AdjM[Pointers[6], Pointers[5]])
                                    if (Pointers[6] == VertEnd)
                                        AddPath(7);

                                for (Pointers[7] = 0; Pointers[7] < 8; Pointers[7]++)
                                {
                                    if (AdjM[Pointers[7], Pointers[6]])
                                        if (Pointers[7] == VertEnd)
                                            AddPath(8);
                                }

                            }

                        }

                    }

                }

            }

        }
    }

I'm pretty sure it's easy to compress, but I don't see how. Please do not tell me to change the algorithm; it has to be like this.

\$\endgroup\$
6
  • 4
    \$\begingroup\$ It's hard to assess what's going on here without a plain-English description of what your code is actually doing. What does Pointers[] represent? What does the AdjM function do/check? What does the AddPath() function do? What do VertStart and VertEnd represent? What are you trying to accomplish with this code? \$\endgroup\$
    – nhgrif
    Commented Dec 18, 2014 at 22:40
  • \$\begingroup\$ pointers is just array of integers i use instead of i,j,k,l,m... . AdjM is boolean matrix(array) of existing edges betwin points. VertStart is the start point to search to Vert End. Actualy i'm getting all ways from VertStart to VertEnd to lists(AddPath) and do some processing lter. Hope you can help, cos i need it to my university. \$\endgroup\$
    – G0867532
    Commented Dec 18, 2014 at 22:55
  • \$\begingroup\$ can send you full code: pastebin.com/YHLSuk1m \$\endgroup\$
    – G0867532
    Commented Dec 18, 2014 at 22:56
  • \$\begingroup\$ @G0867532: The full code should be posted here. \$\endgroup\$
    – Jamal
    Commented Dec 19, 2014 at 0:26
  • 2
    \$\begingroup\$ There's just not enough context in the question for me to up-vote this. When I wrote my answer, I had no clue what the actual problem was that you were trying to solve, I still don't know. \$\endgroup\$ Commented Dec 19, 2014 at 12:16

2 Answers 2

12
\$\begingroup\$

I'm not convinced that what you are doing really makes sense, but I did see the recursive pattern in your code and I believe that this is how you can refactor it.

First of all, we create a method that you can call recursively:

void DoSomething(int index) {
    for (Pointers[index] = 0; Pointers[index] < 8; Pointers[index]++) {
        if (AdjM(Pointers[index], Pointers[index - 1]))
            if (Pointers[index] == VertEnd)
                AddPath(index + 1);

        if (index < 7) {
            DoSomething(index + 1);
        }
    }
}

Then, unfortunately, the outermost loop did not completely match this pattern so the first loop remains, but it can be used to call the second:

for (Pointers[0] = 0; Pointers[0] < 8; Pointers[0]++)
    if (AdjM(Pointers[0], VertStart))
    {
        DoSomething(1);
    }

A few other notes:

  • It is recommended to always use braces
  • Your number '8' is a 'magic number'. That should be extracted to a constant. Then you can also rewrite '7' as YOUR_MAGIC_NUMBER - 1
  • VertStart + 0 can be simplified to simply VertStart
\$\endgroup\$
0
5
\$\begingroup\$

When you have something like this:

if (AdjM[Pointers[1], Pointers[0]])
    if (Pointers[1] == VertEnd)
        AddPath(2);

You could just write it like this:

if (AdjM[Pointers[1], Pointers[0]] && Pointers[1] == VertEnd)
{
    AddPath(2);
}

There is no reason that you couldn't merge the two if statements, there were no else statements or anything there.

I also added braces, because of the amount of nesting that is going on here I wouldn't want to be confused about the scope.

There is several places that you do this where you can combine if statements.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.