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

This is the starting point for the Java code... public class NumberWork { public

ID: 3630980 • Letter: T

Question

This is the starting point for the Java code...

public class NumberWork {

public static void main(String[] args) {
conNumbers(9, 3, 2);
conNumbers(1, 2, 3);
conNumbers(5, 4, 3);
conNumbers(-2, 0, -1);
conNumbers(-1, 10, 5);
}
public static void conNumbers(String a, String b, String c){


Starting under the method conNumbers, write a method that accepts the three numbers given in the test cases and returns whether they are consecutive or not. The order doesn't matter, so long as the numbers if taken out of the code could be arranged in a consecutive order. The method should return with true or false answers to whether each test contains consecutive numbers, for example the output for these tests should look like this...

false
true
true
true
false

Explanation / Answer

Another solution is to loop through the items and count consecutive items:

#include<stdlib.h>

  

/* Helper functions to get minimum and maximum in an array */

int getMin(int arr[], int n);

int getMax(int arr[], int n);

  

/* The function checks if the array elements are consecutive

  If elements are consecutive, then returns true, else returns

  false */

bool areConsecutive(int arr[], int n)

{

  if ( n < 1 )

    return false;

  

  /* 1) Get the minimum element in array */

  int min = getMin(arr, n);

  

  /* 2) Get the maximum element in array */

  int max = getMax(arr, n);

  

  /* 3) max - min + 1 is equal to n, then only check all elements */

  if (max - min + 1 == n)

{

      /* Create a temp array to hold visited flag of all elements.

         Note that, calloc is used here so that all values are initialized

         as false */

      bool *visited = (bool *)calloc(sizeof(bool), n);

      int i;

      for (i = 0; i < n; i++)

      {

         /* If we see an element again, then return false */

         if ( visited[arr[i] - min] != false )

           return false;

  

         /* If visited first time, then mark the element as visited */

         visited[arr[i] - min] = true;

      }

  

      /* If all elements occur once, then return true */

      return true;

  }

  

  return false; // if (max - min + 1 != n)

}

  

/* UTILITY FUNCTIONS */

int getMin(int arr[], int n)

{

  int min = arr[0];

  for (int i = 1; i < n; i++)

   if (arr[i] < min)

     min = arr[i];

  return min;

}

  

int getMax(int arr[], int n)

{

  int max = arr[0];

  for (int i = 1; i < n; i++)

   if (arr[i] > max)

     max = arr[i];

  return max;

}

  

/* Driver program to test above functions */

int main()

{

    int arr[]= {5, 4, 2, 3, 1, 6};

    int n = sizeof(arr)/sizeof(arr[0]);

    if(areConsecutive(arr, n) == true)

        printf(" Array elements are consecutive ");

    else

        printf(" Array elements are not consecutive ");

    getchar();

    return 0;

}

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