0

The purpose of the program is to create a random list of 1000 numbers in an array, sort that array, then find the greatest set of numbers within (x, x+50). The program successfully generates and sorts the numbers within the array, but crashes when the (i, j) set finding algorithm starts. The program generates no errors on compiling, and I'm sure the error is simple, but for the life of me I can't find the issue. Thanks in advance you amazing people!

int main( ){

    int a, b, temp, i, j, x, y, tempTotal, arrayStartMax;
    int finalTotal = 0;
    int *info[ARRAY_FULL];

    for (i=0; i<ARRAY_FULL; i++){
        info[i]=(int*)malloc(sizeof(int));
        *info[i]=rand()%1000;
    }

    for (a = 0; a < ARRAY_FULL; ++a){
        for (b = a + 1; b < ARRAY_FULL; ++b){
            if (*info[a] > *info[b]){
                temp = *info[a];
                *info[a] = *info[b];
                *info[b] = temp;
            }
        }
    }

    for (i=0; i<ARRAY_FULL; i++){
        printf("%d\n", *info[i]);
    }

    for (i = 0; i <= ARRAY_HALF; i++){
        x = *info[i];
        y = x+ARRAY_HALF;
        tempTotal = 0;
        for (j = i; j < i+ARRAY_HALF; i++){
            if (*info[j] >= x || *info[j] <= y) {
                tempTotal++;
            }
            if (tempTotal > finalTotal) {
                arrayStartMax = *info[i];
                finalTotal = tempTotal;
            }
        }
    }

    printf("Interval should start at %d for maximum numbers in a set.", arrayStartMax);

}

For the purpose of this program I would like to mention that ARRAY_FULL = 100 and ARRAY_HALF = 50.

14
  • 2
    for (j = i; j < i+ARRAY_HALF; i++){ : i++ --> j++ ? Commented Jul 11, 2017 at 3:37
  • 3
    Also I suggest int *info[ARRAY_FULL]; --> int info[ARRAY_FULL]; Commented Jul 11, 2017 at 3:38
  • 1
    since the values in the array range from 0 to 999, and since the array is sorted, the only possible set of 50 max values is in array index 49 to array index 99. So all the messy calculations in the last 1/2 of the posted code can be eliminated Commented Jul 11, 2017 at 4:37
  • 1
    @user3629249: A stream of 9 (valid) comments about coding style etc should probably be converted into a single answer. Commented Jul 11, 2017 at 5:15
  • 1
    It is curious that you state that you're working with 1000 numbers, but ARRAY_FULL is 100 and ARRAY_HALF is 50. Also, if you make an MCVE (minimal reproducible example), then your comment would be unnecessary; the values would be shown. Please create an MCVE in future — it won't be much bigger than the code you showed anyway (10 extra lines?). Commented Jul 11, 2017 at 5:18

1 Answer 1

1

Your code is throwing segfault because you're walking i out of bounds in this for loop.

for (j = i; j < i+ARRAY_HALF; i++){
        if (*info[j] >= x || *info[j] <= y) {
            tempTotal++;
        }
        if (tempTotal > finalTotal) {
            arrayStartMax = *info[i];
            finalTotal = tempTotal;
        }

You set j = i then increment i prior to the comparison. So j will always be less than i.

Limit i in the comparison section of the for loop and it won't segfault.

I don't think the comparison is doing what you want, but you should be able to find your way home from here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.