Any C++ programmers here? - TennisForum.com
Reply
 
LinkBack Thread Tools
post #1 of 27 (permalink) Old Sep 2nd, 2008, 12:03 AM Thread Starter
Kart & I against the WtaWorld!
 
Rocketta's Avatar
 
Join Date: Nov 2001
Location: NC
Posts: 35,922
                     
Any C++ programmers here?

I often have problems with the most simple code. I have to write a code that determines a strings link and use recursion in it? They only let you pass the array of characters as a constant to the function. Help, I'm stumped over this easy little program.
Rocketta is offline  
Sponsored Links
Advertisement
 
post #2 of 27 (permalink) Old Sep 2nd, 2008, 07:32 AM
Senior Member
 
sandv1's Avatar
 
Join Date: Jan 2007
Posts: 2,592
                     
Re: Any C++ programmers here?

Long-time prg student.... but only w/ 1.5 years working w/ C++...

From what I can tell, it looks like (with whatever you're doing) you'll need to use "pointer math" (use a search engine to research the term). More to the point, you have to _first_ establish the address of the parameter. Also, since your passing a const parameter, you will likely have to do some copying of the parameter in yr recursive function even if it's only to cycle through one char at a time.

If recursion is required, then pointer math plays into that. In other words, your rec. func. should be re-setting the pointer to current address plus one. You will likely have a flag in this func. to find the end of the term.

For example, say you're passing the string "ballbasher" ( :-p ), you are first going to find the starting point of where that specific char array is located in memory i.e. where the first 'b' is stored in memory. If the function reaches the end of the term, then the flag should go "found it!" i.e. char == null character or '\0' ...or by however you are terminating the function. Otherwise, the rec. func. should keep going (and, in turn, cycling through the term one char at a time by increasing your variable address by one. Obviously, the function will call itself again if the term hasn't been exhausted yet.... or if the flag is missed!)

This should get you started at least...

By the way, websites such as cplusplus.com have good tutorials, so bookmark that site and use it!
sandv1 is offline  
post #3 of 27 (permalink) Old Sep 2nd, 2008, 07:48 AM
Senior Member
 
kittyking's Avatar
 
Join Date: Aug 2006
Location: New Zealand
Posts: 23,539
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by sandv1 View Post
Long-time prg student.... but only w/ 1.5 years working w/ C++...

From what I can tell, it looks like (with whatever you're doing) you'll need to use "pointer math" (use a search engine to research the term). More to the point, you have to _first_ establish the address of the parameter. Also, since your passing a const parameter, you will likely have to do some copying of the parameter in yr recursive function even if it's only to cycle through one char at a time.

If recursion is required, then pointer math plays into that. In other words, your rec. func. should be re-setting the pointer to current address plus one. You will likely have a flag in this func. to find the end of the term.

For example, say you're passing the string "ballbasher" ( :-p ), you are first going to find the starting point of where that specific char array is located in memory i.e. where the first 'b' is stored in memory. If the function reaches the end of the term, then the flag should go "found it!" i.e. char == null character or '\0' ...or by however you are terminating the function. Otherwise, the rec. func. should keep going (and, in turn, cycling through the term one char at a time by increasing your variable address by one. Obviously, the function will call itself again if the term hasn't been exhausted yet.... or if the flag is missed!)

This should get you started at least...

By the way, websites such as cplusplus.com have good tutorials, so bookmark that site and use it!
Wtf language IS that!

Thank you for everything Mercedes, now please be nice to Socrates for me, perhaps fetch him a bird or two. We love you, always.
kittyking is offline  
post #4 of 27 (permalink) Old Sep 2nd, 2008, 08:12 AM
Senior Member
 
mandy7's Avatar
 
Join Date: Sep 2002
Posts: 12,263
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by kittyking View Post
Wtf language IS that!
I'm not sure, but it looks like fluent dork
mandy7 is offline  
post #5 of 27 (permalink) Old Sep 2nd, 2008, 10:55 AM Thread Starter
Kart & I against the WtaWorld!
 
Rocketta's Avatar
 
Join Date: Nov 2001
Location: NC
Posts: 35,922
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by sandv1 View Post
Long-time prg student.... but only w/ 1.5 years working w/ C++...

From what I can tell, it looks like (with whatever you're doing) you'll need to use "pointer math" (use a search engine to research the term). More to the point, you have to _first_ establish the address of the parameter. Also, since your passing a const parameter, you will likely have to do some copying of the parameter in yr recursive function even if it's only to cycle through one char at a time.

If recursion is required, then pointer math plays into that. In other words, your rec. func. should be re-setting the pointer to current address plus one. You will likely have a flag in this func. to find the end of the term.

For example, say you're passing the string "ballbasher" ( :-p ), you are first going to find the starting point of where that specific char array is located in memory i.e. where the first 'b' is stored in memory. If the function reaches the end of the term, then the flag should go "found it!" i.e. char == null character or '\0' ...or by however you are terminating the function. Otherwise, the rec. func. should keep going (and, in turn, cycling through the term one char at a time by increasing your variable address by one. Obviously, the function will call itself again if the term hasn't been exhausted yet.... or if the flag is missed!)

This should get you started at least...

By the way, websites such as cplusplus.com have good tutorials, so bookmark that site and use it!
Thanks, this does give me some things to look up.


You are correct in that we are to pass into the function a specific pointer in the array but we won't be doing it by actual character but by address, ie player[0], player[1], etc.

What I cant' figure out is how to code the counter in the recursion without initializing a variable over and over and over which of course gives me an infinite loop.

