0

I don't have the right to use more than 25 lines for a function. One of my function has just 26 lines so I want to simplified it.

char *name(int big, int small, int nb, char *tab)
{
    while (big < nb)
    {
        small = 1;
        while (small < nb)
        {
            ....
            printf("here are the other lines\n");
            ....
            small++;
         }
    big++;
    }
    return (tab);
}

big and small are initialize at 1. How could I include the small++ and big++ in the while condition?

I tried while (big++ && big < nb), it doesn't work why ? The only way I just found is while ((big = big + 1) && big < nb)

Does anyone can explain why the first way doesn't work and if you have another solution ?

Thanks

11
  • 1
    Maybe while (big++ < nb)? Commented Mar 1, 2017 at 10:04
  • 2
    What's the point of receiving small as a parameter if you just change its value every iteration of the outer loop? Commented Mar 1, 2017 at 10:04
  • The inner loop could easily be replaced by a for loop: for (int small = 1; small < nb; ++small) { ... } Commented Mar 1, 2017 at 10:05
  • Also while (small+ < nb). Commented Mar 1, 2017 at 10:06
  • 1
    Your function prototype is wrong, you have small twice, the second time without a type. Commented Mar 1, 2017 at 10:09

1 Answer 1

1

You probably want this (two lines less than your solution):

char *name(int big, int nb, char *tab)
{
    while (big++ < nb)
    {
        int small = 1;
        while (small++ < nb)
        {
            ....
            printf("here are the other lines\n");
            ....
        }
    }
    return tab;
}

Disclaimer: this is untested code.

while (big++ && big < nb) doesn't work, because it is simply not the same thing as:

while (big < nb)
{
  ...
  big++;
}

With while (big++ && big < nb) you increment big prior to the test. In the original code big is incremented after the test.

BTW: while ((big = big + 1) && big < nb) is the same thing as while (big++ < nb).

Sign up to request clarification or add additional context in comments.

1 Comment

For the incrementation before or after, I change the value to 0 instead of 1 to try, but my program didn't enter to the while. else, your code answer my question, I didn''t think about that way ! thank you

Your Answer

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