Created
February 22, 2019 16:08
-
-
Save somejavadev/9c122452dfe2f4b6a90e94ff912f017c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
System.out.print("Unsorted Random Array: "); | |
Random x = new Random(); | |
int [] arr = new int [10]; | |
for (int a = 0; a <= 7; a++) { | |
arr[a] = x.nextInt(5); // Change bound from 4 -> 5 since it is exclusive. | |
if(a<7){ | |
System.out.print(arr[a]+", "); | |
} | |
else if (a>=7){ | |
System.out.println(arr[a]); | |
} | |
} | |
//Call new method | |
addFirstFor(arr); | |
} | |
/** | |
* Method that accepts the array as an argument | |
* | |
* @param arr | |
*/ | |
public static void addFirstFor(int[] arr) { | |
int sum = 0; | |
for(int x = 0; x < 4; x++) { | |
sum = sum + arr[x]; | |
} | |
arr[4] =sum; //Add the sum to the fourth index | |
System.out.println("Added: " + sum + " to index 4."); | |
System.out.println(Arrays.toString(arr)); // Use Arrays.toString to print a readable Array | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment