-
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
-
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.
-
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;
}
}
-
Thank you guys very much for all the helps...