14 Replies - 2879 Views - Last Post: 22 October 2013 - 06:46 AM Rate Topic: -----

#1 erkant   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Recursive function for getting the sum of ascii values of digits

Posted 19 October 2013 - 10:47 AM

I have the code below and it is working just fine, though it is not a recursive function. I want to write a recursive function which will do the same job. I have an integer number, and I want to get the sum of all the digits' ASCII values.

#include <stdio.h>

int ascii(int num)
{
    int temp = num;
    int i, counter = 0;
    int total = 0;

    while(num > 0)
    {
        num /= 10;
        counter++;
    }

    char arr[counter];

    for(i = 0; i < counter; i++)
        arr[i] = '0';

    for(i = 0; i < counter && temp > 0; i++)
    {
        arr[i] += temp%10;
        temp /= 10;
    }

    for(i = 0; i < counter; i++)
        total += arr[i];

    return total;
}

int recascii(int num)
{
    return (char)num%10 + recascii(num/10);
}

int main()
{
    printf("%d", ascii(1234));

    return 0;
}


This post has been edited by erkant: 20 October 2013 - 04:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Recursive function for getting the sum of ascii values of digits

#2 salazar   User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 648
  • Joined: 26-June 13

Re: Recursive function for getting the sum of ascii values of digits

Posted 19 October 2013 - 11:29 AM

Do you have any idea of where to start?
Was This Post Helpful? 0
  • +
  • -

#3 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: Recursive function for getting the sum of ascii values of digits

Posted 19 October 2013 - 11:36 AM

As a hint, the sumOfASCIIDigits of 12345 = sumOfASCIIDigits of 1234 + (5+'0').
Was This Post Helpful? 0
  • +
  • -

#4 salazar   User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 648
  • Joined: 26-June 13

Re: Recursive function for getting the sum of ascii values of digits

Posted 19 October 2013 - 11:59 AM

How would you break the problem down to a similar smaller version of the problem?
Was This Post Helpful? 0
  • +
  • -

#5 erkant   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Recursive function for getting the sum of ascii values of digits

Posted 20 October 2013 - 04:49 AM

I added a recursive function called recascii. What do you think? I guess I need to keep dividing the number with 10, and also do modulo 10 to get the value of each number, but then again I need to get their ASCII value.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7382
  • View blog
  • Posts: 15,312
  • Joined: 16-October 07

Re: Recursive function for getting the sum of ascii values of digits

Posted 20 October 2013 - 05:40 AM

I think a new post would be less confusing...

Also, I think recascii will never stop running. Well, it will, when your seg fault. ;)

Can you write a loopy version that doesn't use an array? That would be a place to start.
Was This Post Helpful? 0
  • +
  • -

#7 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Recursive function for getting the sum of ascii values of digits

Posted 20 October 2013 - 02:22 PM

I wouldn't look too hard at the iterative version of the code for this. Recursive code is like this:

You are in a central living room, and notice a door. You open the door - it's a room.
1) you walk into a room, you do something, and see another room's door.
  2) you walk into the other room, do something, and see another room's door.
     3) you walk into the other room, do something, and see another room's door.
        4) you walk into this room, and see that there are no more second doors.
           You pick up a card with your name on it. It says "go back to the central living room".

           you walk out of room #4. You do something before you leave.
      you walk out of room #3. You do something before you leave.
   you walk out of room #2. You do something before you go.
you walk out of room #1. You do something before you go 


And you're back into the central living room.

When you walked into the higher numbered rooms, you were going one depth deeper, into recursion. The "something" that you do as you walk deeper, may be nothing at all - maybe you put it off and only do something, on the way back up out of the rooms.

The "card" is the base case - every recursive function must have one, or it will loop until it crashes. You can go pretty deep with recursion, but you can't go infinitely deep, for sure!

Recursion relies on using the "stack" that each depth will have (and keep), as you move around. It's like a tablet of paper. You only see and use the paper on the top - but the deeper pages are there, and if they have writing on them, the writing is still on the page, too.

With your problem, you can solve it going downward, or going upward, back out of the recursion. First thing is to set up the base case that will test whether you have reached the last room or not.
Was This Post Helpful? 0
  • +
  • -

#8 erkant   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Recursive function for getting the sum of ascii values of digits

Posted 20 October 2013 - 03:36 PM

This one returns zero.

int ascii(int num)
{
   if(num == 0)
       return 0;
   else
       return (char)num%10 + ascii(num/10);
}