However, today is a new day and maybe it will look all different to me.
Rocketta is offline  
post #6 of 27 (permalink) Old Sep 2nd, 2008, 10:57 AM Thread Starter
Kart & I against the WtaWorld!
 
Rocketta's Avatar
 
Join Date: Nov 2001
Location: NC
Posts: 35,922
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by kittyking View Post
Wtf language IS that!
computer language
Rocketta is offline  
post #7 of 27 (permalink) Old Sep 2nd, 2008, 11:01 AM
The only girl in the world.
 
Kim's_fan_4ever's Avatar
 
Join Date: Jul 2006
Posts: 21,050
                     
Re: Any C++ programmers here?

I hate pointers Never really understood them.
Good luck with that Rocketta

Swiss champs.
Kim's_fan_4ever is offline  
post #8 of 27 (permalink) Old Sep 2nd, 2008, 11:18 AM Thread Starter
Kart & I against the WtaWorld!
 
Rocketta's Avatar
 
Join Date: Nov 2001
Location: NC
Posts: 35,922
                     
Re: Any C++ programmers here?

Thanks.
Rocketta is offline  
post #9 of 27 (permalink) Old Sep 3rd, 2008, 04:54 AM
country flag ys
Adrenaline junkie
 
ys's Avatar
 
Join Date: Sep 2001
Location: always on the move
Posts: 22,384
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by Rocketta View Post
I often have problems with the most simple code. I have to write a code that determines a strings link and use recursion in it? They only let you pass the array of characters as a constant to the function. Help, I'm stumped over this easy little program.
C++ is my one of my primary languages.. But I am not sure I understand the problem.. What is "a strings link"? Could you elaborate?

"..just knowing that as long as i choose life, there is hope."

Je ne suis pas charlie

PLAY TENNIS!

No more obama, la-la-la-la-la-la
ys is offline  
post #10 of 27 (permalink) Old Sep 3rd, 2008, 10:17 AM
Senior Member
 
kittyking's Avatar
 
Join Date: Aug 2006
Location: New Zealand
Posts: 23,539
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by Rocketta View Post
computer language
k swt thnx i onli undrstnd englsh nd a wddle bt ov mri

Thank you for everything Mercedes, now please be nice to Socrates for me, perhaps fetch him a bird or two. We love you, always.
kittyking is offline  
post #11 of 27 (permalink) Old Sep 3rd, 2008, 01:45 PM Thread Starter
Kart & I against the WtaWorld!
 
Rocketta's Avatar
 
Join Date: Nov 2001
Location: NC
Posts: 35,922
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by ys View Post
C++ is my one of my primary languages.. But I am not sure I understand the problem.. What is "a strings link"? Could you elaborate?
I meant a strings length.
Rocketta is offline  
post #12 of 27 (permalink) Old Sep 3rd, 2008, 02:17 PM
Senior Member
 
Join Date: Jul 2007
Posts: 6,219
                     
Re: Any C++ programmers here?

from high school computer science class if i remember correctly
9th grade i think
only had high school training in C not C++
you have to set a pointer to the string
char *c = "String";
int length = count(c);

the above two lines should be in the void main() function
however the length count function should look something like this
int count(char *str)
{
if(*str=='\0')
return 0;
else
{str++;
return (1 + count(str));}
}

i am not sure of the language syntax
it was in high school that i last did programming

If you're not paying for something, then you're the product.

Last edited by Expat; Sep 3rd, 2008 at 04:19 PM.
Expat is offline  
post #13 of 27 (permalink) Old Sep 3rd, 2008, 02:40 PM
country flag ys
Adrenaline junkie
 
ys's Avatar
 
Join Date: Sep 2001
Location: always on the move
Posts: 22,384
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by lalit View Post
from high school computer science class if i remember correctly
9th grade i think
only had high school training in C not C++
you have to set a pointer to the string
char *c = "String";
int length = count(c);

the above two lines should be in the void main() function
however the length count function should look something like this
int count(char *str)
{
if(*str='\0')
return 0;
else
{str++;
return (1 + count(str));}
}

i am not sure of the language syntax
it was in high school that i last did programming
I think that's definitely good enough then.. You can make it few characters shorter though

int count(char *str)
{
return (*str) ?
(1 + count(++str)) : 0;
}

"..just knowing that as long as i choose life, there is hope."

Je ne suis pas charlie

PLAY TENNIS!

No more obama, la-la-la-la-la-la
ys is offline  
post #14 of 27 (permalink) Old Sep 3rd, 2008, 11:34 PM
She's coming for your faves wigs
 
Aaron.'s Avatar
 
Join Date: May 2006
Location: Retired
Posts: 17,710
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by mandy7 View Post
I'm not sure, but it looks like fluent dork
Im sure a fluent dork has a better career then your ass

Quote:
Originally Posted by JUSTICE4ALL View Post
You shut the hell up, you don't know me.! KING, Bow down bitch cause i'm the shit! HOE! I know you see that big ass patch!
Aaron. is offline  
post #15 of 27 (permalink) Old Sep 4th, 2008, 05:54 AM
Senior Member
 
mandy7's Avatar
 
Join Date: Sep 2002
Posts: 12,263
                     
Re: Any C++ programmers here?

Quote:
Originally Posted by Aaron. View Post
Im sure a fluent dork has a better career then your ass
But he still won't make as much money as YOU get for starring in gay porn
mandy7 is offline  
Reply

Quick Reply
Message:
Options

Register Now



In order to be able to post messages on the TennisForum.com forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.

User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.

Password:


Confirm Password:
Email Address
Please enter a valid email address for yourself.

Email Address:
OR

Log-in









Human Verification

In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.



Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page



Posting Rules  
You may not post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

 
For the best viewing experience please update your browser to Google Chrome