Placing an interval in an array of pointers

I tried sending an email to my professor for help with this and it didn't help much.

a given interval of size 50, starting at x, when [x, x + 50] (x and x + 50 are on), find where to place this interval so that it has the maximum number of elements that the array points to in This. Consider only intervals starting with a number in the set. I am really stuck with this.

He gave us an array of random pointers, so his code. I took it apart.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int i, n, temp, j, k, x;
    int *info[100];

    for (i = 0; i < 100; i++) {
        info[i] = (int*)malloc(sizeof(int));
        *info[i] = rand() % 1000;
    }
    for (k = 0; k < 100; k++) {
        for (j = k; j < 100; j++) {
            if (*info[k] > *info[j]) {
                temp = *info[j];
                *info[j] = *info[k];
                *info[k] = temp;
            }
        }
    }

    for (k = 0; k < 100; k++) {
        if (*info[k/50] = 1)
            printf("%d\n %d\n", *info[k], *info[k] + 50);
            break;
    }

    return 0;
}

      

+3


source to share


1 answer


It's a matter of honest homework.
StackOverflow recommends helping, but not solving (and post the solution code I just tested later).

So here are some tips:

  • Summary: I believe the lesson here should teach the distinction between arrays and ranges of values, and between indices and the value of members in those indices.
  • there are several "ranges / arrays" involved
  • an array of 100 random values
  • random values ​​are in the range [0,1000)

  • the range to move is [x,x+50]

    almost always a sub-range of the value range (it can exceed 1000).
  • the values ​​of members of a random array can be within a range, this is a boolean attribute
  • Does the index affect a random array within a range
  • each possible position of the range starts with the value of a member of a random array
  • so that you have 100 possible range positions to check
  • for each of them, count the number of values ​​(members of the random array that are inside the range)
  • (sorry if that sounds like "haha," but) I think sorting only distracted you from the solution; at least if you skip optimization schemes first

FROM



"VRA":=range of possibe values in random array  
"."  :=position of the value of one member of random array  
       (only a few examples, instead of 100) 
"r"  :=possible range,  
       starting at the value of one member in random array,  
       ending 50 later


VRA [  .    ..        . .  . ...             .   .  ..              . )
    0                                                                1k
2 r    [    ]
2 r         [    ]
1 r          [    ]
3 r                   [    ]
3 r                     [    ]
4 r                        [    ]
3 r                          [    ]
2 r                           [    ]
1 r                            [    ]
2 r                                          [    ]
3 r                                              [    ]
2 r                                                 [    ]
1 r                                                  [    ]  
1 r                                                                 [    ]  

      

Note that the points do not change position when sorting a random array.

In my case (which repeats on my machine because the randomizer is not seeded), the answer is 10 hits in the range 703, which is index 62 of the sorted random array. And with an unsorted array, the result is the same, only 40.
Offtopic: This disappointed me. If we have forty-something and two-something, then, of course, it must have been 42 years old. Well, it can't be helped, the effect of the narrative is not that similar to this universe. (Yes, I know I'm mixing quotes from two authors.)

+1


source







All Articles