Thread: Help! Sorting Problem

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    6

    Help! Sorting Problem

    I'm working on a homework assignment, I have to make a program that balances Chemical Equations. I've got most of the work done but my program requires that both sides of the equation be in the same order.
    To do this I figured I would sort them into alphabetical order. I wrote up some code to do this(listed below).

    I really need some help because its due in a couple of days, can someone tell me what I am doing wrong.

    Its an Insertion sort, the code should be fairly explanatory.
    thanks for your help.

    Code:
    short CStore::AlphaSort()
    {
    	int i = 0; //Loop Varibles
    	int k = 0;
    	char key = 0;		// current value working on
    
    	for(i = 1; i <= EleNum; i++)
    	{
    		key = CStore::Element[i].Capital;
    
    		k = i-1;
    	cout << "A" << key << "|" << i << "|" << k << endl; 		
    
    	//////////////////////
    		while( CStore::Element[k].Capital > key)	//go through 
    		{
    			//move a value up one in the list
    
    			CStore::Element[k+1].ChemNum = CStore::Element[k].ChemNum;
    			CStore::Element[k+1].Capital = CStore::Element[k].Capital;
    			CStore::Element[k+1].LCase   = CStore::Element[k].LCase;
    			CStore::Element[k+1].Amount  = CStore::Element[k].Amount;
    
    			cout << "B" << key << "|" << i << "|" << k << endl;
    			
    			if(k == 0)			//when loop reaches bottom
    				{
    					cout << "C" << key << "|" << i << "|" << k << endl;
    					CStore::Element[k].ChemNum = CStore::Element[i].ChemNum;
    					CStore::Element[k].Capital = CStore::Element[i].Capital;
    					CStore::Element[k].LCase   = CStore::Element[i].LCase;
    					CStore::Element[k].Amount  = CStore::Element[i].Amount;
    
    				}
    
    			k--;								
    
    		}
    	//////////////////////
    
    	CStore::Element[k+1].ChemNum = CStore::Element[i].ChemNum;
    	CStore::Element[k+1].Capital = CStore::Element[i].Capital;
    	CStore::Element[k+1].LCase   = CStore::Element[i].LCase;
    	CStore::Element[k+1].Amount  = CStore::Element[i].Amount;
    	cout << "D" << key << "|" << i << "|" << k << endl;
    
    	}
    	////////////////////////////////////////////////
    	PrintArray("After Sort:");
    
    	/////////////////////////////////////////////////////////
    	return 1;
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(i = 1; i <= EleNum; i++)
    Well if it's stored in a regular array, then you're off by 1 here
    for(i = 0; i < EleNum; i++)

    > key = CStore::Element[i].Capital;
    If the element is part of your class, there's no need to prefix everything with scope
    key = Element[i].Capital;

    > CStore::Element[k+1].ChemNum = CStore::Element[k].ChemNum;
    You can do in 1 line what you do in 4, by doing
    Element[k+1] = Element[k];

    Go study the algorithm again - I'm pretty sure you need to store at least one element in a temporary variable while you move elements around in the array.
    All I see at the moment is lots of clashes as one element overwrites another in the process of moving it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    6
    Thanks for the tips, I've cleaned up the code, but i'm still having problems. The cleaned up code is below


    Code:
    short CStore::AlphaSort()
    {
    	int i = 0; //Loop Varibles
    	int k = 0;
    	CElement Temp;
    	char key = 0;		// current value working on
    
    	for(i = 1; i <= EleNum; i++)
    	{
    
    		Temp = Element[i];
    
    		k = i-1;	
    
    	//////////////////////
               //go through the loop backwards moving data up
    		while(CStore::Element[k].Capital > key)	
    		{
    			//move a value up one in the list
    
    			Element[k+1] = Element[k];
    		
    			if(k == 0)			//when loop reaches bottom
    				{
    					Element[k] = Temp;
    				}
    
    			k--;	//the while loop varible decrements
    
    		}
    	//////////////////////
    	Element[k+1] = Temp;
    
    	}
    	////////////////////////////////////////////////
    	PrintArray("After Sort:");
    
    	/////////////////////////////////////////////////////////
    	return 1;
    
    
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Not all of it though
    for(i = 1; i <= EleNum; i++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    6
    Its meant to start at 1

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    6
    I fixed it!
    One of the varibles was the wrong one. Here is the code if anybody cares.

    Code:
    short CStore::AlphaSort()
    {
    	int i = 0; //Loop Varibles
    	int k = 0;
    	CElement Temp;
    
    	for(i = 1; i <= EleNum; i++)
    	{
    
    		Temp = Element[i];
    
    		k = i-1;	
    
    	//////////////////////
    		while((k >= 0) && Element[k].Capital > Temp.Capital)	//go through the loop backwards moving data up
    		{
    			//move a value up one in the list
    
    			Element[k+1] = Element[k];
    		
    			if(k == 0)			//when loop reaches bottom
    				{
    					Element[k] = Temp;
    				}
    
    			k--;		//the while loop varible decrements
    
    		}
    	//////////////////////
    	Element[k+1] = Temp;
    
    	}
    	////////////////////////////////////////////////
    	PrintArray("After Sort:");
    
    	/////////////////////////////////////////////////////////
    	return 1;
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problem?
    By audinue in forum C Programming
    Replies: 5
    Last Post: 01-06-2009, 02:16 PM
  2. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  3. What is problem about the sorting?
    By Mathsniper in forum C Programming
    Replies: 2
    Last Post: 04-17-2005, 07:00 AM
  4. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 01:44 PM