Now, before everyone says anything, I know that goto statements are never needed. Having said that, I was given an assignment to implement them into a sort of some type. I was assigned selection sort and figured that if I wrote the sort without go to's, it would be easier to just implement them after. Well I'm having trouble understanding how to implement them. Here's my code for the selection sort code. Array A is the array that contains the numbers that need to be sorted. Can anyone explain to me where goto statements would need to be implemented?

int i;

   *numberCompares = 0;
   *numberMoves = 0;
   for (i = 0; i <= n-2; i++)
   {
      int T,j,select = i;

      for (j = i+1; j <= n-1; j++)
      {
         if ( (*ShouldSelect)(A[select],A[j]) ) select = j;
         *numberCompares += 1;
      }
      T = A[i]; A[i] = A[select]; A[select] = T;
      ++*numberMoves, ++*numberMoves, ++*numberMoves;

Recommended Answers

All 5 Replies

I would offer that you should contend the basis of the assignment. Anything that makes you use a construct that is to be used lightly (if at all) in a context it was not intended to support is flawed in my opinion.
The goto can be used to implement control flow. That is, if you are a particular spot in your code and you want to be at another spot in your code at that moment a goto can do that (within reason - function boundaries are one limitation).
Now, consider that in light of the control flow you have within your code. You have a nested loop within an outer loop. Each of those loops maintains state about a starting condition, terminating condition and a 'do this every iteration' step. Once you understand all the intermingled state you would have to support to redo that with a goto you will be well equiped to challenge the assignment.

Okay, so what your telling me is that instead of the nested loop, a goto that points to the code in each loop would work? Am I understanding that right?

yes, for example, the loop that starts on line 5 could be rewritten like below, which is more like what you might write in assembly programs. Although goto is not generally used in C programs, it does show your teacher that you understand how loops work.

int i = 0;
start:
if( i > (n-1))
   goto end
// do something

++i;
goto start;
end:

Okay, well using that logic. I wrote this. It looks good to me but of course I wrote it so I'm going to think that. Anyone see anything wrong with this?

int i;

   *numberCompares = 0;
   *numberMoves = 0;
   i = 0;
   BEGIN:
   if(!(i <= n-2))goto END1; 
   {
       int T,j,select = 1;
       i++;

       FIRSTLOOP:
       j = i+1;
       if(!(j <= n-1)) goto BEGIN;
       {
           j++;

           if(!(*ShouldSelect)(A[select],A[j]) ) select = j; goto FIRSTLOOP;
                *numberCompares += 1;
       }
       T = A[i]; A[i] = A[select]; A[select] = T;
      ++*numberMoves, ++*numberMoves, ++*numberMoves;      
   }
END1:
   ;}

you don't need the braces on lines 8. 15, 20 and 23. Then reformat so that each line of code ligns up on the left side - no indention is needed.

Test it, run it, to find out if your program is correct or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.