Thread: C++ beginner question

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    Question C++ beginner question

    Hi,

    I am a beginner. I'm working from a book called "The complete idiot's guide to c++". And I feel like an idiot, because I can't figure out the following.

    The question in the book is from the Polymorphism chapter 22. Specifically, from a STACK to a LIST.
    The part I am struggling with is:
    I need to ask the user for them to select an element (not an offset) from an array & then remove that element from the array. Then the higher-placed elements will be shuffled down one place to fill the empty element. The elements below will remain unchanged. I think I have the part where I ask the user to select the element, but I'm struggling to figure out how to then remove that element & shuffle the higher-placed elements down one & leave the other elements untouched.
    Can someone please help?

    I hope I provided enough details!
    Margie

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, do you have a stack, an array, or a list? You shouldn't have a stack, because you can't access elements in the middle of a stack. Deleting from an array involves moving all the other elements down one spot (so you've vacated spot 8; now you have to put element 9 in spot 8, element 10 in spot 9, element 11 in spot 10) etc. Removing from a list just involves moving some pointers around.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    I have a LIST. I started like this, but I'm just struggling so badly with the removing of the element from the LIST & then shuffling!

    for (index = 1; index < back; index++)
    {
    cout << "Enter the LIST element you wish to view ";
    cin >> data[index];

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Sorry I made a mistake there, it should say, "Enter the list element you wish to remove".

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you have a list, then there is no shuffling that is required -- you'll just set the pointer of n-1 to point to n+1 instead, and that takes element n right out of your loop.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    If you are talking about shuffling the results down, you do not have a list, you have an array. If you are using C++ you should actually use the list container in the STL. Often times when beginning programming it is difficult to get the answers you want because of misusing words to describe problems. May I suggest you post a little more code to demonstrate what you are doing and where in your code you are stumped. Specifically, where you define your 'list'.

    Also here is a link to some good book recommendations. As a general rule, stay away from those 'for dummies' books. In my experience they were written by dummies.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Arrays start at zero, not one.
    You need to start by thinking about the steps involved in removing an item.

    If you had a row of 5 jellybeans and you wanted to remove the middle jellybean, and get rid of the gap, what steps are involved?
    Note that you are only ever allowed to touch one jellybean at a time and jellybeans are not allowed to touch each other.
    You start by eating the middle one, and then what...?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AndrewHunter
    If you are talking about shuffling the results down, you do not have a list, you have an array.
    Actually, it sounds like margiesam has a list that is implemented with an array rather than a linked list.

    Quote Originally Posted by AndrewHunter
    If you are using C++ you should actually use the list container in the STL. Often times when beginning programming it is difficult to get the answers you want because of misusing words to describe problems.
    You, tabstop and the C++ standard misused "list" to always mean "linked list"
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner C Question Help
    By pmacdonald in forum C Programming
    Replies: 32
    Last Post: 06-22-2011, 09:32 PM
  2. Beginner's Question on QT
    By unix7777 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2008, 05:53 PM
  3. Beginner Question
    By TKnight in forum C Programming
    Replies: 3
    Last Post: 11-09-2008, 07:16 AM
  4. Beginner question - please help
    By mo34 in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2008, 02:34 PM
  5. Beginner Question
    By Chobo in forum C++ Programming
    Replies: 13
    Last Post: 01-23-2007, 11:16 PM

Tags for this Thread