1

So I have an Array of Structs with Months and Days on it, and I want to sort it so the first struct will hold the day 1 of the month 1, the second the day 2 of the mont 1 and so on.

I am trying to do this by storing the correct array in a temporal array and then replacing them.

My problem is, to do the following, the only algorithm I could made crashes my prompt and gives me a segmetation fault. By now i'm confused enough not to know if my logic is wrong or i'm doing it in a too complex way for the program to work.

Here is the function with my cod

void sortData(struct StructData data[], int size){

    int i=0,x=0,z=0,v_day=0,v2_day=0;
    struct StructData temp[sz];

    for (i=0;i!=12;i++){

        for (x=0;x!=12;x++){

            if (data[x].month == i+1){

                for (v=0;v!=31;v++){

                    for(v2=0;v2!=31;v2++){

                        if (data[v2].day == v+1){

                            temporal[z] = data[v2];
                            z=z+1;

                        }
                    }
                }
            } 
        }
    }

    i=0;


    for (i=0;i!=size;i++){
        data[i] = temporal[i];
    }
}

There is probably a better way to do this, I just don't see it.

jchang
  • 31
  • 1
  • 7
  • Try messing around with qsort: http://stackoverflow.com/questions/1787996/c-library-function-to-do-sort – Phyreprooph Dec 09 '15 at 07:52
  • Segfault here is probably due to out of bound array indexes. Use a debugger which will tell you where exactly the segfault occurs. If you don't know how to use a debugger then it's time to start learning it now. Show more code if you want more answers. – Jabberwocky Dec 09 '15 at 07:54
  • @Phyreprooph I though so, but how could I sort the whole struct by that rule? I don´t want to sort just the days. I need the whole struct sorted as it holds data of that specific day. – jchang Dec 09 '15 at 07:55
  • will you add your structure ? – Shvet Chakra Dec 09 '15 at 07:57
  • @MichaelWalz I added the whole function to the post. By 'out of bond array indexes' you mean I'm working with variables I don't have? If so, I thought it wouldn't be a problem as i'm using an ' if ' statement that should not operate with the variable as long as it doesn't match the requirements. – jchang Dec 09 '15 at 08:06
  • Are you sure that `v2`, that can reach value `30`, is always within your data bounds that are, as I can understand, at max `size` ? The same for index `x`. As I can understand you are using `for` to loop for all possible month and days, but the error is that you are using them as indexes also. – LPs Dec 09 '15 at 08:08
  • @hydrz I meant "out of bound". An out of bound array index is an index that is greater than the size of the array, eg: `int array[10]; x = array[15];`. – Jabberwocky Dec 09 '15 at 08:09
  • @MichaelWalz Oh. The array size is by the thousands, but I'm not sure if that's what you mean. I'm not an english speaker. – jchang Dec 09 '15 at 08:18
  • @hydrz if your array has a size of 10 and you try to access the element number 15 (this is the index) the your index is out of bounds. This concept is ver very very important to understand. – Jabberwocky Dec 09 '15 at 08:27

2 Answers2

1

You probably want to use qsort with a compare function something like this:

int comp (const void * elem1, const void * elem2)
{
    struct StructData f = *((struct StructData*)elem1);
    struct StructData s = *((struct StructData*)elem2);
    if (f.day > s.day) return  1;
    if (f.day < s.day) return -1;
    return 0;
}

Keep in mind that all this code probably won't work. Haven't written C code in a long time so this is more like theoretical code. Basically you want to be looking at the structures from the array and comparing their values inside the function somehow.

Phyreprooph
  • 517
  • 2
  • 13
-1

Sort on months, if they are same sort on days. Use any sorting algo.