Question #57096

Hello, I need help with writing a program in arrays using these following methods:
1- Sorting
2- Searching
3- Filling randomly
4- Min, Max, Sum, Avg
5- Remove Repeat
6- Count all,element
7- reverse
8-printing
9- return array
10-Menu

Expert's answer

import java.util.Random;
public class CustomArrays {
//sorting array arr using bubble sort
public static void sort(int[] arr) {
int swap;
for (int i = 0; i < arr.length; i++)
for (int j = 1; j < (arr.length - i); j++) {
if (arr[j-1] > arr[j]) /* ascending order, for descending order use < */ {
swap = arr[j-1];
arr[j-1] = arr[j];
arr[j] = swap;
}
}
}
//returns the first index of 'value' in 'arr', -1 if 'arr' contains no 'value'
public static int search(int[] arr, int value) {
int result = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
result = i;
break;
}
}
return result;
}
//fills 'arr' with random numbers (min<=random number<max)
public static void randomFill(int[] arr, int min, int max) {
Random rand = new Random();
for (int i = 0; i < arr.length; i++)
arr[i] = rand.nextInt(min + max) - min;
}
//returns minimal value in 'arr'
public static int min(int[] arr) {
int result = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < result) result = arr[i];
}
return result;
}
//returns maximal value in 'arr'
public static int max(int[] arr) {
int result = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > result) result = arr[i];
}
return result;
}
//returns the sum of all elements in 'arr'
public static int sum(int[] arr) {
int result = 0;
for (int i = 0; i < arr.length; i++) result += arr[i];
return result;
}
CustomArrays.txt[12/22/2015 11:49:14 AM]
//returns the average value of elements in 'arr' (rounded average for integers)
public static int avg(int[] arr) {
int result = 0;
for (int i = 0; i < arr.length; i++) result += arr[i];
return result/arr.length;
}
//removes repeated elements, substituting them with zeros
public static void removeRepeat(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if ((arr[i] != 0) && (arr[i] == arr[j])) arr[j] = 0;
}
}
}
//returns number of elements in 'arr'
public static int countElements(int[] arr) {
return arr.length;
}
//reverses elements of 'arr'
public static void reverse(int[] arr) {
int swap;
for (int i = 0; i < arr.length/2; i++) {
swap = arr[i];
arr[i] = arr[arr.length - (i + 1)];
arr[arr.length - (i + 1)] = swap;
}
}
//prints 'arr'
public static String printArray(int[] arr) {
String result = new String();
for (int i = 0; i < arr.length - 1; i++)
result += arr[i] + ", ";
result += arr[arr.length - 1];
return result;
}
//makes a copy of 'arr'
public int[] returnArray(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++)
result[i] = arr[i];
return result;
}
//prints methods defined in CustomArrays
public static void printMethods() {
System.out.println("1. sort");
System.out.println("2. search");
System.out.println("3. randomFill");
System.out.println("4. min");
System.out.println("5. max");
System.out.println("6. sum");
System.out.println("7. avg");
System.out.println("8. removeRepeat");
System.out.println("9. countElements");
System.out.println("10. reverse");
}
CustomArrays.txt[12/22/2015 11:49:14 AM]
System.out.println("11. printArray");
System.out.println("12. returnArray");
System.out.println("13. printMethods");
}
public static void main(String[] args) {
CustomArrays.printMethods();
int[] arr = new int[20];
CustomArrays.randomFill(arr, 0, 100);
System.out.println(CustomArrays.printArray(arr));
CustomArrays.sort(arr);
System.out.println(CustomArrays.printArray(arr));
System.out.println(CustomArrays.min(arr));
System.out.println(CustomArrays.max(arr));
System.out.println(CustomArrays.sum(arr));
System.out.println(CustomArrays.avg(arr));
CustomArrays.removeRepeat(arr);
System.out.println(CustomArrays.printArray(arr));
System.out.println(CustomArrays.countElements(arr));
CustomArrays.reverse(arr);
System.out.println(CustomArrays.printArray(arr));
}


CustomArrays.txt[12/22/2015 11:49:14 AM]

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS