Another Question

This is a discussion on Another Question within the C++ Programming forums, part of the General Programming Boards category; An array has 5 numbers in it and another array has 5 strings in it. While keeping each array in ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    Another Question

    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.

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    22
    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.

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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.

  5. #5
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    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);
    }
    if any numbers are the same this probley will not work. easy to fix.
    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

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    In addition, if you haven't seen it already, there is another recent thread that could be of great help to you:

    Sorting a vector numerically

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21