Thread: corecursive

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    Unhappy corecursive

    I am having problems with a pair of corecursive functions I am trying to write. One function should sum the alphabetic characters in a string and the other function should sum the total of the digits in the same string. They should call each other. What I have so far is below, but I'm not sure how to fix the errors or what I am missing? Any and all help would be greatly appreciated.

    Code:
    void count_alph(char *ctest_string,int *alph_sum, int *digit_sum)
    {
    	int i=0;
    	static int alpha_count;
    	*alph_sum= alpha_count;
    
    	while (ctest_string[i]!='\0')
    		if (isalpha(ctest_string[i++]))
    			++alpha_count;
    		else
    			sum_digit(ctest_string, digit_sum, alph_sum);
    }
    
    void sum_digit(char *stest_string, int *digit_sum, int *alph_sum)
    {	
    	int j=0;
    	static int sum_count;
    	*digit_sum=sum_count;
    	
    	while (stest_string != '\0')
    		if(isdigit(stest_string[j++]))
    			sum_count += stest_string[j];
    		else
    			count_alph(stest_string,digit_sum,alph_sum);
    }

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    It seems really queer to me...
    First, why you are using two functions, even recursive ones, for such a simple task, which can be done in single loop?

    And that assignment
    *alph_sum= alpha_count;
    seems not sufficient: as I suppose, in *alph_sum should be result after calling the count_alph(). But you assign something to that and then change the alpha_count, and this change won't appear in the result *alph_sum.

    It's good (at least to better readability) to assign a value before using, even if it is static and so it's done by compiler (at least I think that it is zeroed).

    And in second loop the condition
    stest_string != '\0' seems to be not what you mean, because you are comparing non-zero pointer (if call to your function is meaningful) to zero ('\0'), so it's still true.
    Condition in the first loop is far more better.

    What if the string argument of your function is NULL pointer? Then you will receive sigsegv or something like that because you are trying to acces value string[0].
    Last edited by aerian; 09-18-2003 at 02:48 AM.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    4
    The problems with the code that is why I am here. Unfortunately the exercise calls for a pair of corecursive functions. I can code them with an iterative loop, but that is not what the exercise calls for. Any ideas on how to fix the code?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Still I cannot figure out what should the functions do... Why should they call each other?

    Some bugs I commented in my previous post (condition in second function, ...).

    Try it on string 'aaa1aaa'. If I understand you correctly, the result should be 6 for char count and 1 for digit count. What will your code do? It will (after fixing the things I considered errors) count 3*a, then call sum_digit(), which will count 1, then call count_alph()... and because they are always called with initial string (because j is local and automatic variable in both functions), it will never end...

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Ah, I got some idea

    The count_alph() should count alphabetical characters, and on the first occurence of digit it should call sum_digits(), but not with initial string, but only with rest of the string (thus &string_name[j]), so the string will shorten and shorten and finally it will be "" and both functions will end. So, after calling the other function, the current function should stop counting, thus you have to add break; statement to that else part in if statement.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    4
    Thanks for the good idea! Let me plug it in and do some more adjusting. I don't know why they wrote this exercise is like this. Please stay tuned but hopefully this will do the trick.

Popular pages Recent additions subscribe to a feed