Thread: Noob need help too.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Noob need help too.

    This is Just basic C.

    Hi all,
    I am a noob in here. I was trying to work work with a program that read one line at a time from a text file, then allocate memory for the name and convert the number into an int. For Example, "LIGHT BLUE 123456" this is one line in the text file. The job here is to Allocate the LIGHT BLUE and convert 123456 from char to int, then store it into an int array. Well that is all done. However, i also have to sort the list of NAME in alphabetical order too.. Then what ever come first, the number of that name has to move along... the bold part is the part that i am stuck on..

    Any help is appreciated.

    this is my code for sort and compare 2 strings

    Code:
    void SortString(char *colorName[], int colorVal[], int index)
    {
    	 int curr;// the index start at the second elenment from the left in the array of pointer
         int walk; // the index at the element want to compares
         char *hold; // pointer to the string you want to compare with
    	 char *temp; // pointer to the the next value in the string to compared to
    	 int located; // true or false
         
         for(curr = 1; curr < MAXMAIN; curr++)
         {
             hold = colorName[curr];  // assign the point at curr to hold
    		 located = 0;
             
             for(walk = curr - 1; walk >=0 && !located;)
             {
    			 temp = colorName [walk]; //assign the first string of colorname into temp, make ready for compare
                 if (myStricmp(hold,temp) < 0)
                 {
    				 colorName[walk +1] = colorName[walk];
                     //colorVal [walk +1] = colorVal[walk];
    				 walk--;  // move 1 index 
                 }
                 else
                 {
                     located = 1;
                 }             
             }
             colorName[walk+1] = hold;
         }
         return;
    }
    
    
    int myStricmp (char str1[], char str2[])
    { // pointer traversal version:
    	
    	while(tolower(*str1)==tolower(*str2) && *str1!= '\0'){	
    		++str1;
    		++str2;
    	} // end while
    
    	return (tolower(*str1)-tolower(*str2));
    } // end myStricmp

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for ...
       for ...
            if swap condition
            {
                int numtmp;
                char *nametmp;
    
                /* swap name locations */
                nametmp = namearray[ swapspot1 ];
                namearray[ swapspot1 ] = namearray[ swapspot2 ];
                namearray[ swapspot2 ] = nametmp;
    
                /* swap numbers too */
                numtemp = numarray[ swapspot1 ];
                numarray[ swapspot1 ] = numarray[ swapspot2 ];
                numarray[ swapspot2 ] = numtmp;
    
                /* swap something else too... */
            }
    You just need to do swap both at the same time, using the same indexes (assuming you are just running parallel arrays, which appears to be the case and question). You could swap countless arrays here at the same time if you really wanted to, just by adding another three swap lines for each one.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    You have the right idea with the commented out line
    Code:
    //colorVal [walk +1] = colorVal[walk];
    But that is only part of sorting colorVal too. You also have to have a hold for colorVal.
    Code:
    void SortString(char *colorName[], int colorVal[], int index)
    {
        int curr;// the index start at the second elenment from the left in the array of pointer
        int walk; // the index at the element want to compares
        char *hold; // pointer to the string you want to compare with
        char *temp; // pointer to the the next value in the string to compared to
        int holdVal;
        int located; // true or false
        
        for(curr = 1; curr < MAXMAIN; curr++)
        {
            hold = colorName[curr];  // assign the point at curr to hold
            holdVal = colorVal[curr];
            located = 0;
            
            for(walk = curr - 1; walk >=0 && !located;)
            {
                temp = colorName [walk]; //assign the first string of colorname into temp, make ready for compare
                if (myStricmp(hold,temp) < 0)
                {
                    colorName[walk +1] = colorName[walk];
                    colorVal[walk +1] = colorVal[walk];
                    walk--;  // move 1 index 
                }
                else
                {
                    located = 1;
                }             
            }
            colorName[walk+1] = hold;
            colorVal[walk+1] = holdVal;
        }
        return;
    }
    A much cleaner method is grouping colorName and colorVal into a struct and then sorting the struct.
    Code:
    struct color
    {
        char* name;
        int val;
    };
    
    void sort_color(struct color a[], int limit)
    {
        for (int x = 1; x < limit; ++x)
        {
            struct color hold = a[x];
            bool located = false;
            int y;
            
            for (y = x - 1; y >= 0 && !located; )
            {
                if (myStricmp(hold.name, color[y].name) < 0)
                {
                    color[y+1] = color[y];
                    --y;
                }
                else
                {
                    located = true;
                }             
            }
    
            color[y+1] = hold;
        }
    }

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2

    Cool

    Thank you guys very much for all the helps...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob to C need a little help!!!
    By sk8harddiefast in forum C Programming
    Replies: 8
    Last Post: 02-27-2010, 02:47 PM
  2. noob
    By valthyx in forum C++ Programming
    Replies: 36
    Last Post: 07-22-2008, 01:04 PM
  3. Please help out a noob
    By Pop in forum C Programming
    Replies: 14
    Last Post: 10-12-2006, 06:52 AM
  4. noob help
    By Xenofon in forum C Programming
    Replies: 5
    Last Post: 10-05-2006, 05:42 AM
  5. plz help here, noob actually .D
    By BjoRn3n in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2005, 03:04 PM