Thread: Recursive function

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    38

    Recursive function

    i need help with this one:

    Code:
    #include <stdio.h>
    #include <string.h>
    char CountEvenASCII(char str[255], int a, int x);
    
    main()
    {
        char w[255];
        int num1, num2;
        printf("Input character string: ");
        scanf("%s", w);
        CountEvenASCII(w, num1, num2);
        getch();
        return 0;
    }
    
    char CountEvenASCII(char str[255], int a, int x)
    {
        a = x = 0;
        if (str[x] % 2 == 0)
        {
            a = (str[x] + a);
        }
        x++;
        printf("\n%d\n\n",a);
        CountEvenASCII(str,a,x);
    }
    It should display the number of letters with even ASCII values. Please tweak this one for me.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    How about you actually do the work and only request help for things you don't understand?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You make no effort of:
    1. returning anything from CountEvenASCII()
    2. printing the number counted in the count function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    You are assigning x to 0 every time you call the function.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    okay i understand.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    need help with this one

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char CountEvenASCII(char str[255], int a, int x);
    
    main()
    {
        int num1, num2;
        char w[255];
        num1 = 0;
        printf("Input character string: ");
        scanf("&#37;s", w);
        CountEvenASCII(w, num1, num2);
        printf("\n%d\n\n",w);
        getch();
        return 0;
    }
    
    char CountEvenASCII(char str[255], int a, int x)
    {
        x=strlen(str);
        if (str[x] % 2 == 0)
        {
            a = (str[x] + a);
        }
        x++;
        CountEvenASCII(str,a,x);
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you actually understand what your own code does?

    Do you understand how to solve the problem without recursion (as recursion, at least in this case, is just a way to iterate, we can ignore recursion for the first part of the problem - which is to count the number of even characters. Start by writing a function that does that, then we can work on making it recursive).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Now you are setting x to the length of the string each time? Do you realize this makes x++ meaningless?

    How does your function know that it is done?

    Do you know what a 'base case' is?

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    But i made the code, and a recursive is a function is one that calls itself. Can you proofread it for me? Please?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mkdl750 View Post
    But i made the code, and a recursive is a function is one that calls itself. Can you proofread it for me? Please?
    Yes, a recursive function is one that calls itself, but you don't seem to get what I'm trying to say:
    1. You still need to write code that produces the correct result - and it is CLEAR to me that you do not understand the code you have written ("made").
    2. My suggestion is to write the code you need to count first, then change that code to make it recursive.
    3. We do not do other peoples homework - you are going to school to learn, not to figure out how to copy something.

    By the way, as noops suggested, you may want to figure out the base-case (or exit condition) for your recursion - as it currently stands, I don't see any way that your code would even finish execution [that is, assuming the compiler does clever optimization and turns your recursion into an iteration, which any decent compiler should be able to do].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM