An array has 5 numbers in it and another array has 5 strings in it. While keeping each array in order with the other. How would you organize them so that the numbers go from least to greatest in the array.
An array has 5 numbers in it and another array has 5 strings in it. While keeping each array in order with the other. How would you organize them so that the numbers go from least to greatest in the array.
wouldn't this be easier to do with a two-dimensional array? like a table...
EDIT: Nevermind. I don't really think so...
I actually had a similar problem to this for an assignment earlier this semester. What I ended up doing was using a "work" array, and then copy the data into my work array in order, and then copy it back into the main array when I was done. You could do the same thing here. I know there's a way to do it without a temporary array, but I couldn't get it to work properly.
Last edited by Ricochet; 12-11-2003 at 02:19 AM.
you probably want a single array with structs or classes in it which contain both the number and the string.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
Ricochet's suggestion of using temporary variables is probably the simplest. You could do this one string, and one number-variable, but I think the easiest way would be to use two complete temporary arrays.
Assumptions:
- You haven't studied Structures, Classes, the Standard Template Library.
- You can figure-out how to sort the "numbers".
- You don't care about efficient use of memory (only 5 array-pairs).
// ===== Psudo Code ====================
// Given the following, sort by age:
Name[1] == Bob
Name[2] == George
Name[3] == Sally
Age[1] == 15 //Bob's Age
Age[2] == 20 //George's Age
Age[3] == 10 //Sally's Age
// Make a new set of arrays sorted by age, like this:
TempName[1] = Name[3] // Sally is youngest
TempAge[1] = Age[3] // Sally's Age = 10
TempName[2] = Name[1] // Bob
TempAge[2] = Age[1] // Bob's Age = 15
TempName[3] = Name[2] // George is oldest
TempAge[3] = Age[2] // George's Age = 20
// Now the temporay arrays are sorted...
// Overwrite the original array:
Name[1] = TempName[1] // Sally is youngest
Name[2] = TempName[2] // Bob
Name[3] = TempName[3] // George is oldest
Age[1] = TempAge[1] // Sally's Age = 10
Age[2] = TempAge[2] // Bob's Age = 15
Age[3] = TempAge[3] // George's Age = 20
// ===== Psudo Code ====================
And, FYI:
If you were using structures or classes, your variables might look something like this:
Person[1].Name =Sally
Person[1].Age = 10
This keeps all of the related information together.
Suggestion: Use more descriptive titles for our posts. See the Board Guidelines and Hints. "Another Question" is kinda' vague.
if any numbers are the same this probley will not work. easy to fix.Code:void cflArrange_intArray( int* array, int count) { int temp = 0; int* temp_array = (int*)tsxCalloc( count, sizeof(int)); if(!temp_array) return; for(int a=0; a != count; a++){ int pos = 0; temp = array[a]; for(int b=0; b != count; b++){ if( temp > array[b]) pos++; } temp_array[pos] = temp; } memcpy( array, temp_array, count*sizeof(int)); tsxFree( (void*) temp_array); }
ignore the tsxFree and tsxCalloc. switch then our for the normal memory functions
btw-if yo ushow this to your teacher and their worth half their weight in mustard then they will ask you where you got it. if not copy away
Last edited by Nor; 12-11-2003 at 04:43 PM.
Try to help all less knowledgeable than yourself, within
the limits provided by time, complexity and tolerance.
- Nor
In addition, if you haven't seen it already, there is another recent thread that could be of great help to you:
http://cboard.cprogramming.com/showt...threadid=48247