PDA

View Full Version : Any C++ programmers here?


Rocketta
Sep 2nd, 2008, 01:03 AM
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. :banghead:

sandv1
Sep 2nd, 2008, 08:32 AM
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!

kittyking
Sep 2nd, 2008, 08:48 AM
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!

:eek: Wtf language IS that!

mandy7
Sep 2nd, 2008, 09:12 AM
:eek: Wtf language IS that!
I'm not sure, but it looks like fluent dork

Rocketta
Sep 2nd, 2008, 11:55 AM
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. :wavey:


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
Sep 2nd, 2008, 11:57 AM
:eek: Wtf language IS that!

computer language

Kim's_fan_4ever
Sep 2nd, 2008, 12:01 PM
I hate pointers :sobbing: Never really understood them.
Good luck with that Rocketta :wavey:

Rocketta
Sep 2nd, 2008, 12:18 PM
Thanks. :lol:

ys
Sep 3rd, 2008, 05:54 AM
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. :banghead:

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?

kittyking
Sep 3rd, 2008, 11:17 AM
computer language

k swt thnx i onli undrstnd englsh nd a wddle bt ov mri

Rocketta
Sep 3rd, 2008, 02:45 PM
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.

Expat
Sep 3rd, 2008, 03:17 PM
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

ys
Sep 3rd, 2008, 03:40 PM
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;
}

Aaron.
Sep 4th, 2008, 12:34 AM
I'm not sure, but it looks like fluent dork Im sure a fluent dork has a better career then your ass :kiss:

mandy7
Sep 4th, 2008, 06:54 AM
Im sure a fluent dork has a better career then your ass :kiss:
But he still won't make as much money as YOU get for starring in gay porn :yeah:

Aaron.
Sep 4th, 2008, 10:59 AM
But he still won't make as much money as YOU get for starring in gay porn :yeah: Wont happen

kittyking
Sep 4th, 2008, 11:42 AM
Wont happen

Well I could cover your face in something so no one would recognize you ;)

Meteor Shower
Sep 4th, 2008, 11:45 AM
After just doing a stupid test in economy suddently seeing programming is not so bad.
What code languages do you guys know?

Rocketta
Sep 4th, 2008, 06:37 PM
Thanks for everyone's help. I had a major mental block with this I don't know why?? On to the next project. ;)

Rocketta
Sep 4th, 2008, 06:39 PM
After just doing a stupid test in economy suddently seeing programming is not so bad.
What code languages do you guys know?

I only know a little c++, some Java but really can't remember much of it.

Fingon
Sep 5th, 2008, 02:50 AM
After just doing a stupid test in economy suddently seeing programming is not so bad.
What code languages do you guys know?

I used to know COBOL, C and C++, and some VB.

but since I started with the .net framework and focused on C#.

Aside from a little javascript from time to time I only do C# now.

Fingon
Sep 5th, 2008, 02:54 AM
Thanks for everyone's help. I had a major mental block with this I don't know why?? On to the next project. ;)

I could have helped if you asked 5 years ago, but I have forgotten a lot about C++.

With C# I don't need to bother with pointers, unless it's a case of real critical performance which usually I don't have.

string manipulation is so much easier, just get the StringBuilder class.

can use regular expressions to search for patterns, much easier and safe than going character by character.

No memory leaks (unless you are calling unmanaged code).

My life it's a lot easier, and I always remember some C++ programmers that constantly would say that the compiler is not your mother and doesn't have to tell you everything.

The C# compiler is a very strict mother and trust me, saves me a lot of headaches.

Why are you on C++ and not something like java or .net?

Rocketta
Sep 5th, 2008, 04:38 AM
I could have helped if you asked 5 years ago, but I have forgotten a lot about C++.

With C# I don't need to bother with pointers, unless it's a case of real critical performance which usually I don't have.

string manipulation is so much easier, just get the StringBuilder class.

can use regular expressions to search for patterns, much easier and safe than going character by character.

No memory leaks (unless you are calling unmanaged code).

My life it's a lot easier, and I always remember some C++ programmers that constantly would say that the compiler is not your mother and doesn't have to tell you everything.

The C# compiler is a very strict mother and trust me, saves me a lot of headaches.

Why are you on C++ and not something like java or .net?

the computer certificate program at the school I go to does c++ and object-oriented programming. It's part of the engineering program and I don't know if that has something to do with it? but after you go through the program you are eligible to apply to grad school. :shrug:

Meteor Shower
Sep 5th, 2008, 12:13 PM
I could have helped if you asked 5 years ago, but I have forgotten a lot about C++.

With C# I don't need to bother with pointers, unless it's a case of real critical performance which usually I don't have.

string manipulation is so much easier, just get the StringBuilder class.

can use regular expressions to search for patterns, much easier and safe than going character by character.

No memory leaks (unless you are calling unmanaged code).

My life it's a lot easier, and I always remember some C++ programmers that constantly would say that the compiler is not your mother and doesn't have to tell you everything.

The C# compiler is a very strict mother and trust me, saves me a lot of headaches.

Why are you on C++ and not something like java or .net?

Thanks for the info.

I was thinking about self-studying C# because I heard it goes in the industry right now, but i heard the transition takes much time even if you know C and C++, which I know both pretty well (no i'm not a CS student :o )

ys
Sep 5th, 2008, 02:09 PM
Thanks for the info.

I was thinking about self-studying C# because I heard it goes in the industry right now

Depends on an industry.. Telecom, finance, all performance-critical stuff will stay C/C++.. unless something comparable performance-wise comes along.. Leave alone the fact that most of Server installations are still UNIX/LINUX, and they don't run C# AFAIK.. I do not see , conceptually, a major difference between C# and Java. No matter how you slice or dice it, they are essentially still interpreters, not compilers..

Meteor Shower
Sep 5th, 2008, 04:34 PM
I didn't know that C# doesn't run on UNIX/LINUX :eek:
Very strange, I know a guy who works at a high tech company and he works with C#, so its definitely sounds weird that he doesn't work with unix or linux. I'm not sure what his role exactly is though so who knows.

Paco
Sep 5th, 2008, 07:14 PM
After just doing a stupid test in economy suddently seeing programming is not so bad.
What code languages do you guys know?

Started with Pascal (or how it was called :p) but wasn't the big fan of it
Then I learnt C++ which was pretty interesting but I haven't use it for very long time
Next was Java, about which I don't know what to say. I mean some options are nice but I don't know how to explain it
Then turned into assembler :angel: and men that's cool :lol:
I've learnt a bit about C# but actually never ever used it :o
If we count HTML, JavaScript, xml as a real language, then I know them too ;)

On the other hand I'm not programming that much and never was good on it :o

On previous semester I was programming PLC's. And now I should learn more about it (because of practise job I had during holidays)
Next semester I'm going to program microcontollers (both assembler and C++)