I want to re-arrange elements depending on input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ebasboy

    #1

    I want to re-arrange elements depending on input

    I am creating a program that should allow user to enter Top 20 hit songs.
    The user should enter song name and artist name in order of position.The output should be printed in table format and should be allow user to re-arrange songs later if she want to do so.
    The user has to enter song name and the new position she wants the song moved to (this means she chooses from the 20 filled positions).For example if the user want to take a song from position10 to position1, the song will make the song in position1 move one position down and the same to the rest of the song

    So the problem I have is how to make this work, i have a little knowldge in sorting arrays of which I think could help. Can Someone help me copmlete the following code:

    Code:
    //last part of the code
    //print data
     for int pos=0;pos<20;pos++
    cout<<setw(5)<<(pos+1)<<setw(15)<<song[pos]<<setw(15)<<artist[pos]<<endl;
    
    //here i need to tell the program to move songs one step up or down depending on user choice
    Last edited by Frinavale; Oct 29 '10, 03:37 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I see you are using cout so that means you are using C++.

    That being the case, I recommend you use the C++ Standard Library list<> template for your database. Use C++ string objects for your data fields.

    All of the moving around you need to do is already coded in this list<> template, which implements a double-linked list.

    To make things easy, declare a struct to contain your data items and make a lst of variables of that struct.

    When you sort, you can write a function that compares two of your struct variables and returns a true if the first argument is greater than the second aregument and false otherwise. Then you can use the C++ Standard Library sort<> template. You use iterators to the start and end of the list plus the address of your compare function. Voila! your list is sorted.

    Almost all C++ textbooks have examples of how to do exactly this.

    Comment

    Working...