Sorting Question

This is a discussion on Sorting Question within the C++ Programming forums, part of the General Programming Boards category; I have too sort the iD numbers of an array in order. with a linearsort, but i have to keep ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Sorting Question

    I have too sort the iD numbers of an array in order. with a linearsort, but i have to keep the iD and GPAs matched up, because i have an array for ID and an array for the GPAs, so if they IDS move, then they wouldnt match the IDS. how can i sort them so they stick together? So the IDS get sorted and the GPAs stay with thier respective ids?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    make a struct (or class) of ID and GPA
    sort an array of that struct instead.

    Alternatively, use std:: pair.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Quote Originally Posted by cyberfish View Post
    make a struct (or class) of ID and GPA
    sort an array of that struct instead.

    Alternatively, use std:: pair.
    Yeah im new in C++, and have no idea what that is, but my prof. said i need to stick with what i know and use a forloop and the linear arrays

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,390
    Have you learned about structs and classes?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    then, do it as you would on the array, but swap the elements in both arrays where you would have swapped one

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Quote Originally Posted by cyberfish View Post
    then, do it as you would on the array, but swap the elements in both arrays where you would have swapped one
    Code:
    void lsort(int &n, int val[], double gp[]){
         
         int temp,temp1;
         
         for(int pass = 0; pass < n - 1; pass++)
              for(int cand = pass + 1; cand < n; cand++)
                   if(val[pass] > val[cand]){
                                 temp=val[pass];
                                 val[pass]=val[cand];
                                 gp[pass]=gp[cand];
                                 val[cand]=temp;
                                 gp[pass]=gp[cand];
                   }
         return;
    }
    yeah thats what i did but when i print the new array, in the output i get this::

    ID GPA
    123 1.9
    124 1.9
    225 1.1
    632 3.2

    THE ID GOES IN ORDER BUT THE 124 ID, gets the 123 id for some reason?
    THE INPUT FROM FILE IS THIS
    4
    124 2.6
    123 1.9
    225 1.1
    632 3.2

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    You have sucessfully implemented a sorting algorithm. It's what I'd call SimpleSort.

    You are swapping items in the val array using a temporary just fine, but then you go and copy an item in the gp array over top of another item in the gp array (twice in fact), wiping that item out. What made you think that the process for swapping items in one array would be different to the process of swapping items in the other array?
    You could make it at least slightly easier for yourself and use std::swap instead.

    You could also use std::sort, but you'd probably need quite a peculiar assignment operator in order to swap items in both arrays. You'd be best to make a struct that holds both pieces of data if possible.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Quote Originally Posted by iMalc View Post
    You have sucessfully implemented a sorting algorithm. It's what I'd call SimpleSort.

    You are swapping items in the val array using a temporary just fine, but then you go and copy an item in the gp array over top of another item in the gp array (twice in fact), wiping that item out. What made you think that the process for swapping items in one array would be different to the process of swapping items in the other array?
    You could make it at least slightly easier for yourself and use std::swap instead.

    You could also use std::sort, but you'd probably need quite a peculiar assignment operator in order to swap items in both arrays. You'd be best to make a struct that holds both pieces of data if possible.
    I cant use any structure or that std:: because she han't taught it yet, i have to use what i have there. So how can i fix it, that it get the right values for GPA?

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by Rkukulski View Post
    I cant use any structure or that std:: because she han't taught it yet, i have to use what i have there. So how can i fix it, that it get the right values for GPA?
    What you want to do is detect the ordering by comparing the data in the val array "check",
    you swap the data for the items 'pass' and 'cond' in the val array "check",
    and the items 'pass' and 'cond' in the gp array "failure".
    You already worked out that you need a temporary item to swap items in the first array. You need to do the same thing for the other array as well. You've already made this "temp1" variable, but it's the wrong type to use for swapping data in the gp array of doubles.
    I don't think I can make it any clearer without giving you the code.
    Last edited by iMalc; 04-28-2008 at 03:27 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    You could always start by sorting one array first, then make a copy of that code and modify it for the second array
    It should be easier that way. Get one working first, then copy that working implementation and do it for the second.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Quote Originally Posted by Elysia View Post
    You could always start by sorting one array first, then make a copy of that code and modify it for the second array
    It should be easier that way. Get one working first, then copy that working implementation and do it for the second.
    I don't think that is what he wants to do, if I understand the question right.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    What it means, first get the code for sorting the first array done.
    Then, by using that as a template, get the code for sorting the secondary array done.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by Elysia View Post
    What it means, first get the code for sorting the first array done.
    Then, by using that as a template, get the code for sorting the secondary array done.
    Unfortunately you don't understand what the OP wants to do, because what you're talking about isn't possible. It would be impossible to sort one array without sorting the other array at the same time or the corresponding items from the other array will not match up.
    The only way to even come close would be to generate an index list in a third array and then use that to step through the items in the other two arrays instead of stepping through them from 0 to n-1. That is unfortunately a lot more complex.

    All Rkukulski needs to do is get the other swap corrected.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    I was relaying the common message:
    Don't focus on getting two tasks done at once. Fix the first, then the second.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. Question sorting character arrays: C programming
    By NeoSigma in forum C Programming
    Replies: 3
    Last Post: 05-23-2003, 09:28 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. an actual question (sorting arrays)
    By master5001 in forum C Programming
    Replies: 4
    Last Post: 08-13-2001, 10:21 PM

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