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

Write a method named minGap that accepts an integer array as a parameter and ret

ID: 3765585 • Letter: W

Question

Write a method named minGap that accepts an integer array as a parameter and returns the minimum 'gap' between adjacent values in the array. The gap between two adjacent values in a array is defined as the second value minus the first value. For example, suppose a variable called array is an array of integers that stores the following sequence of values:

The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth gap is 5 (12 - 7). Thus, the call of minGap(array) should return 1 because that is the smallest gap in the array. If you are passed an array with fewer than 2 elements, you should return 0.

Explanation / Answer

Program in C:

#include <iostream>
#include<stdio.h>

int minGap(int [],int);

int main()
{ int m,length,i;


printf("Enter the length of the array ");
scanf("%d",&length);

int a[length];

printf("Enter the elements ");

  
     for(i=0;i<length;i++)
   
     {scanf("%d",&a[i]);}
   
     m=minGap(a,length);
    printf("Minimum gap is : %d",m);
  
    return 0;
}
int minGap(int a[],int length)
{
    int g,min;
    min=a[1]-a[0];

if(length<2)
{return 0;}

else{
    for(int i=1;i<length;i++)
    {
            g=(a[i+1]-a[i]);
          
            if(min>g){
                      min=g;
                      }}
                    
    }   return min;             
}

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