mgrex's Profile User Rating: -----

Reputation: 3 Apprentice
Group:
Active Members
Active Posts:
182 (0.11 per day)
Joined:
25-March 10
Profile Views:
1,101
Last Active:
User is offline Nov 23 2012 10:29 PM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
0
Icon   mgrex has not set their status

Posts I've Made

  1. In Topic: An example of a Linked list program with multiple fields?

    Posted 30 Oct 2012

    I had some luck by using a template vector.

    Now I have logical error. It won't display the linked list's content in order. The statement on line 43 in the int main, month April), is always displayed before the initializations coming before it in lines 23-29.

    #include <iostream>
    #include <string>
    
    #include "Listing.h" 
    #include "Rainfall_stats.h" 
    using namespace std;
    
    int main()
    {
    	Listing<Rainfall_stats>list;
    
    	string monthNames[] = {"January","Febuary","March","April","May","June","July","August","September","October","November", "December"};
    	int numMonths;
    	double rainAmount;
    
    	cout << "How many months do you want to enter data for (1 thorugh 12): ";
    	cin >> numMonths;
    
    	
    	Rainfall_stats * temp = new Rainfall_stats[numMonths];
    
    	int i = 0;
    	for(i = 0; i < numMonths; i++)
    	{
    		cout << "Rainfall amount for " << monthNames[i] << ": ";
    		cin >> rainAmount;
    
    		temp[i] = Rainfall_stats(monthNames[i], rainAmount);
    		list.appendNode(temp[i]);
    	}
    
    	cout << "i amount: " << i;
    
    	// Display the values in the list. 
    	cout << "\nHere are the initial values:\n";
    	list.displayList(); 
    	cout << endl;
    
    	cout << "Adding another month's data...";
    	Rainfall_stats temp2(monthNames[i], 3);	// I had to overload > and < relational operators
    	list.insertNode(temp2);						// logical error, line 40 isn't placed last, in line 49's output.
    	
    	Rainfall_stats temp3(monthNames[i+1], 2);	// I had to overload > and < relational operators
    	list.insertNode(temp3);	
    
    	Rainfall_stats temp4(monthNames[i+2], 4);	// I had to overload > and < relational operators
    	list.insertNode(temp4);	
    
    	cout << "\nFull list again:\n";
    	list.displayList(); 
    	cout << endl;
    
    	delete [] temp;
    
    	system("pause");
    	return 0;
    }
    



    Rainfall_stats.h
    // Specification file for the NumberList class 
    #ifndef Rainfall_stats_H 
    #define Rainfall_stats_H
    //#include <cstdlib> 
    #include <iostream>		// for NULL
    #include <string>
    using namespace std;
    
    class Rainfall_stats 
    { 
    private: 
    	string month;
    	double amount;
    
    public: 
    	// Constructor: notice the constructor initializes the head pointer to NULL (0)
    	// This establishes an empty linked list. 
    	Rainfall_stats() 
    	{	month = "January";	
    		amount = 0;			} 
    
    	// Destructor: destroys  the list by deleting all its nodes. 
    	Rainfall_stats(string m, double a)
    	{	
    		month = m;
    		amount = a;
    	}
    
    	// Linked list operations 
    	// These functions are defined in NumberList.cpp.
    	void setMonth(string m)
    	{
    		month = m;
    	}
    
    	void setAmount(double a)
    	{
    		amount = a;
    	}
    
    	string getMonth() const
    	{
    		return month;
    	}
    
    	double getAmount() const
    	{
    		return amount;
    	}
    
    	bool operator > (const Rainfall_stats &right)	// Overloaded	> 
    	{  
    		bool status; 
    
    		if (month > right.month) 
    			status = true; 
    		//else if (month == right.month && amount > right.amount) 
    		//	status = true;
    		else 
    			status = false;
    		
    		return status; 
    	}
    	
    		
    	bool operator < (const Rainfall_stats &right)	// Overloaded < 
    	{  
    		bool status; 
    
    		if (month < right.month) 
    			status = true; 
    		//else if (month == right.month && amount < right.amount) 
    		//	status = true;
    		else 
    			status = false;
    		
    		return status; 
    	}
    
    
    	friend ostream &operator << (ostream &strm, const Rainfall_stats &obj) 
    	{ 
    		strm << "Month " << obj.month << ", had " << obj.amount << " inches of rainfall"; 
    		return strm; 
    	}
    }; 
    #endif 
    
    


    Listing.h
    // A class template for holding a linked list. 
    //The limitation of the NumberList class is that it can only hold double values.
    
    #ifndef LISTING_H 
    #define LISTING_H 
    #include <iostream> 	// For cout and NULL 
    using namespace std; 
    
    template <class T> 
    class Listing 
    { 
    private: 
    	// Declare a structure for the list. 
    	struct ListNode 
    	{ 
    		T value;					// The value in this node
    		struct ListNode *next;		// To point to the next node 
    	}; 
    
    	ListNode *head; // List head pointer 
    
    public: 
    	// Constructor : Renamed from NumberList to LinkedList
    	Listing() 
    		{	head = NULL;	} 
    
    	// Destructor : Renamed from NumberList to LinkedList
    	~Listing();
    	//{;}
    
    	// Linked list operations 
    	void appendNode(T); 
    	void insertNode(T); 
    	void deleteNode(T);
    	void displayList() const; 
    };
    
    // Note that the template uses the ==, !=, and < relational operators to compare node values, 
    // and it uses the << (line 93) operator with cout to display node values. Any type passed to  
    // the template must support these operators. 
    
    
    //********************************************************
    // appendNode appends a node containing the value 
    // passed into newValue, to the end of the list. 
    //******************************************************** 
    template <class T> 
    void Listing<T>::appendNode(T newValue) 
    { 
    	ListNode *newNode;		// To point to a new node 
    	ListNode *nodePtr;		// To move through the list 
    
    
    	// Allocate a new node and store nurn there. 
    	newNode = new ListNode; 
    	newNode->value = newValue;
    	newNode->next = NULL; 
    
    	// If there are no nodes in the list make newNode the 1st node. 
    	if(!head) 
    		head = newNode; 
    	// Otherwise, insert newNode at end.
    	else 
    	{ 
    		// Initialize nodePtr to head of list. 
    		nodePtr = head; 
    		
    		// Find the last node in the list. 
    		while (nodePtr->next) 
    			nodePtr = nodePtr->next; 
    		
    		// Insert newNode as the last node. 
    		nodePtr->next = newNode; 
    	}
    }
    
    //************************************************** 
    // displayList shows the value 
    // stored in each node of the linked list 
    // pointed to by head. 
    //************************************************** 
    template <class T> 
    void Listing<T>::displayList() const
    { 
    	// To move through the list
    	ListNode *nodePtr;
    	
    	// Position nodePtr at the head of the list. 
    	nodePtr = head;		
    
    	// While nodeptr points to a node, traverse the list. 
    	while (nodePtr)		
    	{ 
    		// Display the value in this node. 
    		cout << nodePtr->value << endl; 
    
    		// Move to the next node. 
    		nodePtr = nodePtr->next; 
    	}
    }
    
    //*****************************************************
    // The insertNode function inserts a node with 
    // newValue copied to its value member. 
    //*****************************************************
    template <class T> 
    void Listing<T>::insertNode(T newValue) 
    {
    	ListNode *newNode;				// A new node 
    	ListNode *nodePtr;				// To traverse the list 
    	ListNode *previousNode = NULL;	// The previous node 
    	
    	
    	// Allocate a new node and store num there. 
    	newNode = new ListNode; 
    	newNode->value = newValue; 
    	
    	// If there are no nodes in the list make newNode the first node. 
    	if(!head) 
    	{ 
    		head = newNode; 
    		newNode->next = NULL; 
    	} 
    	// Otherwise, insert newNode. 
    	else 
    	{ 
    		// position nodePtr at the head of list. 
    		nodePtr = head; 
    
    		// Initialize previousNode to NULL. 
    		previousNode = NULL; 
    
    		// Skip all nodes whose value is less than num. 
    		while (nodePtr != NULL && nodePtr->value < newValue) 
    		{ 
    			previousNode = nodePtr; 
    			nodePtr = nodePtr->next; 
    		} 
    
    		// If the new node is to be the 1st in the list, 
    		// insert it before all other nodes. 
    		if (previousNode == NULL) 
    		{
    			head = newNode; 
    			newNode->next = nodePtr; 
    		}
    
    		else // Otherwise insert after the previous node. 
    		{ 
    			previousNode->next = newNode; 
    			newNode->next = nodePtr; 
    		} 
    	} 
    } 
    
    //******************************************************** 
    // The deleteNode function searches for a node  
    // with searchValue as its value. The node, if found, * 
    // is deleted from the l ist and from memory. * 
    //******************************************************** 
    template <class T> 
    void Listing<T>::deleteNode(T searchValue) 
    { 
    	ListNode *nodePtr;			// To traverse the list 
    	ListNode *previousNode;		// To point to the previous node
    
    	// If the list is empty, do nothing. 
    	if (!head) 
    		return; 
    	// Determine if the first node is the one. 
    	if (head->value == searchValue) 
    	{ 
    		nodePtr = head->next; 
    		delete head; 
    		head = nodePtr; 
    	} 
    	else 
    	{ 
    		// Initialize nodePtr to head of list. 
    		nodePtr = head; 
    
    		// Skip a11 nodes whose value member is not equal to num.
    		while (nodePtr != NULL && nodePtr->value != searchValue) 
    		{ 
    			previousNode = nodePtr; 
    			nodePtr = nodePtr->next;
    		} 
    
    		// If nodePtr is not at the end of the list, link the prevLous
    		// node to the node after nodePtr, then delete nodePtr.
    		if (nodePtr) 
    		{
    			previousNode->next = nodePtr->next; 
    			delete nodePtr;
    		}
    	} 
    } 
    
    
    //******************************************************** 
    // Destructor 
    // This function deletes every node in the list.
    //******************************************************** 
    template <class T> 
    Listing<T>::~Listing() 
    { 
    	ListNode *nodePtr;		// To traverse the list 
    	ListNode *nextNode;		// To point to the next node 
    
    	// position nodePtr at the head of the list. 
    	nodePtr = head; 
    
    	// While nodePtr is not at the end of the list... 
    	while (nodePtr != NULL) 
    	{ 
    		// Save a pointer to the next node . 
    		nextNode = nodePtr->next; 
    		// Delete the current node. 
    		delete nodePtr; 
    		// position nodePtr at the next node . 
    		nodePtr = nextNode; 
    	} 
    }
    #endif 
    
    
  2. In Topic: An example of a Linked list program with multiple fields?

    Posted 29 Oct 2012

    Yes, I was thinking about that.

    The structure of the multiple fields would be in int main, but what of line 14 in the original post?
  3. In Topic: Selection sort refuses to work properly.

    Posted 24 Oct 2012

    The most I've learned from googling "Debugging", is how to use breakpoints, which has been of no use so far.

    http://weblogs.asp.n...tudio-2010.aspx

    Edit: Weird, it seems, the program works now, by slightly modifying the sort algorithm.
  4. In Topic: Selection sort refuses to work properly.

    Posted 24 Oct 2012

    I googled on a debugging turoial, and I couldn't find a proper one, for Visual C++ 2010.
    https://www.google.c...c.1.LjZBNUpwy3I

    This was worthless: http://kipirvine.com...o2010/index.htm

    BTW, the array index was able to be arranged in order by changing Line 143 in SearchableVector.h, from T, to int type.
    	int startScan, minIndex; 
    	T minValue; 
    


    But instead of 1 2 3 5 6 7 15
    Output is 0 5 5 5 5 6 15 15
  5. In Topic: Logical error Searching a Template vector array.

    Posted 21 Oct 2012

    I think i may have solved it.

    Modifying the findItem function's, for loop, seemed to get the program to display the correct answer.

    template <class T> 
    int SearchableVector<T>::findItem(const T item) 
    { 
    	for (int count = 0; count <= this->size()-1; count++) 
    	{
    		if (getElementAt(count) == item) 
    			return count; 
    	}
    	return -1;
    }
    #endif 
    

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

mgrex hasn't added any friends yet.

Comments

mgrex has no profile comments yet. Why not say hello?