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):
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.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 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:
Thanks!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;