Results 1 to 27 of 27

Thread: Recursive Function

  1. #1

    Thread Starter
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620

    Recursive Function

    Need a little assitance.
    Need a recursive function that will return ascii values of a string characters.
    Say the user enters: hi there
    The function would have to return the valud for 'h' then be called again from itself and return the value of 'h'+'i' and then call itself again, and keep on doing this untill the end of the string.

    I'm not sure how i would go about doing that.
    Any suggestions?

    Thanx,
    D!m
    Dim

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Well.. I am not sure exactly what u are after but it doesnt sound remotely like anything that needs recursion. maybe tell us what u want the function to do then we can provide an answer.
    Regards
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  3. #3

    Thread Starter
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    I need it to be recursive...
    here is what i mean:

    this recursive function needs to return ascii values of characters from a string. On the first base call it needs to return the value for the first character, then on the second call it needs to return the ascii value of the first character and the second character combined and so on.
    And i need it to be recursive...any suggetions?
    Dim

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    it's impossible unless you have the other way around, return 'h' + the call on 'i there'
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I know its not C++, but :

    VB Code:
    1. Private Function doIt(ByVal strString As String, ByVal startPosition As Long) As String
    2.     If startPosition = Len(strString) + 1 Then Exit Function
    3.     doIt = Asc(Mid(strString, startPosition, 1)) & "," & doIt(strString, startPosition + 1)
    4. End Function
    5.  
    6. Private Sub Form_Load()
    7.     MsgBox doIt("Hello", 1)
    8. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It's so nice to see you around here too Jamie, when are you going to learn C++?
    (yeah yeah I know about the books, i'm going to read them now)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Zaei
    Guest
    What is the object of the function? There seems to be no point to this at all.

    Z.

  8. #8

    Thread Starter
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    I'm just learning C++...and i'm at the recursive functions section. My assignment was to make a function that would do that. That's why i didn't want simply an answer, but a hit at what i should be doing.

    D!m
    Dim

  9. #9
    Zaei
    Guest
    Ah, I just got the idea. It wants to total the ASCII values of the string =). Gotcha. I wasnt clear on what the function was trying to do. Sorry =).

    Z.

  10. #10
    jim mcnamara
    Guest
    Code:
    main(){
           char *v = "hi there";
           ascii(v);
    }
    
    void ascii(char *s){
            if (*s== '\0'){
                 printf("\n");
                 return;
            }
            printf("%d ",*s++);
            ascii(s);      
    }

  11. #11

    Thread Starter
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    Thanx guys, but i got it going. Just so you know what i meant, here is the working code
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void ascii(string, int&, int);
    
    int main()
    {
       string word;
       int temp = 0;
       int i = 0;
       cout << "Please enter a string: ";
       getline(cin, word);
       ascii(word, temp, i);
       return 0;
    }
    
    void ascii(string n, int& x, int d)
    {
          if(d <= n.length())
          {
             char a = n.at(d);
             x += int(a);
             d++;
             cout << x << " ";
             ascii(n, x, d);
          }
    }
    Thanx,
    D!m
    Dim

  12. #12
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by kedaman
    It's so nice to see you around here too Jamie, when are you going to learn C++?
    (yeah yeah I know about the books, i'm going to read them now)
    Well because I know Java already, I'm tinkering around with C++.
    Couldn't get a DLL to work... VB couldn't find the API entry point... um then decided to start simple and do a loop. Got the loop code okay, but I couldn't print out the numbers
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  13. #13
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    There's a pretty good tutoiral on C++ on this forum. Once you've compiled a C++ program, how d'you get it to run?
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  14. #14
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Just double click on the shaggin' thing
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  15. #15
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    Yeah, but it goes through all this bollox about "You have not created a space for this program blah, blah, blah...."

    Any way around that?
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  16. #16
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well you go into VC++ right, then go into New, then do a new console application. Let it create all the workspaces and bollox it wants.

    To compile + run, click on the red exclamation mark
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  17. #17
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    Thanks. Will try that later.

    Won't be able to submit this lab - my user profile in the ICT labs is still FUBAR.

    Could you tell me what's wrong with Java on these machines (Arches)? I type 'javac Blah.java' and it says "javac is not a recognised internal or external command, operable program or batch file"
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  18. #18
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    They don't have java installed, or the path variable is ****ed.
    Where is the comp sci lab on ?
    And do I have to turn up ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  19. #19
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    The Lab's on in the ICT2 Lab and no, you don't have to turn up - just so long as you get that method done (he-heh, "Methadone")

    Well, J++ is installed on these machines, so I assume it's got something to do with the Classpath.
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  20. #20
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    J++ may be installed, doesn't necessarily mean that the JDK is though. I'll do the binarytree3.java thing here in work.
    And this comms tutorial. Whats the story with that ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  21. #21
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    You turn up with the book (I've got a copy - don't worry) and do the questions he's marked out. There's a demonstrator there who you can ask questions or get to check your answers. It's in the James Usher Theatre (Orange Door in Arts Block, I think)

    Probably not a good idea to go messing around with the JDK in here then...
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  22. #22
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    See you at the arts entrance at ten to for the comms tutorial ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  23. #23
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    Yeah, sure. You going to the Com Tech lecture and lab today?
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  24. #24
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Lecture yeah but we've people from pc live coming in to us to look at the program at 4
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  25. #25
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    Let me know how that goes. And email me any solutions you get to BinaryTree3 please
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  26. #26
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Hmm.
    Well I'll start work on the binarytree3 in ten minutes.
    Text me or email to remind me to start working on it
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  27. #27
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    Heading over to the Labs now to see if that .reg file will get the Startup Menu working. See you in a few mins...
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width