Question

In: Computer Science

Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[],...

Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[], int size); 2. Write a function to generate an array of random integers. void random_array (int arr[], int size, int range); The scale of numbers should be 1 to range. 3. Write a function to find the sum of a sequence of number. int sum (int arr[], int size); 4. Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. Example: Rotation of the above array by 2 will make array. In your main function, call the above functions:  Declare an array of size that input by user.  Initialize this array with random values. (task 2)  Display this array. (task 1)  Display the sum of the numbers in the array. (task 3)  Ask user for number of elements to rotate ‘d’.  Call the user defined function named ‘rotate’. (task 4)  Display the array again.

Solutions

Expert Solution

import java.util.Scanner;

import java.util.concurrent.ThreadLocalRandom;

public class Array {

   public static void main(String[] args) {

       Scanner s = new Scanner(System.in);
       System.out.println("Enter the size of the array");
       int size = s.nextInt();
       int arr[] = new int[size];
       System.out.println("Enter the range of randomization for the array");
       int range = s.nextInt();
       random_array(arr, size, range);
       print_array(arr, size);
       int sum = sum(arr, size);
       System.out.println("The sum of the values of the above array is " + sum);
   }

   static void print_array(int arr[], int size){
       for (int i=0; i<size;i++){
           System.out.print(arr[i] + " ");
       }
       System.out.println();
   }

   static void random_array(int arr[], int size, int range){
       int random = 0;
       for (int i=0; i<size; i++){
           random = ThreadLocalRandom.current().nextInt(1, range+1);
           arr[i] = random;
       }
   }

   static int sum (int arr[], int size){
       int sum = 0;
       for (int i=0; i<size;i++){
           sum += arr[i];
       }
       return sum;
      
   }
}

P. S. The description of the fourth function (rotation) is not clear and the example is incomplete. Please give more details in comments and I'll work on it :)


Related Solutions

C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that generates and stores a specified number of random numbers each of whose values are between 0 and 1000 in an array
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
3. Array Operations 3-1 Write a function, equalsArray that when passed two int arrays of the...
3. Array Operations 3-1 Write a function, equalsArray that when passed two int arrays of the same length that is greater than 0 will return true if every number in the first array is equal to the number at the same index in the second array. If the length of the arrays is less than 1 the function must return false. For example, comparing the two arrays, {1,2,3,4,5} and {1,2,3,4,5} would return true but the two arrays {3,7} and {3,6}...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT