Moving sorting to fuction

This is a discussion on Moving sorting to fuction within the C Programming forums, part of the General Programming Boards category; Hi, I have a branched sorting method for moving correlating sales data between multiple arrays in ascending order by a ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    10

    Moving sorting to fuction

    Hi,

    I have a branched sorting method for moving correlating sales data between multiple arrays in ascending order by a certain order of arrays. It works fine but results in a lot of repeat code. I've been trying to move the one most repeated, yet identical bit of code to a function, but haven't had any success and was hoping I could get some help. Here is an example of the code in action (at one of the last stages of sorting):

    Code:
    for (i=0; i < numoflines; i=i+1) {
        for (j=0; j < numoflines - 1; j=j+1) {
            if(postcode[j] == postcode[j+1]) {
                if(year[j] == year[j+1]) {
                    if(month[j] == month[j+1]) {
                        if(price[j] == price[j+1]) {
                            if(bedrooms[j]>bedrooms[j+1]) {
                                tempmove = postcode[j+1];
                                postcode[j+1] = postcode[j];
                                postcode[j] = tempmove;
                                
                                tempmove = month[j+1];
                                month[j+1] = month[j];
                                month[j] = tempmove;
                                
                                tempmove = year[j+1];
                                year[j+1] = year[j];
                                year[j] = tempmove;
                                
                                tempmove = bedrooms[j+1];
                                bedrooms[j+1] = bedrooms[j];
                                bedrooms[j] = tempmove;
                                
                                tempmove = price[j+1];
                                price[j+1] = price[j];
                                price[j] = tempmove;
                            }
                        }
                    }
                }
            }
        }
    }
    I essentially want to move all the code within the last "if" function to a separate function I can simply call in the middle of all the branched "if"s. I've tried a couple of ways but I don't seem to have the correct idea for how to create the function in the correct format.

    I don't really want to radically change the way I'm sorting the data, just move the following code to a separate function to make my source file a lot shorter and easy to read:

    Code:
    tempmove = postcode[j+1];
    postcode[j+1] = postcode[j];
    postcode[j] = tempmove;
    
    tempmove = month[j+1];
    month[j+1] = month[j];
    month[j] = tempmove;
    
    tempmove = year[j+1];
    year[j+1] = year[j];
    year[j] = tempmove;
    
    tempmove = bedrooms[j+1];
    bedrooms[j+1] = bedrooms[j];
    bedrooms[j] = tempmove;
    
    tempmove = price[j+1];
    price[j+1] = price[j];
    price[j] = tempmove;
    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2010
    Location
    China
    Posts
    12
    May I ask you some questions?
    1
    Are the arrays, such as year, month, local or global?

    2
    since one of the 'if' conditions is if (year[j] == year[j+1])
    why did you exchange the values of year[j] and year[j+1] like this:
    tempmove = year[j+1];
    year[j+1] = year[j];
    year[j] = tempmove;


    Sorry for my bad English.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    I'm not sure what you mean by local or global, but they're declared in the main function. If I had to guess, I'm guessing that makes them local?

    The reason it still moves the array value that is equal is to keep the code the same in all of the sorting bits, so I could (hopefully) easily replace them with one function.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    China
    Posts
    12
    Since the arrays are local, i.e. declared in a function, you must pass them as arguments to your new function.
    I think the following code can work as you expect.
    Code:
    for (i=0; i < numoflines; i=i+1) {
        for (j=0; j < numoflines - 1; j=j+1) {
            if(postcode[j] == postcode[j+1]) {
                if(year[j] == year[j+1]) {
                    if(month[j] == month[j+1]) {
                        if(price[j] == price[j+1]) {
                            if(bedrooms[j]>bedrooms[j+1]) {                           
                                ExchangeValuesWithNextNeighbor(&postcode[j], &month[j],&year[j],&bedrooms[j],&price[j]);
                            }
                        }
                    }
                }
            }
        }
    }
    
    void ExchangeValuesWithNextNeighbor(int *p1, int *p2, int *p3, int *p4, int *p5)
    {
        int tempmove;
    
        tempmove = p1[1];
        p1[1] = p1[0];
        p1[0] = tempmove;
     
        tempmove = p2[1];
        p2[1] = p2[0];
        p2[0] = tempmove;
        
        tempmove = p3[1];
        p3[1] = p3[0];
        p3[0] = tempmove;
        
        tempmove = p4[1];
        p4[1] = p4[0];
        p4[0] = tempmove;
        
        tempmove = p5[1];
        p5[1] = p5[0];
        p5[0] = tempmove;
    
    }
    Last edited by orientuser; 09-16-2010 at 04:11 AM.

  5. #5
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Are you familiar with structures?

    I'm asking because the data seems to be related (does it describe a house or an apartment?), yet it is not organized as such. If it were organized in structures, you wouldn't need to do so many switching in separate arrays, that must be kept in sync, or else the data becomes effectively corrupted. A single array would be enough.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    Thank you for the help thus far.

    I did attempt a solution using pointers like that, but I was informed we could not use techniques we are yet to cover in lectures unfortunately. I might contact my lecturer however to double check this.. thanks orientuser!

    To msh, yes, it is data pertaining to house sales over a given period of time. Our program is required to handle input of between 10 to 10,000 lines of data. I'm pretty awful at C (I'm studying it as a general education subject within my Arts degree ) and my initial solution was to create separate arrays for each column of the input data. My tutor told me later that this wasn't a great way to do it but that I wouldn't be overly penalised for it. If I have enough time, I may attempt to move it to a single 2D array. Though I'm not sure it makes my sorting method any simpler!

  7. #7
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Are you allowed to use structures or not?

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    Sorry, should have clarified. We haven't covered structures yet, which I presume would rule out their use in this assignment. I'm also wary of doing things I don't necessarily understand in the first place since it's not going to help me much once I'm in my final exam!

  9. #9
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Could you provide a sample of input and output? I'm assuming input looks something like the below, yes?
    <begin text file>
    [number of inputs N]
    [input 1]
    [input 2]

    ...

    [input N]
    <end text file>

    Are the any other sorting criteria then the number of bedrooms?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-24-2007, 12:05 AM
  3. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 03:37 PM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM

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