1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

help in writing a function to retrieve last item in an unsorted list

Discussion in 'C++' started by elsa87, Nov 17, 2008.

  1. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    hi everyone..
    please help me write a function which will return the last element in an unsorted list.
    the prototype of the function should be
    ItemType ReturnLastItem();

    we should assume that the list is not empty ans it shouldnt change after the program is executed..
    pleeeeeeeeease help me
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Can you define ItemType as well as how the list is passed to the function ReturnLastItem
     
  3. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Im sorry i dont know what you mean..
    all i know that i have an unsorted list and i should retrieve the last item in it..
    can u plz help?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    How do you pass that unsorted list to the function so that it can get the last element.
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Is the list held in a global variable?
    How is ItemType defined?
    Are you on a course?
     
  6. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    actually yes..i'm taking this course and i dont really feel like im learning anything in it..
    the whole question says that we should add a member function ReturnLastItem () to the UnsortedType ADT that returns the last element in the list. The function has the following prototype:
    ItemType ReturnLastItem();
    Preconditions: List is initialized and not empty.
    Postconditions: List is unchanged.
    this is all i know..and i have no idea how it should be done..
    we should point to the last item and then retrieve it but i dont know how to point to that item if i dont know the size of the list??
    plz help me out
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    I got it and its a member function but then giving you the code would not help but you should make a try and then its very straight forward.
     
  8. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    omg..plz help me out..after u give me the code i will be able to understand how it's done..but with nothing i will never understand a single thing
     
  9. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    void UnsortedType::RetrieveItem(ItemType& item, bool& found){
    	bool moreToSearch;
    	int location=0;
    	found = false;
    	moreToSearch = (location < length);
    	while (moreToSearch && !found)
    	{
    
                                found = true;
                    	item = info[location];
    
    			}
    }
    
    
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Don't be so feeble; have a go. You've been given the prototype so you should be able to get the first line (??? UnsortedType::???????) and the last statement in the function (a return statement).

    The code you've posted seems to have something missing - a mechanism to step to the next item in the list and an if statement that should go before "found=true". What is this code?

    On the line that sets moreToSearch, there is a reference to a variable called "length". You said you don't know the length of the list. So what is length, if it's not the length of the list?

    Is info[] where the individual entries in the list are stored?
     
  11. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    mmm ok..i'll have a second try
    Code:
    Void returnLastItem(ItemType& item)
    {
    int location=0;
    if(//the pointer in the last item is NULL then execute(i dont know how to write this))
    
    item = info[location];//i think this is to print
    }
    
     
  12. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    plz
    heeeeeeeeeeeeeeeeeeeeeeeeeeeelp
    im dying here..plz help
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    I think you're assuming this is harder than it really is.

    You've been given the prototype. Why have you changed the return type to Void? You can't return something from a void function.

    What is length? HINT - THIS IS A MAJOR CLUE
    What is info[]? HINT - THIS IS ANOTHER MAJOR CLUE

    What does the function have to return? Do you think it might have something to do with info[] and length? HINT - YES AND YES.

    Why do you think you need to test if the pointer in the last item is NULL?
     
  14. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    is it right now?
    Code:
    ReturnLastItem (item,info)
    {
    int location=MAX-1;
    return(info[location]);
    }
    
     
  15. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  16. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    thanks..put how can i compile it now?
     
  17. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    This function alone will not compile
     
  18. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    could u plz tell me what to write because i need to compile it..
    i think these go on top:
    Code:
    #include <iostream.h>
    #include <string.h>
    class unsorted list
    {
    private:
    	
    public:
    	
    };
    
    
     
  19. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Could you answer some questions please? Without the answers I have no way of saying whether you're right or not.

    1. Why have you changed the function from the prototype? Remember at the top you said "the prototype of the function should be ItemType ReturnLastItem();", but for some reason you've changed it to "ReturnLastItem (item,info)" (which is wrong on four counts: (a) there is no return type; (b) there is no type for item (and if this is a class member why do you want to pass it in as a new variable?); (c) there is no type for info (and if this is a class member etc); (d) it does not match the prototype you've been given.

    2. What is length?

    3. What is info[]?

    And now a new one from your latest code:

    4. What is MAX?

    In terms of the other code you should add, remember the statement you made earlier: "the whole question says that we should add a member function ReturnLastItem () to the UnsortedType ADT". So it's not what code you should wrap around ReturnLastItem() itself, but what code you should insert ReturnLastItem() into. Do you have a source/header file for UnsortedType?
     
  20. elsa87

    elsa87 New Member

    Joined:
    Oct 10, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Hi
    no i dont have a source file
    and im really so stressed out..i've been trying to solve this question for several days and the due date is tomorrw..
    the length is undetermined..i supposed it is an integer called max
    and item = info[location] is used to retrieve the item and save it in item..so when we get the cout we will say cout<<item;
    could u plz help me this time to get the result
     

Share This Page