iwanttoprogram
iwanttoprogram

Reputation: 541

Elimination of this goto statement

Was searching from ACM programming contest archives and found a solution to the coconuts program: It has a goto in it- how do I eliminate this? Is there a template or procedure to follow to do so. Thanks

/*
1997 East-Central ACM regional programming contest
Held on November 8, 1997
Coconuts, Revisited -- Problem 3
Sample solution by Ed Karrels, [email protected]
November 1997
*/
#include <stdio.h>
/* return 1 if this number of coconuts can be divided up
properly between this number of people */
int SplitCoco(long n_coco, long n_people) {
long i;
for (i=0; i<n_people; i++) {
/* check that the coconuts divide by the number of people
plus one remainder */
    if (n_coco % n_people != 1) return 0;
    /* remove 1 for the monkey, and one person's share */
        n_coco = n_coco - 1 - (n_coco / n_people);
    }
    /* check that the remaining coconuts divide evenly among
    the people */
    return (n_coco % n_people) == 0;
}


int main() {
    long n_coco;
    long n_people;
    long i, j, k;

    FILE *inf = stdin;
    while (fscanf(inf, "%ld", &n_coco), n_coco!=-1) {
    /* starting at the # of coconuts-1, count down until
    a number of people is found that works */
        for (n_people=n_coco-1; n_people > 1; n_people--) {
            if (SplitCoco(n_coco, n_people)) {
                 printf("%ld coconuts, %ld people and 1 monkey\n",
                 n_coco, n_people);
                 goto found;
                 /* OK, so yea, I put a 'goto' in my code :-)
                it was quick and it works. I don't do
                it often, I swear. */
            }
        }
     /* if no number of people greater than 1 works, there is
     no solution */
        printf("%ld coconuts, no solution\n", n_coco);
        found:
    }
    return 0;
}

Upvotes: 1

Views: 395

Answers (3)

Clifford
Clifford

Reputation: 93476

Another answer has already suggested this but also suggested it was less readable - I disagree - I find it reads like what it means. Either way like it or not it deserves illustrating as a possible solution.

My solution requires a definition of a boolean type. I have assumed the the C99 <stdbool.h> definition (or C++ compilation)

This is just the body of the outer while loop:

    bool found = false ;
    for (n_people=n_coco-1; n_people > 1 && !found; n_people--) 
    {
        found = SplitCoco(n_coco, n_people)
        if( found ) 
        {
            printf("%ld coconuts, %ld people and 1 monkey\n", n_coco, n_people);
        }
    }

    if( !found ) 
    {
        /* if no number of people greater than 1 works, there is no solution */
        printf("%ld coconuts, no solution\n", n_coco);
    }

In some cases it is possible that the additional per-loop test is prohibitive, but I would suggest that in most cases it is insignificant.

Upvotes: 1

user1651640
user1651640

Reputation: 181

replace

goto found;

with

 break;

replace

 printf("%ld coconuts, no solution\n", n_coco);

with:

if(n_people <= 1)
    printf("%ld coconuts, no solution\n", n_coco);

Upvotes: 4

In your case, you could make a separate routine countaining the while and replace the goto found with a return.

In general, you might replace each goto with a flag and some while loop. That does not make the code easier to read.

Upvotes: 8

Related Questions