Created
January 25, 2015 07:47
-
-
Save jukbot/c4238e55422d8bc7926a to your computer and use it in GitHub Desktop.
Cut array indexs and insert array
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
package arraylab; | |
public class Arraylab { | |
public static void main(String[] args) { | |
int arr[] = new int[100]; | |
// add 2^i to the array where 0 <= i <= 20 | |
// EX: arr[1,2,4,8,16,32,64,... 2^30] | |
int expo=1; | |
for (int i=0; i < 100; i++) { | |
arr[i] = expo; | |
expo *= 2; | |
} | |
printArr(arr); | |
System.out.println("Array size = " + arraySize(arr)); | |
System.out.println("Average array = " + averageArray(arr)); | |
insertArray(arr, (int)averageArray(arr)); | |
printArr(arr); | |
deleteArray(arr, 128); | |
printArr(arr); | |
} | |
public static void deleteArray(int [] a, int deleteValue){ | |
int i=0; | |
for (i=0; a[i]< deleteValue; i++) { //a[i] | |
if(a[i] == deleteValue) | |
} | |
int test = i; | |
for (i=test ; i< arraySize(a); i++) { | |
a[i] = a[i+1]; | |
} | |
} | |
// for (i=arraySize(a) && a[i]!=deleteValue; i++) // ถอยหลังไอ้สัส | |
public static void insertArray(int [] a, int value){ | |
int i=0; | |
for (i=arraySize(a)-1; a[i]>value; i--) // ถอยหลังไอ้สัส { | |
{ a[i] = a[i+1] = a[i]; } | |
a[i+1] = value; | |
} | |
public static double averageArray(int[] a) { | |
int sum =0; | |
for(int i=0; i < arraySize(a) ; i++) { | |
sum += a[i]; | |
} | |
return (sum*1.0)/arraySize(a); | |
} | |
public static void printArr(int[] a) { | |
for(int i=0; i < a.length ; i++) { | |
System.out.print(a[i] + " "); | |
} | |
System.out.println(); | |
} | |
public static int arraySize(int[] a) { | |
int size=0; | |
for(int i=0; a[i]>0; i++, size++); | |
return size; | |
} | |
} | |
//END OF CODE LINE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment