PDA

Click to See Complete Forum and Search --> : Recursive Function


Dim
Feb 5th, 2002, 12:03 AM
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

beachbum
Feb 5th, 2002, 12:17 AM
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

Dim
Feb 5th, 2002, 12:21 AM
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?

kedaman
Feb 5th, 2002, 03:16 AM
it's impossible unless you have the other way around, return 'h' + the call on 'i there'

plenderj
Feb 5th, 2002, 03:25 AM
I know its not C++, but :


Private Function doIt(ByVal strString As String, ByVal startPosition As Long) As String
If startPosition = Len(strString) + 1 Then Exit Function
doIt = Asc(Mid(strString, startPosition, 1)) & "," & doIt(strString, startPosition + 1)
End Function

Private Sub Form_Load()
MsgBox doIt("Hello", 1)
End Sub

kedaman
Feb 5th, 2002, 04:22 PM
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)

Zaei
Feb 5th, 2002, 06:41 PM
What is the object of the function? There seems to be no point to this at all.

Z.

Dim
Feb 5th, 2002, 08:01 PM
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

Zaei
Feb 5th, 2002, 08:34 PM
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.

jim mcnamara
Feb 5th, 2002, 09:00 PM
main(){
char *v = "hi there";
ascii(v);
}

void ascii(char *s){
if (*s== '\0'){
printf("\n");
return;
}
printf("%d ",*s++);
ascii(s);
}

Dim
Feb 5th, 2002, 09:53 PM
Thanx guys, but i got it going. Just so you know what i meant, here is the working 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

plenderj
Feb 6th, 2002, 02:12 AM
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 :rolleyes:

MethadoneBoy
Feb 6th, 2002, 03:10 AM
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?

plenderj
Feb 6th, 2002, 03:13 AM
Just double click on the shaggin' thing :)

MethadoneBoy
Feb 6th, 2002, 03:16 AM
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? :)

plenderj
Feb 6th, 2002, 03:19 AM
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

MethadoneBoy
Feb 6th, 2002, 03:24 AM
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"

plenderj
Feb 6th, 2002, 03:29 AM
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 ?

MethadoneBoy
Feb 6th, 2002, 03:35 AM
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") :D

Well, J++ is installed on these machines, so I assume it's got something to do with the Classpath.

plenderj
Feb 6th, 2002, 03:37 AM
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 ?

MethadoneBoy
Feb 6th, 2002, 03:41 AM
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...

plenderj
Feb 6th, 2002, 03:43 AM
See you at the arts entrance at ten to for the comms tutorial ?

MethadoneBoy
Feb 6th, 2002, 03:46 AM
Yeah, sure. You going to the Com Tech lecture and lab today?

plenderj
Feb 6th, 2002, 03:47 AM
Lecture yeah but we've people from pc live coming in to us to look at the program at 4 :rolleyes:

MethadoneBoy
Feb 6th, 2002, 03:48 AM
Let me know how that goes. And email me any solutions you get to BinaryTree3 please ;)

plenderj
Feb 6th, 2002, 03:51 AM
Hmm.
Well I'll start work on the binarytree3 in ten minutes.
Text me or email to remind me to start working on it :)

MethadoneBoy
Feb 6th, 2002, 03:54 AM
Heading over to the Labs now to see if that .reg file will get the Startup Menu working. See you in a few mins...