Was This Post Helpful? 0
  • +
  • -

#9 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Recursive function for getting the sum of ascii values of digits

Posted 20 October 2013 - 06:09 PM

if(num==0) ?

Ascii values range from 0 to 127, so if you are using increasing values of num, then the function will exit with num=0, immediately.

call your function with ascii(0,0).

That is, "num" is the checker for when we're going to stop recursing. The second 0 in the call is the parameter sum, which of course, will be equal to 0, on this first call.

Now we need a base case, to get a return going.
int ascii(int variable1, int variable2)

//What would the two variables be for this //function to use the following?

if(num<128) {
   sum+=num;
   ++num;
   recurse(put in your parameters here);
}
else
   return what needs to be returned?


Was This Post Helpful? 0
  • +
  • -

#10 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: Recursive function for getting the sum of ascii values of digits

Posted 20 October 2013 - 11:09 PM

Your logic is just about correct, erkant.

Quote

(char)num%10


I believe this will not do what you intend for it to do. If num is 12345, it might be trying to cast 12345 to a character which is not what you want it to do. Even if it is doing the % operation first, it still may not be what you intend. In some languages, (char)5 would result in the 5th ascii value instead of '5' which is the 53rd ascii value. I recommend sticking with what you were originally doing and just add '0' (or 48) to the result of the % operation plus the recursive call.
Was This Post Helpful? 0
  • +
  • -

#11 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Recursive function for getting the sum of ascii values of digits

Posted 21 October 2013 - 04:47 AM

View Postmojo666, on 20 October 2013 - 11:09 PM, said:

Your logic is just about correct, erkant.

Quote

(char)num%10


I believe this will not do what you intend for it to do. If num is 12345, it might be trying to cast 12345 to a character which is not what you want it to do. Even if it is doing the % operation first, it still may not be what you intend. In some languages, (char)5 would result in the 5th ascii value instead of '5' which is the 53rd ascii value. I recommend sticking with what you were originally doing and just add '0' (or 48) to the result of the % operation plus the recursive call.


The cast to a char is never needed with % 10, because the largest number you would cast is 9, and any integer value less than +-128, will fit into a char, without a cast.
Was This Post Helpful? 0
  • +
  • -

#12 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: Recursive function for getting the sum of ascii values of digits

Posted 21 October 2013 - 07:46 AM

View PostAdak, on 21 October 2013 - 12:47 PM, said:

The cast to a char is never needed with % 10, because the largest number you would cast is 9, and any integer value less than +-128, will fit into a char, without a cast.


According to cppreference, the typecast takes precedence over the mod operator, so it should be trying to cast the whole number before performing the %. Even if it is fixed to perform % first, it might cast it to the wrong value, so I stick by my recommendation.

This post has been edited by mojo666: 21 October 2013 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

#13 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Recursive function for getting the sum of ascii values of digits

Posted 21 October 2013 - 02:08 PM

View Postmojo666, on 21 October 2013 - 07:46 AM, said:

View PostAdak, on 21 October 2013 - 12:47 PM, said:

The cast to a char is never needed with % 10, because the largest number you would cast is 9, and any integer value less than +-128, will fit into a char, without a cast.


According to cppreference, the typecast takes precedence over the mod operator, so it should be trying to cast the whole number before performing the %. Even if it is fixed to perform % first, it might cast it to the wrong value, so I stick by my recommendation.


It can't cast a an integer "to the wrong value", if it's within range. You're argument is the same as saying a small apple isn't an apple, without a cast. Chars ARE small ints.
Was This Post Helpful? 0
  • +
  • -

#14 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: Recursive function for getting the sum of ascii values of digits

Posted 21 October 2013 - 10:52 PM

The OP is making a program in which 19 would need to return '1'+'9' which is 106. It may be different for c++, but in many languages casting an int "n" will just return the nth ascii value, so (char)(19%10) will return '\t' instead of '9'. Thus, it may be returning the wrong value.

This post has been edited by mojo666: 21 October 2013 - 10:53 PM

Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg   User is offline

  • member icon

Reputation: 5670
  • View blog
  • Posts: 17,408
  • Joined: 25-December 09

Re: Recursive function for getting the sum of ascii values of digits

Posted 22 October 2013 - 06:46 AM

In C or C++ a char is a number. For example the char '\t' has the value of 9. A signed char may hold values between 0 and 127, which can be mapped to the ASCII characters.

The only time you may need to cast a char to in int is to print the value instead of the character representation.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1