Skip to content

Instantly share code, notes, and snippets.

@somejavadev
Created February 22, 2019 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somejavadev/9c122452dfe2f4b6a90e94ff912f017c to your computer and use it in GitHub Desktop.
Save somejavadev/9c122452dfe2f4b6a90e94ff912f017c to your computer and use it in GitHub Desktop.
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