Thread: Homework Help

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    Homework Help

    I have a homework assignment I am having trouble with. The instructions are:

    Implement the function maxLoc(), which return an iterator pointing at the largest element in a list.

    // return an iterator pointing to the largest element
    // in the list.
    templete <typename T>
    list<T>::iterator maxLoc(list<T>& aList);
    Write a program that tests maxLoc(), using the following declarations:
    string strArr[] = {"insert", "erase", "templete", "list"};
    int strSize = sizeof(strArr) / sizeof(string);
    list<string> strList(strArr, strArr+strSize);
    The program should repeatedly call maxLoc(),
    output the largest value, and then delete the value, until the list is empty.


    Now my program executes fine. The problem is when it gets to the line to erase the elements at maxLoc, it is erasing the first element in the list instead. I cannot figure out why it is doing this because there is nothing in there re-assigning maxLoc to the beginning of the list.

    What am I doing wrong? Can I not use aList.erase(maxLoc) to erase teh element at *maxLoc? I tried useing aList.erase(iter) but that gave me sevaral errors. I just can't see how maxLoc is reassigning to the first element in the list.

    Code:
    #include <iostream>
    #include <list>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    template <typename T>
    list<string>::iterator maxLoc(list<string>& aList);
    
    int main()
    {		
    		string strArr[] = {"insert","erase","template","list"};
    		int strSize = sizeof(strArr)/sizeof(string);
    		list<string> aList(strArr, strArr + strSize);
    		list<string>::iterator iter = aList.begin();
    		list<string>::iterator maxLoc=aList.begin();
    
    
    		//validate that the list is not empty.  Run loop until aList.empty() = true
    		while (!aList.empty())
    		{
    		cout << "The list contains the elements " << endl;
    
    			//loop reads through the list and sets the maxLoc to equal iter if iter is greater than the current maxLoc
    			while (iter!=aList.end())
    			{
    				
    				cout << *iter << endl;
    				if (*iter > *maxLoc)
    					*maxLoc = *iter;
    				++iter;			
    			}//end loop
    		
    			//Output the max element that was found
    			cout << "Max Element is " << *maxLoc << endl << endl;
    
    			//erase the element found at maxLoc from teh list
    			cout << "Remove " << *maxLoc << " from the list." << endl << endl;
    			aList.erase(maxLoc);
    			
    
    			//reset the iter and maxLoc pointers
    			iter = aList.begin();
    			maxLoc = aList.begin();
    			
    			cin.get();
    		}//end loop
    
    	cout << "The List is Empty." << endl;
    	cout << "Press Enter to Continue";
    	cin.get();
    	return 0;
    	
    }//end main()

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> *maxLoc = *iter;

    On that line you are assigning the value held by iter to the position pointed to by maxLoc. You should be assigning the actual iterator instead.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    ::bangs head on desk::

    I have fought over this for three days. That line is actually what my instructor sent me so I never questioned that line.

    It worked...sort of. I changed it to

    maxLoc = iter;

    but now it is not calulating maxLoc correctly. On the next run, it says list is the max but shouldn't it be insert? I thought it was judging max by how large (or how many characters) the string was but is it doing it by ASCII value? What do you think?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Why on earth would you name a variable the same name as a function? To confuse anyone who reads your code? (-20 pts.)

    2) You start with the maxloc iterator pointing to the first element:
    Code:
    list<string>::iterator maxLoc=aList.begin();
    and you never move it, so it will always point to the first element in the list. (-20 pts)

    3) This is not what you want to do:
    Code:
    *maxLoc = *iter;
    That copies the value that iter points to into the location that maxLoc points to. Your instructions do not say to do that. (-20 pts)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I thought it was judging max by how large (or how many characters) the string was but is it doing it by ASCII value?

    Yes, it is doing it by ASCII value (the normal way to sort strings, so "list" is greater than "insert" ('i' < 'l'). It's working.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    1) Because that is what my instructor specifically asked for.
    2) Is this wrong? I wanted maxLoc to originally equal the first element so that when iter runs through the list, it has something concrete to compare to.
    3) I already fixed that, as stated in my previous post. That was actually the issue keeping my program from executing properly.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Thanks, Daved. I really appreciate it.

    Yes, it is doing it by ASCII value (the normal way to sort strings, so "list" is greater than "insert" ('i' < 'l'). It's working.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't forget that you still need to actually move the working code into the function that your assignment calls for.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I thought it was judging max by how large (or how many characters) the string was but is it doing it by ASCII value
    Strings are compared char by char using the characters' ascii codes. The comparison starts with the first char of each string. If both chars are equal, then the comparison proceeds to the next char, and so on until there are two differing chars. If all the chars are equal and the end of one string is reached, then the shorter string will be less than the longer string.
    Last edited by 7stud; 12-04-2005 at 12:53 AM.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Quote Originally Posted by Daved
    Don't forget that you still need to actually move the working code into the function that your assignment calls for.
    Thanks, Daved. I am trying to figure that out now. I am not very good at functions so will probably post for more help later if I can't work it out.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Because that is what my instructor specifically asked for.
    No teacher would tell you to duplicate names in your program.

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    I now have two issues. I do not have a very good understanding of tmplate based functions (we really have spent no time on them). It has also been a long time since I had to deal with reference passing. I created the function below the code but cannot seem to be able to get it to call correctly. For all I know I could be WAY off course. How far off am I? I am getting the error:

    error C2783: 'std::list<_Ty>::iterator maxLoc(std::list<_Ty> &)' : could not deduce template argument for 'T'
    with
    [
    _Ty=std::string
    ]


    Code:
    #include <iostream>
    #include <list>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    template <typename T>
    list<string>::iterator maxLoc(list<string>& aList);
    
    int main()
    {		
    		string strArr[] = {"insert","erase","template","list"};
    		int strSize = sizeof(strArr)/sizeof(string);
    		list<string> aList(strArr, strArr + strSize);
    		list<string>::iterator iter = aList.begin();
    		list<string>::iterator max=aList.begin();
    
    
    		//validate that the list is not empty.  Run loop until aList.empty() = true
    		while (!aList.empty())
    		{
    			cout << "The list contains the elements " << endl;
    
    			//call teh maxLoc function and pass max into main()
    			maxLoc(max);
    
    			//Output the max element that was found
    			cout << "The Max Element is " << *max << endl << endl;
    
    			//erase the element found at maxLoc from teh list
    			cout << "Press Enter to remove " << *max << " from the list and continue." << endl << endl;
    			aList.erase(max);
    			
    
    			//reset the iter and maxLoc pointers
    			iter = aList.begin();
    			max = aList.begin();
    			
    			cin.get();
    		}//end loop
    
    	cout << "The List is Empty." << endl;
    	cout << "Press Enter to Continue";
    	cin.get();
    	return 0;
    	
    }//end main()	
    
    template <typename T>
    T maxLoc(list<string>&)
    {
    	//loop reads through the list and sets the maxLoc to equal iter if iter is greater than the current maxLoc
    			while (iter!=aList.end())
    			{
    				
    				cout << *iter << endl;
    				if (*iter > *max)
    					max = iter;
    				++iter;			
    			}//end loop
    			return max;
    }//end maxLoc()

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Quote Originally Posted by 7stud
    No teacher would tell you to duplicate names in your program.
    I'm sorry, but he did.

    I see what you are saying though and what kind of problems will arise. I changed the variable to max instead.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Look at your assignment instructions and match your function prototype and definition to the prototype in the assignment. Remember that the function is supposed to be generic for a list of any type of object, not just strings, so in the function don't refer to strings, refer to T.

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Quote Originally Posted by Daved
    Look at your assignment instructions and match your function prototype and definition to the prototype in the assignment. Remember that the function is supposed to be generic for a list of any type of object, not just strings, so in the function don't refer to strings, refer to T.
    OK, now I have

    Code:
    template <typename T>
    list<T>::iterator maxLoc(list<T>& aList);
    {
    	//loop reads through the list and sets the maxLoc to equal iter if iter is greater than the current maxLoc
    			while (iter!=aList.end())
    			{
    				
    				cout << *iter << endl;
    				if (*iter > *max)
    					max = iter;
    				++iter;			
    			}//end loop
    			return max;
    }//end maxLoc()
    for my function but what should be in the global declaration area? I have:
    Code:
    template <typename T>
    list<string>::iterator maxLoc(list<string>& aList);
    int main()
    but get numerous errors, beginning with 'T' : undeclared identifier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM