-
Sorting Arrays
You all probably have no trouble with this but I do. I am sorting two dimensional arrays. Don't have any idea how to do this. Never done it before. But this is what I have.
double temp2[][];
long temp1;
for(i = 0; i<emp_counter; i++)
{
for(j = 0; j<emp_counter; j++)
{
if(emp_id[j - 1] > emp_id[j])
{
temp1 = emp_id [j - 1];
emp_id [j - 1] = emp_id [j];
emp_id [j] = temp1;
strcpy(temp2[j-1], last[j]);
strcpy(last[j - 1], last[j]);
strcpy(last[j], temp2[j-1]);
}
}
Is this at all right. It's close. But I don't know what to declare temp2 as.
Sorry if this is considered one of those "Annoying Questions."
:confused:
-
First of all, that's code for normal arrays, and second of all it's not entirely correct...well, it is but instead of strcpy you should just use an equals sign. ex: array[1] = array[2]. For multidimensional arrays just add another loop, our you could run an entire sort on array[j][] and another one on array[][j]. Either one works just fine.
-
I couldn't figure out whether this was a 2D array of doubles, a 2D array of chars, or a 1D array of strings.
Besides, I'd use qsort (see previous discussions)
-
sorting
I guess I didn't explain myself very well. I am dealing with a 2D array for double, 2D array for char, char, long and intergers. I am not suppose to use qsort for some reason. I wouldn't be able to have to seperate codes for array[][j] and array[j][] because my teacher is totally into speed and short codes. Here is a part of the code again. Hopefully I can get a little help.
int i, j;
long temp1;
double temp2[][];
char temp3[][];
char temp4;
for(i = 0; i<emp_counter; i++)
{
for(j = 0; j<emp_counter; j++)
{
if(emp_id[j - 1] > emp_id[j])
{
temp1 = emp_id [j - 1];
emp_id [j - 1] = emp_id [j];
emp_id [j] = temp1;
strcpy(temp3[j-1], last[j]);
strcpy(last[j - 1], last[j]);
strcpy(last[j], temp3[j-1]);
strcpy(temp3[j-1], first[j]);
strcpy(first[j - 1], first[j]);
strcpy(first[j], temp3[j-1]);
temp2 = wg_hr_py [j - 1][0];
wg_hr_py [j - 1][0] = wg_hr_py [j][0];
wg_hr_py [j][0] = temp2;
temp2 = wg_hr_py [j - 1][1];
wg_hr_py [j - 1][1] = wg_hr_py [j][1];
wg_hr_py [j][1] = temp2;
temp2 = wg_hr_py [j - 1][2];
wg_hr_py [j - 1][2] = wg_hr_py [j][2];
wg_hr_py [j][2] = temp2;
strcpy(temp4[j-1], section[j]);
strcpy(section[j - 1], section[j]);
strcpy(section[j], temp4[j-1]);
Would I be able to get away with my variables being declared like that? Confused and frustrated. I can't seem to get past this point.