Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. Creating a method arrayModifier(int[] a) which takes an integer array of length n, where n > 5.  The method returns an integer array of length 10 where the first 1/2 of the array contains elements from the start of the input array and the second 1/2 of the array contains elements from the end of the input array.
  2. I'm not 100% sure how to start any help would be great !
  3.  
  4. My test case:
  5.  
  6.   int[] a = {4, 5, 6, 7, 8, 9};
  7.   System.out.println(Arrays.toString(arrayModifier(a)))
  8.  
  9. output:
  10.  
  11.   [4, 5, 6, 7, 8, 5, 6, 7, 8, 9]
  12.  
  13. Another test case:
  14.  
  15.   int[] a = {23, 43, 23, 56, 78, 95, 67, 24, 15, 28, 37, 48, 66};
  16.   System.out.println(Arrays.toString(arrayModifier(a)))
  17.  
  18. output:
  19.  
  20.   [23, 43, 23, 56, 78, 15, 28, 37, 48, 66]
  21.  
  22.  
  23. Code:
  24.  
  25. public static int[] arrayModifier(int[] a) {
  26.    int ctr = 0;
  27.    int i=0;
  28.    for(int i = 0;i<a.length;i++){
  29.        {
  30.            if(a[i] >= 5) {
  31.                a[i] = a[10];
  32.                ctr ++;
  33.            }
  34.        }
  35.    return ctr;
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement