Write a Java program that gets a positive integer n randomly

Write a Java program that: gets a positive integer n, randomly generates a double array, x, of length n, outputs the contents of the array x, gets a positive integer rotation amount, r, creates a new array y with the items of x moved forward by r positions, and outputs the contents of the array y. Elements that are rotated off the array will appear at the end. For example, suppose x contains the following items in sequence: 1 2 3 4 5 6 7 After rotating by 3, the elements in y will appear in this sequence: 4 5 6 7 1 2 3 Array x should be left unchanged by your program. Be sure to test your program with different array sizes and rotation amounts (see Sample Run, below). Sample Run (user input in color): What is the array size? 5.5 Invalid array size: not an integer What is the array size? 0 Invalid array size: not positive What is the array size? -10 Invalid array size: not positive What is the array size? 9 Array created and initialized. Before rotation: ============================== x[0]: 8.0 x[1]: 4.0 x[2]: 5.0 x[3]: 21.0 x[4]: 7.0 x[5]: 9.0 x[6]: 18.0 x[7]: 2.0 x[8]: 100.0 What is the rotation amount? 4.5 Invalid rotation amount: not an integer What is the rotation amount? 0 Invalid rotation amount: not positive What is the rotation amount? -2 Invalid rotation amount: not positive What is the rotation amount? 3 Array rotated into new array After rotation: ============================== y[0]: 21.0 y[1]: 7.0 y[2]: 9.0 y[3]: 18.0 y[4]: 2.0 y[5]: 100.0 y[6]: 8.0 y[7]: 4.0 y[8]: 5.0

Solution

/*This is a function which is used to to left Rotate array[] of size n by 1*/

void toleftRotatebyOne(int array[], int n);

/*This is a function to left rotate array[] of size n by elements r*/

void letsleftRotate(int array[], int r, int n)

{

  int i;

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

    toleftRotatebyOne(array, n);

}

void toleftRotatebyOne(int array[], int n)

{

  int i, temp;

  temp = array[0];

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

     array[i] = array[i+1];

  array[i] = temp;

}

/* This is a utility function to print the final array */

void toprintArray(int array[], int size)

{

  int i;

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

    printf(\"%d \", array[i]);

}

/* This is the main driver program to test above functions */

int main()

{

   int array[] = {5,6,7,8,9,10};

   letsleftRotate(array, 5, 7);

   toprintArray(array, 7);

   getchar();

   return 0;

}

Write a Java program that: gets a positive integer n, randomly generates a double array, x, of length n, outputs the contents of the array x, gets a positive in
Write a Java program that: gets a positive integer n, randomly generates a double array, x, of length n, outputs the contents of the array x, gets a positive in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site