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
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?
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
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.
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
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]; } }
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?
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 }
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?
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: };
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?
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