 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Heather Guest
|
Posted: Mon Nov 15, 2004 11:39 am Post subject: Need some pointers |
|
|
I am not asking anyone to write my program for me. I want to get that out
upfront. I just desperately need some pointers.
C++ is the first language I have ever taken a class in. Up until now I have
grasped things rather easily. This time I haven't. Whether it is the concept
itself or just trouble concentrating (had a tooth cut out, reacted to the
numbing medication, yada yada..been knocked on my butt by medication) I
don't know. This is what I was assigned: "Read in a string of maximum 11
characters (including the at the end) and whatever is typed in, reverse
it using a function of your own making".
I know I need to find the and go backwards from there...I just can not
grasp how to do it. I have tried looking online using google, but I am not
finding anything that doesn't rely on an outside function. Can someone
please help? I don't usually wait until last minute to do programs, but the
medication they gave me to counteract the reaction to the numbing medication
has kept me knocked out most of the time since Thursday and I just am
totally lost and need some pointers.
Thanks
Heather
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Mang Guest
|
Posted: Mon Nov 15, 2004 9:37 pm Post subject: Re: Need some pointers |
|
|
"Heather" <hlc101202 (AT) mchsi (DOT) com> schrieb im Newsbeitrag
news:0WSld.338164$wV.50567 (AT) attbi_s54 (DOT) ..
| Quote: | I am not asking anyone to write my program for me. I want to get that out
upfront. I just desperately need some pointers.
C++ is the first language I have ever taken a class in. Up until now I
have
grasped things rather easily.
|
So you are either a genious or you haven't learnt that much about C++ :-)
This time I haven't. Whether it is the concept
| Quote: | itself or just trouble concentrating (had a tooth cut out, reacted to the
numbing medication, yada yada..been knocked on my butt by medication) I
don't know. This is what I was assigned: "Read in a string of maximum 11
characters (including the at the end) and whatever is typed in, reverse
it using a function of your own making".
I know I need to find the and go backwards from there...I just can not
grasp how to do it. I have tried looking online using google, but I am not
finding anything that doesn't rely on an outside function. Can someone
please help? I don't usually wait until last minute to do programs, but
the
medication they gave me to counteract the reaction to the numbing
medication
has kept me knocked out most of the time since Thursday and I just am
totally lost and need some pointers.
|
These terms should help you to build various solutions to your problem:
std::strlen
loops (for, while)
std::string (this class is so heavy; take a close look at everything dealing
with reverse_iterator [constructors, member functions returning them]).
std::reverse
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Mon Nov 15, 2004 9:38 pm Post subject: Re: Need some pointers |
|
|
"Heather" <hlc101202 (AT) mchsi (DOT) com> wrote
| Quote: | I am not asking anyone to write my program for me. I want to get that out
upfront. I just desperately need some pointers.
....
don't know. This is what I was assigned: "Read in a string of maximum 11
characters (including the at the end) and whatever is typed in, reverse
it using a function of your own making".
You could look for inspiration at the implementation of the std::reverse |
function of the standard library implementation you use (header
<algorithm>).
| Quote: | I know I need to find the and go backwards from there...I just can not
grasp how to do it. I have tried looking online using google, but I am not
finding anything that doesn't rely on an outside function.
You swap the first and last character, and go on that way. |
I think you should just start implementing this and see...
Of course, clc++.*moderated* is not a good place to get urgent replies
(since every message posted is first reviewed by a moderator).
Good luck ;)
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
R.F. Pels Guest
|
Posted: Mon Nov 15, 2004 11:17 pm Post subject: Re: Need some pointers |
|
|
Heather wrote:
| Quote: | I know I need to find the and go backwards from there...I just can not
|
Use a stack.
--
Ruurd
..o.
...o
ooo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
M Jared Finder Guest
|
Posted: Tue Nov 16, 2004 11:31 am Post subject: Re: Need some pointers |
|
|
Heather wrote:
| Quote: | I am not asking anyone to write my program for me. I want to get that out
upfront. I just desperately need some pointers.
C++ is the first language I have ever taken a class in. Up until now I have
grasped things rather easily. This time I haven't. Whether it is the concept
itself or just trouble concentrating (had a tooth cut out, reacted to the
numbing medication, yada yada..been knocked on my butt by medication) I
don't know. This is what I was assigned: "Read in a string of maximum 11
characters (including the at the end) and whatever is typed in, reverse
it using a function of your own making".
I know I need to find the and go backwards from there...I just can not
grasp how to do it. I have tried looking online using google, but I am not
finding anything that doesn't rely on an outside function. Can someone
please help? I don't usually wait until last minute to do programs, but the
medication they gave me to counteract the reaction to the numbing medication
has kept me knocked out most of the time since Thursday and I just am
totally lost and need some pointers.
|
It's perfectly okay to use outside functions, as long as you also
implement those functions. You may have seen a solution along the lines
of this:
void reverse_string( char* string ) {
reverse( string, string + strlen( string ));
}
reverse and strlen are both standard library functions. If you can
implement a my_reverse and my_strlen, then you can write your solution
using those instead of the standard ones. To implement them, you'll
need to use looping and feel somewhat comfortable with pointers.
-- MJF
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel Salters Guest
|
Posted: Wed Nov 17, 2004 2:12 am Post subject: Re: Need some pointers |
|
|
"Heather" <hlc101202 (AT) mchsi (DOT) com> wrote
| Quote: | I am not asking anyone to write my program for me. I want to get that out
upfront. I just desperately need some pointers.
C++ is the first language I have ever taken a class in. Up until now I have
grasped things rather easily. This time I haven't. Whether it is the concept
itself or just trouble concentrating (had a tooth cut out, reacted to the
numbing medication, yada yada..been knocked on my butt by medication) I
don't know. This is what I was assigned: "Read in a string of maximum 11
characters (including the at the end) and whatever is typed in, reverse
it using a function of your own making".
|
That question is a giveaway. You have a bad C++ class. Usually, we don't
bother with or maximum string lengths. It's only when you have to
interface with C code, that you need to use char const* instead of
std::string.
| Quote: | I know I need to find the and go backwards from there...I just can not
grasp how to do it. I have tried looking online using google, but I am not
finding anything that doesn't rely on an outside function.
|
Can you create a pointer to the ' ' character, given a char const* to the
first char? That is really easy; just look at the first char (*pointer).
If that is ==' ', you found it. If not, move the pointer to the next char
(pointer++) until you have the ' '.
Now, go back one (pointer--) and you have the last character.
Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Thu Nov 18, 2004 12:27 am Post subject: Re: Need some pointers |
|
|
| Quote: | Heather wrote:
I am not asking anyone to write my program for me. I want to get that out
upfront. I just desperately need some pointers.
|
M Jared Finder <mfinder (AT) digipen (DOT) edu> wrote
| Quote: | It's perfectly okay to use outside functions, as long as you also
implement those functions.
|
Not neccesarily. If this is an instructor-led class, it may depend on
the instructor and/or on the purpose of the lesson. If the purpose is
to get some experience with pointer variables, the instructor might be
asking for students to do it all in one function without using any
"outside functions" at all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Thu Nov 18, 2004 12:41 am Post subject: Re: Need some pointers |
|
|
| Quote: | "Heather" <hlc101202 (AT) mchsi (DOT) com> schrieb
C++ is the first language I have ever taken a class in. Up until now I
have grasped things rather easily.
|
"Thomas Mang" <nospam (AT) nospam (DOT) invalid> wrote
| Quote: | So you are either a genious or you haven't learnt that much about C++
|
Not sure it needs to be either one of those extremes.
| Quote: | This is what I was assigned: "Read in a string of maximum 11
characters (including the at the end) and whatever is typed in, reverse
it using a function of your own making".
|
To find the , use strlen to find the length of the string, and then use
pointer arithmetic: the expression
p + std::strlen(p)
will give you a pointer to the .
I notice that most of the answers you've gotten so far have focused on
how to reverse the string in memory. If the above quote is accurate, this
might be more than you need. When the assignment says to "reverse it"
this could be interpreted as "print it out in reverse." If so, your task
can be made much simpler: print out the last character (NOT including the
), then the previous one, and so on until you've printed out the very
first character. You could do this by using a for loop, where you start
out by initializing a pointer q to the character, and you continue
while q>p. Remember that q needs to move backwards instead of forwards.
Good luck.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
M Jared Finder Guest
|
Posted: Sat Nov 20, 2004 4:20 pm Post subject: Re: Need some pointers |
|
|
Allan W wrote:
| Quote: | Heather wrote:
I am not asking anyone to write my program for me. I want to get that out
upfront. I just desperately need some pointers.
M Jared Finder <mfinder (AT) digipen (DOT) edu> wrote
It's perfectly okay to use outside functions, as long as you also
implement those functions.
Not neccesarily. If this is an instructor-led class, it may depend on
the instructor and/or on the purpose of the lesson. If the purpose is
to get some experience with pointer variables, the instructor might be
asking for students to do it all in one function without using any
"outside functions" at all.
|
I've had classes like that; they were all awful because they encouraged
the use of bad habits. If you want to get students familiar with
pointers, give an assignment like "reverse a singlely linked list",
which can't be done with out pointers. But don't impose an artificial
restriction that would cause students to learn bad style.
-- MJF
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gary Labowitz Guest
|
Posted: Sun Nov 21, 2004 4:59 am Post subject: Re: Need some pointers |
|
|
"M Jared Finder" <mfinder (AT) digipen (DOT) edu> wrote
<
| Quote: | I've had classes like that; they were all awful because they encouraged
the use of bad habits. If you want to get students familiar with
pointers, give an assignment like "reverse a singlely linked list",
which can't be done with out pointers. But don't impose an artificial
restriction that would cause students to learn bad style.
|
Good idea in theory, very hard in practice. The assignment you suggest (good
one!) involves the student learning what a linked list is. They have enough
trouble remembering what a variable is. It would take a week to study linked
list enough to understand what reversing a singlely linked list entails.
It's hard enough just getting the idea of passing a pointer to a function,
which is a more useful idea to conquer. (Of course I teach them pass by
reference first, and remind them to use it whenever possible.)
--
Gary
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Mon Nov 22, 2004 11:06 pm Post subject: Re: Need some pointers |
|
|
| Quote: | M Jared Finder <mfinder (AT) digipen (DOT) edu> wrote
It's perfectly okay to use outside functions, as long as you also
implement those functions.
Allan W wrote:
Not neccesarily. If this is an instructor-led class, it may depend on
the instructor and/or on the purpose of the lesson. If the purpose is
to get some experience with pointer variables, the instructor might be
asking for students to do it all in one function without using any
"outside functions" at all.
|
M Jared Finder <mfinder (AT) digipen (DOT) edu> wrote
| Quote: | I've had classes like that; they were all awful because they encouraged
the use of bad habits.
|
Which bad habits are you referring to?
I think it's very instructive to ask students to write their own version
of std::strcpy(). That doesn't mean that I would advise them not to use
the library version... as a matter of fact, it's a good opportunity to
lecture on the difference between system-dependant code (often written
in assembly language) and portable code, and when each one is appropriate.
| Quote: | If you want to get students familiar with
pointers, give an assignment like "reverse a singlely linked list",
which can't be done with out pointers. But don't impose an artificial
restriction that would cause students to learn bad style.
|
Still not sure what "bad style" you're talking about. Perhaps you mean,
"use library code whenever it is helpful?" But you can't introduce the
entire standard library at once.
Also, I agree with Gary Labowitz -- talking about a linked list is
rather an advanced topic. As far as beginners are concerned, the sole
purpose of a pointer is to support "new T" -- and if my school allowed
it, I would talk about smart pointers in the very next lesson. (Alas,
I've got quite a few topics to talk about in an 8-week class, but smart
pointers are not on that list. I do encourage students to look it up
on their own after class has ended.)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
M Jared Finder Guest
|
Posted: Sun Nov 28, 2004 11:38 am Post subject: Re: Need some pointers |
|
|
Allan W wrote:
| Quote: | Allan W wrote:
Not neccesarily. If this is an instructor-led class, it may depend on
the instructor and/or on the purpose of the lesson. If the purpose is
to get some experience with pointer variables, the instructor might be
asking for students to do it all in one function without using any
"outside functions" at all.
M Jared Finder <mfinder (AT) digipen (DOT) edu> wrote
I've had classes like that; they were all awful because they encouraged
the use of bad habits.
Which bad habits are you referring to?
|
Putting way too much code into one function. I have seen way too many
functions that span two to three pages but could be reduced to a few
lines if common functionality was separated out.
| Quote: | I think it's very instructive to ask students to write their own version
of std::strcpy(). That doesn't mean that I would advise them not to use
the library version... as a matter of fact, it's a good opportunity to
lecture on the difference between system-dependant code (often written
in assembly language) and portable code, and when each one is appropriate.
|
I agree; in fact I think its important to get the students to think in
such a way that would get them to write those functions. The original
poster's question was how to reverse a C-string. By realizing you can
implement that as reverse(cstr, cstr + strlen(cstr)), a student starts
to practice and learn about abstraction, which I view as *the*
fundamental skill in computer programming.
-- MJF
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Comeau Guest
|
Posted: Sun Nov 28, 2004 11:53 pm Post subject: Re: Need some pointers |
|
|
In article <30sj4tF33aro8U1 (AT) uni-berlin (DOT) de>,
M Jared Finder <mfinder (AT) digipen (DOT) edu> wrote:
| Quote: | Allan W wrote:
I've had classes like that; they were all awful because they encouraged
the use of bad habits.
Which bad habits are you referring to?
Putting way too much code into one function. I have seen way too many
functions that span two to three pages but could be reduced to a few
lines if common functionality was separated out.
I think it's very instructive to ask students to write their own version
of std::strcpy(). That doesn't mean that I would advise them not to use
the library version... as a matter of fact, it's a good opportunity to
lecture on the difference between system-dependant code (often written
in assembly language) and portable code, and when each one is appropriate.
I agree; in fact I think its important to get the students to think in
such a way that would get them to write those functions. The original
poster's question was how to reverse a C-string. By realizing you can
implement that as reverse(cstr, cstr + strlen(cstr)), a student starts
to practice and learn about abstraction, which I view as *the*
fundamental skill in computer programming.
|
I've historically taught C++ to C programmers. Early on I realized
that they thought C++ was going to give them abstraction, when
actually it is they who need to give it to themselves. So while
they are being teased with classes, I try to hit home that abstraction
has been in their ability/touch all along. One way to do that is
actually to have them design and write a reverse function.
Indeed, the example can cover so many fundamental aspects
to programming involving design, knowledge, creativity,
organization, documentation, commonality, seperation,
delegation, abstraction, making compromises, mainline code
vs library code, efficiency, etc. And what starts out as
"Why the hell are we doing this?" protests becomes a sense
of calmness as each absorbs the insights until eventually
they're blurting "Can we do another like this?" as many
of the students, who include existing programmers, have
never seen anything like that before or realized the numerous
abstraction mechanism availablr.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|