Created
May 24, 2014 06:06
-
-
Save agungsijawir/e8505125a0d29d4f7ede to your computer and use it in GitHub Desktop.
Sequence Search - Find value, highest, lowest and count them.
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
/* | |
* Author: [email protected] | |
* | |
*/ | |
public class SequenceCase02 { | |
public static void main(String[] args) { | |
int lariknya[] = new int[]{11, 12, 10, 15, 6, 10, 13, 7, 14, 6, 15}; | |
int a = 0; | |
int a_size = lariknya.length; | |
int cari; | |
boolean ketemu = false; | |
cari = 13; // tells what number to search | |
while ((!ketemu) && (a < a_size)) { | |
if (cari == lariknya[a]) | |
ketemu = true; | |
else | |
a++; | |
} | |
System.out.print("Array value: "); | |
for (int cc = 0; cc < a_size - 1; cc++) | |
System.out.print(lariknya[cc] + ", "); | |
System.out.println(lariknya[a_size-1]); | |
if (ketemu == true) | |
System.out.println("Bilangan '" + cari + "' ada pada indeks ke-" + a); | |
else | |
System.out.println("Bilangan '" + cari + "' tidak ada di dalam list."); | |
System.out.println("Bilangan '10' ada sebanyak " + getCountMember(lariknya, 10)); | |
System.out.println("Nilai suku terbesar adalah " + getHighestValue(lariknya)); | |
System.out.println("Nilai suku terkecil adalah " + getLowestValue(lariknya)); | |
System.out.println("Jumlah suku bilangan terbesar ada " + getCountHighestValue(lariknya)); | |
System.out.println("Jumlah suku bilangan terkecil ada " + getCountLowestValue(lariknya)); | |
} | |
/* | |
* Find how many whatNumber appears in given array | |
* | |
* INT counter <- 0 | |
* INT WHATNUMBER <- 13 | |
* | |
* FOR (INT P <- 0; P < sizeof(array); P++) DO | |
* IF (array[P] == WHATNUMBER) THEN | |
* counting += 1; | |
* END IF | |
* END FOR | |
* | |
* RETURN counter | |
*/ | |
private static int getCountMember(int arrayNya[], int whatNumber) { | |
int counter = 0; | |
for (int p = 0; p < arrayNya.length; p++) { | |
if (arrayNya[p] == whatNumber) | |
counter++; | |
} | |
return counter; | |
} | |
/* | |
* Find lowest value in given array | |
* | |
* INT lowest <- array[0] | |
* | |
* FOR (INT P <- 0; P < sizeof(array); P++) DO | |
* IF (array[P] < lowest) THEN | |
* lowest = array[P] | |
* END IF | |
* END FOR | |
* | |
* RETURN lowest | |
*/ | |
private static int getLowestValue(int arrayNya[]) { | |
int lowest = arrayNya[0]; | |
for (int p = 1; p < arrayNya.length; p++) { | |
if (arrayNya[p] < lowest) | |
lowest = arrayNya[p]; | |
} | |
return lowest; | |
} | |
/* | |
* Find higest value in given array | |
* | |
* INT highest <- 0 | |
* | |
* FOR (INT P <- 0; P < sizeof(array); P++) DO | |
* IF (array[P] > highest) THEN | |
* highest = array[P] | |
* END IF | |
* END FOR | |
* | |
* RETURN highest | |
*/ | |
private static int getHighestValue(int arrayNya[]) { | |
int highest = 0; // init lowest value | |
for (int p = 0; p < arrayNya.length; p++) { | |
if (arrayNya[p] > highest) | |
highest = arrayNya[p]; | |
} | |
return highest; | |
} | |
/* | |
* Get how many lowest value in given array | |
* | |
* INT counter <- 0 | |
* | |
* FOR (INT P <- 0; P < sizeof(array); P++) DO | |
* IF (array[P] == getLowestValue(array) THEN | |
* counter += 1 | |
* END IF | |
* END FOR | |
* | |
* RETURN counter | |
*/ | |
private static int getCountLowestValue(int arrayNya[]) { | |
int counter = 0; | |
for (int p = 0; p < arrayNya.length; p++) { | |
if (arrayNya[p] == getLowestValue(arrayNya)) | |
counter++; | |
} | |
return counter; | |
} | |
/* | |
* Get how many highest value in given array | |
* | |
* INT counter <- 0 | |
* | |
* FOR (INT P <- 0; P < sizeof(array); P++) DO | |
* IF (array[P] == getHighestValue(array) THEN | |
* counter += 1 | |
* END IF | |
* END FOR | |
* | |
* RETURN counter | |
*/ | |
private static int getCountHighestValue(int arrayNya[]) { | |
int counter = 0; | |
for (int p = 0; p < arrayNya.length; p++) { | |
if (arrayNya[p] == getHighestValue(arrayNya)) | |
counter++; | |
} | |
return counter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment