Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following code where an array of pointers is declared, memory is dy

ID: 3817120 • Letter: C

Question

Consider the following code where an array of pointers is declared, memory is dynamically allocated so that each pointer points to an integer and random numbers are inserted into the integers. int *info[100]; for (i=0; i With above setting, given an interval of size 50 starting at x as [x, x+50] (x and x+50 are included), find where to place this interval so that it has the maximum number of elements pointed by the array in it. Consider the following simple example with array elements 6 11 9 2 4 7 14 With an interval of size 3 [x, x+3], placing the interval at x=6, [x=6, x+3=9] has the most number of elements in it since 3 elements (6, 7 and 9) are in it. Consider only intervals that start at a number in the set.

Explanation / Answer

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

int main()
{

int *info[100];
int maxIndex=0,count,maxCount=0;

srand(time(NULL));


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

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


for(int i=0;i<100;i++) //pick one element at a time and conisder it as a place of interval
{
   count=0;
   for( int j=0;j<100;j++)
   {
     if(info[j]>=info[i] && info[j]<=(info[i]+50)) //Search for the interval elements lies
      count++;
   }
   if(count>maxCount)
   {
     maxCount=count;
     maxIndex=i;
   }
}

printf("The interval is to be placed at %d ",*info[maxIndex]);
return 0;
}

In the above program we pick each element of the array and find count of all other element which are falling between the eleement and element plus 50.We keep track of the maximum count for each element we visit.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote