|
-
Feb 5th, 2002, 01:03 AM
#1
Thread Starter
Fanatic Member
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
-
Feb 5th, 2002, 01:17 AM
#2
PowerPoster
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
-
Feb 5th, 2002, 01:21 AM
#3
Thread Starter
Fanatic Member
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?
-
Feb 5th, 2002, 04:16 AM
#4
transcendental analytic
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.
-
Feb 5th, 2002, 04:25 AM
#5
Retired VBF Adm1nistrator
I know its not C++, but :
VB Code:
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Feb 5th, 2002, 05:22 PM
#6
transcendental analytic
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.
-
Feb 5th, 2002, 07:41 PM
#7
What is the object of the function? There seems to be no point to this at all.
Z.
-
Feb 5th, 2002, 09:01 PM
#8
Thread Starter
Fanatic Member
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
-
Feb 5th, 2002, 09:34 PM
#9
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.
-
Feb 5th, 2002, 10:00 PM
#10
Code:
main(){
char *v = "hi there";
ascii(v);
}
void ascii(char *s){
if (*s== '\0'){
printf("\n");
return;
}
printf("%d ",*s++);
ascii(s);
}
-
Feb 5th, 2002, 10:53 PM
#11
Thread Starter
Fanatic Member
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
-
Feb 6th, 2002, 03:12 AM
#12
Retired VBF Adm1nistrator
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]
-
Feb 6th, 2002, 04:10 AM
#13
Addicted Member
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...."
-
Feb 6th, 2002, 04:13 AM
#14
Retired VBF Adm1nistrator
Just double click on the shaggin' thing
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Feb 6th, 2002, 04:16 AM
#15
Addicted Member
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...."
-
Feb 6th, 2002, 04:19 AM
#16
Retired VBF Adm1nistrator
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]
-
Feb 6th, 2002, 04:24 AM
#17
Addicted Member
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...."
-
Feb 6th, 2002, 04:29 AM
#18
Retired VBF Adm1nistrator
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]
-
Feb 6th, 2002, 04:35 AM
#19
Addicted Member
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...."
-
Feb 6th, 2002, 04:37 AM
#20
Retired VBF Adm1nistrator
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]
-
Feb 6th, 2002, 04:41 AM
#21
Addicted Member
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...."
-
Feb 6th, 2002, 04:43 AM
#22
Retired VBF Adm1nistrator
See you at the arts entrance at ten to for the comms tutorial ?
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Feb 6th, 2002, 04:46 AM
#23
Addicted Member
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...."
-
Feb 6th, 2002, 04:47 AM
#24
Retired VBF Adm1nistrator
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]
-
Feb 6th, 2002, 04:48 AM
#25
Addicted Member
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...."
-
Feb 6th, 2002, 04:51 AM
#26
Retired VBF Adm1nistrator
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]
-
Feb 6th, 2002, 04:54 AM
#27
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|