# Moving sorting to fuction

• 09-15-2010
Passa
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!
• 09-15-2010
orientuser
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;

• 09-16-2010
Passa
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.
• 09-16-2010
orientuser
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; }```
• 09-16-2010
msh
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.
• 09-16-2010
Passa
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!
• 09-16-2010
msh
Are you allowed to use structures or not?
• 09-16-2010
Passa
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! :)
• 09-16-2010
msh
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?