Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

C30N9

Members
  • Content count

    1331
  • Joined

  • Last visited

5 Followers

About C30N9

  • Rank
    Senior Member

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

Single Status Update

See all updates by C30N9

  1. Yesterday, in my C programming lab, I was solving problems with recursions, the hardest part was that some of them required using variables inside recursive functions for better solutions. I tried to avoid declaring variables inside them as that would harm the memory, so I ended up declaring global variables, which ended in a mess. Here is an example of my functions:

    code:
    Array
    The number "10" in the above code is the size of the array I declared later in the main function. I could use a constant value of 10 in SIZE, but a better way is to put another integer variable in the function to store length value like the comment I wrote, but this will make it change its value to the new length, and it will keep declaring integer type variable.
    code:
    Array
    This messy function is supposed to check if a string is a palindrom string. length's value is the length of the character array "LeooeL", same issue as the first code (putting the length as a variable). z (didn't bother to pick a name for it) should be used to indicate whenever it should stop checking, but I didn't finish the function, so I put a comment for each statement concerning it, as it's the not the issue here. Notice where I declared the variables, not favorable.

    1. Show previous comments  4 more
    2. Jonathan

      Jonathan

      C30N9, that extra else if clause you inserted isn't necessary. The && logical operator is short-circuiting in C, meaning that if the left side evaluates to false, the right side recursive call will not be executed, and the result will be 0, which will be returned.

    3. C30N9

      C30N9

      That's new. Neat.

      Any solutions for the first function?

    4. fraggle

      fraggle

      Jonathan said:

      C30N9, that extra else if clause you inserted isn't necessary. The && logical operator is short-circuiting in C, meaning that if the left side evaluates to false, the right side recursive call will not be executed, and the result will be 0, which will be returned.

      And even if C didn't have short-circuiting, the c[0] == c[len - 1] part in the third return statement would be redundant.

      Actually you can simplify further to just:

      code:
      Array

×