Self calculated RGB values aren't matching with actual RGB values - java

I am working on a program where I take a portion of an image and calculate the average RGB of that image. When I calculate it I get totally different values as if I was to use the built in function. When I test my numbers and put them in an RGB color chart they are off while the built in function is very accurate. The problem with using the built in function is that it has literally no functionality. It just prints out what I calculate but I can't use that data. link to function called dump()
public void readSquares(Mat img){
int width = 20;
int height = 20;
int rSum = 0;
int gSum = 0;
int bSum = 0;
int rAvg = 0;
int gAvg = 0;
int bAvg = 0;
Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2RGB);
int channels = img.channels();
int totalBytes = (int)(img.total() * img.channels());
byte buff[] = new byte[totalBytes];
img.get(0, 0, buff);
for (int i=0; i< height; i++) {
//stride is the number of bytes in a row of smallImg
int stride = channels * width;
for (int j=0; j<stride; j+=channels) {
int r = buff[(i * stride) + j];
int g = buff[(i * stride) + j + 1];
int b = buff[(i * stride) + j + 2];
if(r < 0 || g < 0 || b < 0){
r = Math.abs(r);
g = Math.abs(g);
b = Math.abs(b);
}
rSum += r;
gSum += g;
bSum += b;
}
}
rAvg = (int) (rSum / img.total()); //total pixels in picture
gAvg = (int) (gSum / img.total());
bAvg = (int) (bSum / img.total());
System.out.println("R: " + rAvg);
System.out.println("G: " + gAvg);
System.out.println("B: " + bAvg);
This below prints out the accurate values but I can't get the separate channels or find the average RGB it just spits everything out as a string which I can't utilize.
System.out.println(img.dump());
}
Here is an example; I read Red image for pixels this was the output:
My Calculated
R:18
G:27
B:64
The function
The first number represents the R second the G and third the B. It does that for every pixel in the image.
[237, 24, 60, 236, 23, 59, 236, 26, 56, 234, 25, 55, 238, 27, 62, 238, 27, 62, 238, 27, 62, 236, 25, 60, 234, 24, 59, 233, 23, 57, 234, 24, 59, 234, 24, 59, 235, 22, 59, 235, 22, 59, 237, 21, 59, 235, 20, 58, 237, 19, 56, 237, 19, 56, 239, 19, 52, 237, 18, 51;
236, 24, 55, 237, 25, 56, 237, 25, 54, 237, 25, 54, 236, 26, 56, 237, 27, 57, 238, 28, 58, 239, 29, 59, 238, 27, 62, 234, 24, 59, 237, 24, 62, 237, 24, 62, 238, 22, 63, 237, 20, 61, 235, 20, 58, 235, 20, 58, 237, 19, 58, 239, 21, 61, 237, 19, 56, 236, 18, 55;
238, 26, 57, 238, 26, 57, 234, 25, 53, 232, 23, 50, 236, 26, 54, 237, 27, 55, 237, 27, 55, 237, 27, 55, 237, 26, 61, 236, 25, 60, 237, 24, 62, 237, 24, 62, 238, 22, 63, 237, 20, 61, 238, 22, 63, 238, 22, 63, 238, 20, 61, 238, 20, 61, 237, 19, 56, 236, 18, 55;
242, 30, 60, 240, 27, 58, 235, 24, 49, 233, 21, 47, 234, 22, 48, 237, 26, 52, 236, 25, 50, 237, 26, 52, 238, 26, 57, 237, 25, 56, 239, 23, 62, 239, 23, 62, 238, 22, 63, 238, 22, 63, 238, 19, 63, 239, 20, 65, 238, 20, 61, 239, 21, 63, 238, 20, 59, 236, 18, 57;
240, 27, 62, 236, 23, 59, 236, 24, 53, 236, 24, 53, 235, 23, 51, 234, 22, 50, 235, 23, 51, 236, 24, 53, 237, 24, 60, 236, 23, 59, 237, 24, 64, 237, 24, 64, 237, 24, 64, 236, 22, 63, 238, 22, 63, 238, 22, 63, 237, 20, 61, 237, 20, 61, 237, 21, 59, 235, 20, 58;
238, 25, 61, 237, 24, 60, 237, 24, 60, 237, 24, 60, 235, 22, 57, 236, 23, 59, 235, 22, 57, 235, 22, 57, 234, 21, 56, 236, 23, 59, 237, 24, 62, 236, 23, 61, 237, 20, 61, 237, 20, 61, 238, 21, 65, 238, 21, 65, 238, 22, 63, 237, 20, 61, 235, 22, 59, 235, 22, 59;
238, 27, 62, 237, 26, 61, 237, 24, 60, 236, 23, 59, 236, 24, 55, 236, 24, 55, 236, 24, 55, 234, 22, 52, 237, 22, 53, 237, 22, 53, 237, 21, 57, 237, 21, 57, 237, 21, 59, 237, 21, 59, 237, 20, 61, 237, 20, 61, 236, 22, 63, 236, 22, 63, 236, 22, 63, 235, 21, 61;
237, 26, 63, 236, 25, 62, 237, 24, 62, 237, 24, 62, 238, 25, 63, 237, 24, 62, 237, 21, 57, 238, 22, 59, 237, 21, 57, 237, 21, 57, 235, 20, 56, 235, 20, 56, 235, 20, 56, 237, 21, 57, 237, 21, 57, 238, 22, 59, 235, 23, 53, 236, 24, 55, 236, 24, 53, 236, 24, 53;
236, 29, 69, 235, 28, 68, 238, 26, 70, 239, 27, 71, 240, 25, 68, 238, 24, 67, 237, 24, 64, 236, 22, 63, 237, 20, 61, 235, 19, 60, 237, 19, 58, 236, 18, 57, 237, 21, 59, 237, 21, 59, 238, 22, 59, 240, 25, 61, 237, 25, 56, 236, 24, 55, 236, 24, 53, 237, 25, 54;
235, 28, 66, 235, 28, 66, 237, 25, 65, 240, 29, 68, 241, 27, 67, 238, 25, 65, 239, 23, 64, 238, 22, 63, 237, 19, 58, 237, 19, 58, 237, 17, 57, 237, 17, 57, 240, 20, 57, 241, 21, 59, 238, 21, 53, 239, 22, 55, 240, 26, 55, 238, 23, 53, 237, 22, 51, 237, 22, 51;
237, 25, 69, 237, 25, 69, 239, 27, 73, 239, 27, 73, 240, 25, 70, 241, 26, 71, 239, 22, 66, 237, 20, 63, 238, 20, 61, 237, 18, 60, 236, 17, 59, 237, 18, 60, 238, 20, 59, 239, 21, 61, 238, 22, 59, 238, 22, 59, 239, 25, 54, 237, 22, 51, 237, 23, 49, 235, 22, 48;
237, 25, 65, 238, 27, 66, 238, 27, 66, 238, 27, 66, 238, 27, 66, 237, 25, 65, 236, 22, 63, 235, 21, 61, 235, 22, 59, 235, 22, 59, 234, 18, 57, 237, 21, 59, 238, 22, 61, 238, 22, 61, 238, 22, 59, 238, 22, 59, 238, 20, 57, 238, 20, 57, 237, 20, 52, 237, 20, 52;
236, 25, 60, 236, 25, 60, 238, 27, 62, 239, 29, 63, 237, 26, 61, 236, 25, 60, 237, 26, 61, 236, 25, 60, 236, 23, 59, 235, 22, 57, 236, 23, 59, 235, 22, 57, 237, 24, 60, 236, 23, 59, 238, 22, 59, 237, 21, 57, 237, 21, 57, 235, 20, 56, 236, 18, 55, 235, 17, 54;
237, 25, 56, 238, 26, 57, 237, 27, 57, 237, 27, 57, 236, 26, 56, 237, 27, 57, 238, 28, 58, 237, 27, 57, 237, 27, 57, 238, 28, 58, 240, 27, 58, 237, 25, 56, 236, 23, 59, 236, 23, 59, 237, 21, 57, 237, 21, 57, 235, 20, 58, 235, 20, 58, 236, 18, 55, 236, 18, 55;
237, 25, 54, 237, 25, 54, 237, 25, 54, 235, 23, 51, 237, 25, 56, 240, 27, 58, 237, 26, 61, 236, 25, 60, 237, 26, 63, 237, 26, 63, 238, 27, 66, 236, 24, 64, 237, 24, 64, 236, 22, 63, 237, 20, 61, 235, 19, 60, 236, 17, 59, 236, 17, 59, 236, 18, 57, 235, 16, 56;
237, 25, 56, 237, 25, 56, 236, 23, 59, 238, 25, 61, 237, 24, 62, 238, 25, 63, 237, 24, 64, 238, 25, 65, 240, 25, 68, 238, 24, 67, 237, 23, 66, 237, 23, 66, 238, 21, 65, 238, 21, 65, 237, 20, 63, 237, 20, 63, 235, 19, 62, 235, 19, 62, 235, 19, 60, 235, 19, 60;
237, 25, 56, 237, 25, 56, 237, 24, 60, 237, 24, 60, 237, 24, 64, 234, 20, 60, 237, 23, 66, 238, 24, 67, 238, 24, 69, 237, 23, 68, 236, 23, 70, 234, 22, 69, 236, 22, 67, 233, 18, 63, 236, 22, 65, 235, 21, 63, 236, 22, 63, 234, 20, 60, 235, 22, 59, 235, 22, 59;
238, 27, 55, 238, 27, 55, 239, 24, 56, 241, 27, 58, 237, 24, 62, 237, 24, 62, 237, 23, 66, 238, 24, 67, 237, 24, 71, 237, 24, 71, 236, 23, 72, 234, 22, 71, 231, 19, 65, 236, 23, 70, 236, 24, 68, 236, 24, 68, 237, 24, 62, 236, 23, 61, 236, 24, 55, 238, 26, 57;
239, 29, 63, 236, 25, 60, 236, 25, 62, 236, 25, 62, 236, 24, 64, 237, 25, 65, 234, 27, 67, 232, 26, 66, 238, 31, 74, 236, 29, 71, 234, 26, 71, 231, 24, 69, 232, 25, 70, 234, 26, 71, 236, 29, 71, 238, 31, 74, 235, 28, 68, 232, 26, 66, 237, 26, 63, 236, 25, 62;
237, 24, 60, 237, 24, 60, 237, 26, 61, 236, 25, 60, 233, 22, 59, 234, 24, 61, 232, 26, 62, 234, 28, 63, 234, 27, 65, 234, 27, 65, 234, 26, 69, 234, 26, 69, 234, 26, 73, 234, 26, 73, 237, 29, 79, 238, 30, 80, 236, 29, 71, 234, 26, 69, 237, 26, 63, 238, 27, 64]

Strictly speaking, the Java Language Specification states that a byte data type has the range −128 - 127 and that's how Java will interpret a byte. If for instance a byte is promoted or cast to an int Java will interpret the first bit as a sign and use sign extension. There's no language support to help you interpret a byte differently. (There's, for instance, no unsigned keyword in Java.)
That being said, nothing prevents you from viewing a byte as an 8-bit value and interpret those bits as unsigned. Just keep in mind that there's nothing you can do to force your interpretation upon someone else's method. If a method accepts a byte, then that method accepts a value between −128 and 127 unless explicitly stated otherwise.
public static int unsignedToBytes(byte b) {
return b & 0xFF;
}
This method does what you asked. Hope this helps !!!
Edit: There are plenty of good answers on Stack Overflow and elsewhere. I would recommend you to try finding the answer before posting the question. Thanks.

Related

converting a base64 decoded string to list in python does not produce same result as in Java array output

I have a base64 encoded string pTWlzYwVk74RHlbhrHtYxjlmTpa1KY3LVj3X8o3PHUURfY07Qnk5wFPHP7SHDvoJSaM24DybXt20+ou3evsEmLNQfzsF2A1lfSsG2dIKf5Gmhb1qXVN7C6z1mJIRTWt99ei9A1Ozyc7et2DpKpX0SGIaKPcmf2TomYvt1Q+YWTaabUoue9BgI2VHb3L2f/UdRo5ja6beSeA= forexample, In Java when decoding this base64 decoded I get the correct result but in python different result. Here is the code I used.
python code
decoded = base64.b64decode("pTWlzYwVk74RHlbhrHtYxjlmTpa1KY3LVj3X8o3PHUURfY07Qnk5wFPHP7SHDvoJSaM24DybXt20+ou3evsEmLNQfzsF2A1lfSsG2dIKf5Gmhb1qXVN7C6z1mJIRTWt99ei9A1Ozyc7et2DpKpX0SGIaKPcmf2TomYvt1Q+YWTaabUoue9BgI2VHb3L2f/UdRo5ja6beSeA=")
mylist = list(decoded )
print("mylist", mylist)
output becomes
mylist [165, 53, 165, 205, 140, 21, 147, 190, 17, 30, 86, 225, 172, 123, 88, 198, 57, 102, 78, 150, 181, 41, 141, 203, 86, 61, 215, 242, 141, 207, 29, 69, 17, 125, 141, 59, 66, 121, 57, 192, 83, 199, 63, 180, 135, 14, 250, 9, 73, 163, 54, 224, 60, 155, 94, 221, 180, 250, 139, 183, 122, 251, 4, 152, 179, 80, 127, 59, 5, 216, 13, 101, 125, 43, 6, 217, 210, 10, 127, 145, 166, 133, 189, 106, 93, 83, 123, 11, 172, 245, 152, 146, 17, 77, 107, 125, 245, 232, 189, 3, 83, 179, 201, 206, 222, 183, 96, 233, 42, 149, 244, 72, 98, 26, 40, 247, 38, 127, 100, 232, 153, 139, 237, 213, 15, 152, 89, 54, 154, 109, 74, 46, 123, 208, 96, 35, 101, 71, 111, 114, 246, 127, 245, 29, 70, 142, 99, 107, 166, 222, 73, 224]
and the Java side I have used like this
String str = "pTWlzYwVk74RHlbhrHtYxjlmTpa1KY3LVj3X8o3PHUURfY07Qnk5wFPHP7SHDvoJSaM24DybXt20+ou3evsEmLNQfzsF2A1lfSsG2dIKf5Gmhb1qXVN7C6z1mJIRTWt99ei9A1Ozyc7et2DpKpX0SGIaKPcmf2TomYvt1Q+YWTaabUoue9BgI2VHb3L2f/UdRo5ja6beSeA="
byte[] decoded = Base64.getDecoder().decode(str.getBytes(StandardCharsets.UTF_8));
System.out.println("myarray: "+Arrays.toString(decoded));
and output becomes
[-91, 53, -91, -51, -116, 21, -109, -66, 17, 30, 86, -31, -84, 123, 88, -58, 57, 102, 78, -106, -75, 41, -115, -53, 86, 61, -41, -14, -115, -49, 29, 69, 17, 125, -115, 59, 66, 121, 57, -64, 83, -57, 63, -76, -121, 14, -6, 9, 73, -93, 54, -32, 60, -101, 94, -35, -76, -6, -117, -73, 122, -5, 4, -104, -77, 80, 127, 59, 5, -40, 13, 101, 125, 43, 6, -39, -46, 10, 127, -111, -90, -123, -67, 106, 93, 83, 123, 11, -84, -11, -104, -110, 17, 77, 107, 125, -11, -24, -67, 3, 83, -77, -55, -50, -34, -73, 96, -23, 42, -107, -12, 72, 98, 26, 40, -9, 38, 127, 100, -24, -103, -117, -19, -43, 15, -104, 89, 54, -102, 109, 74, 46, 123, -48, 96, 35, 101, 71, 111, 114, -10, 127, -11, 29, 70, -114, 99, 107, -90, -34, 73, -32]
in comparison, the positive values of the two output is same but negatives the python version is outputting different result. why is that? what am I doing wrong? I saw a similar issue on here on stackoverflow, but Unfortunately does not resolve my issue.
Those result are same.
In java byte is signed type, so values bigger than 127 are considered negatives.
Apparently in python byte is unsigned.

delay timer in gwt on quick sort

I'm writing a quick sort algorithm in GWT, between each iteration I'm trying to print the current status.
This is the method
private int partition(int[] list_of_numbers, int first, int last) {
int pivot = list_of_numbers[first];
int up = first;
int down = last;
do {
while ((up < last) && pivot >= list_of_numbers[up]) {
up++;
}
while (pivot < list_of_numbers[down]) {
down--;
}
if (up < down) {
swap(list_of_numbers, up, down);
sortedResult.setText( array_to_string(list_of_numbers));
logger.log(Level.SEVERE, sortedResult.getText());
}
} while (up < down);
swap(list_of_numbers, first, down);
sortedResult.setText( array_to_string(list_of_numbers));
logger.log(Level.SEVERE, sortedResult.getText());
return down;
}
It works really well and we can see from the logger that it puts out the following
30, 3, 23, 7, 77, 46, 62, 91, 89, 22, 48, 96, 32, 40, 95,
30, 3, 23, 7, 22, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
22, 3, 23, 7, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
22, 3, 7, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
7, 3, 22, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
3, 7, 22, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
3, 7, 22, 23, 30, 46, 40, 91, 89, 77, 48, 96, 32, 62, 95,
3, 7, 22, 23, 30, 46, 40, 32, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 62, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 62, 77, 48, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 62, 48, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 95, 96,
but on the output to the screen it will only display the last iteration
I would like to put a delay of 1 second after each sort where the user can see the current array.
I'm not sure how to use the timer in this case. As well I don't know if it is changing the value each time so fast that I only see the last one or if it's only changing it at the end.
A quick google search yields a page guiding you through delaying calculations and updating the interface.
You can find it here.

Efficiently compute the hashCode for a BitSet-like implementation of Set<Integer>

I wonder, how to efficiently compute the hashCode for a BitSet-like implementation of Set<Integer>.
The BitSet#hashCode is obviously fast to compute, rather stupid(*) and incompatible with Set#hashCode().
A fast compatible implementation could go like
int hashCode() {
int result = 0;
for (int i=0; i<bits.length; ++i) {
long word = bits[i];
result += 64 * i * Long.bitCount(word) + weightedBitCount(word);
}
return result;
}
if there was an efficient implementation of
int weightedBitCount(long word) { // naive implementation
int result = 0;
for (int i=0; i<64; ++i) {
if ((word & (1L << i)) != 0) {
result += i;
}
}
return result;
}
In case most bits are unset, the naive implementation could be improved by testing word==0 or using Long.highestOneBit or alike, but these tricks don't help much and are detrimental in other cases.
Is there a clever trick to significantly speed it up in general?
I guess, some batch computation over multiple words could be more efficient. Computing Set#size at the same time would be a nice bonus.
A note concerning premature optimization: Yes, I know. I'm mostly curious (and it can be useful for things like Project Euler).
(*) There are many bits which gets completely ignored (they get shifted out in the multiplication).
Notwithstanding hardware support (e.g., the x86 popcnt instruction), counting the bits in O(1) time is a rather well-known algorithm, generally communicated as SWAR bitcount.
However, your algorithm has a custom kernel that adds a different value based on which bit is set:
result += loop_counter_value;
Lacking a pithy algorithm for bit counting with custom kernels, a tried and true methodology is to utilize precalculated results. In this context, a lookup table. Clearly, a lookup table of all combinations of 64 bits (264 combinations!) is unwieldy, but you can split the difference by precalculating each byte of the n-byte variable. For 8 bytes, this is 256*8, or 2KiB of memory. Consider:
int weightedBitCount(long word) {
int result = 0;
int[][] lookups = {
{0, 0, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 10, 10, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 11, 11, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 14, 14, 15, 15, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 10, 10, 11, 11, 12, 12, 10, 10, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 15, 15, 16, 16, 11, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 16, 16, 17, 17, 15, 15, 16, 16, 17, 17, 18, 18, 18, 18, 19, 19, 20, 20, 21, 21, 7, 7, 8, 8, 9, 9, 10, 10, 10, 10, 11, 11, 12, 12, 13, 13, 11, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 16, 16, 17, 17, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 17, 17, 18, 18, 16, 16, 17, 17, 18, 18, 19, 19, 19, 19, 20, 20, 21, 21, 22, 22, 13, 13, 14, 14, 15, 15, 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 17, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 22, 22, 23, 23, 18, 18, 19, 19, 20, 20, 21, 21, 21, 21, 22, 22, 23, 23, 24, 24, 22, 22, 23, 23, 24, 24, 25, 25, 25, 25, 26, 26, 27, 27, 28, 28},
{0, 8, 9, 17, 10, 18, 19, 27, 11, 19, 20, 28, 21, 29, 30, 38, 12, 20, 21, 29, 22, 30, 31, 39, 23, 31, 32, 40, 33, 41, 42, 50, 13, 21, 22, 30, 23, 31, 32, 40, 24, 32, 33, 41, 34, 42, 43, 51, 25, 33, 34, 42, 35, 43, 44, 52, 36, 44, 45, 53, 46, 54, 55, 63, 14, 22, 23, 31, 24, 32, 33, 41, 25, 33, 34, 42, 35, 43, 44, 52, 26, 34, 35, 43, 36, 44, 45, 53, 37, 45, 46, 54, 47, 55, 56, 64, 27, 35, 36, 44, 37, 45, 46, 54, 38, 46, 47, 55, 48, 56, 57, 65, 39, 47, 48, 56, 49, 57, 58, 66, 50, 58, 59, 67, 60, 68, 69, 77, 15, 23, 24, 32, 25, 33, 34, 42, 26, 34, 35, 43, 36, 44, 45, 53, 27, 35, 36, 44, 37, 45, 46, 54, 38, 46, 47, 55, 48, 56, 57, 65, 28, 36, 37, 45, 38, 46, 47, 55, 39, 47, 48, 56, 49, 57, 58, 66, 40, 48, 49, 57, 50, 58, 59, 67, 51, 59, 60, 68, 61, 69, 70, 78, 29, 37, 38, 46, 39, 47, 48, 56, 40, 48, 49, 57, 50, 58, 59, 67, 41, 49, 50, 58, 51, 59, 60, 68, 52, 60, 61, 69, 62, 70, 71, 79, 42, 50, 51, 59, 52, 60, 61, 69, 53, 61, 62, 70, 63, 71, 72, 80, 54, 62, 63, 71, 64, 72, 73, 81, 65, 73, 74, 82, 75, 83, 84, 92},
{0, 16, 17, 33, 18, 34, 35, 51, 19, 35, 36, 52, 37, 53, 54, 70, 20, 36, 37, 53, 38, 54, 55, 71, 39, 55, 56, 72, 57, 73, 74, 90, 21, 37, 38, 54, 39, 55, 56, 72, 40, 56, 57, 73, 58, 74, 75, 91, 41, 57, 58, 74, 59, 75, 76, 92, 60, 76, 77, 93, 78, 94, 95, 111, 22, 38, 39, 55, 40, 56, 57, 73, 41, 57, 58, 74, 59, 75, 76, 92, 42, 58, 59, 75, 60, 76, 77, 93, 61, 77, 78, 94, 79, 95, 96, 112, 43, 59, 60, 76, 61, 77, 78, 94, 62, 78, 79, 95, 80, 96, 97, 113, 63, 79, 80, 96, 81, 97, 98, 114, 82, 98, 99, 115, 100, 116, 117, 133, 23, 39, 40, 56, 41, 57, 58, 74, 42, 58, 59, 75, 60, 76, 77, 93, 43, 59, 60, 76, 61, 77, 78, 94, 62, 78, 79, 95, 80, 96, 97, 113, 44, 60, 61, 77, 62, 78, 79, 95, 63, 79, 80, 96, 81, 97, 98, 114, 64, 80, 81, 97, 82, 98, 99, 115, 83, 99, 100, 116, 101, 117, 118, 134, 45, 61, 62, 78, 63, 79, 80, 96, 64, 80, 81, 97, 82, 98, 99, 115, 65, 81, 82, 98, 83, 99, 100, 116, 84, 100, 101, 117, 102, 118, 119, 135, 66, 82, 83, 99, 84, 100, 101, 117, 85, 101, 102, 118, 103, 119, 120, 136, 86, 102, 103, 119, 104, 120, 121, 137, 105, 121, 122, 138, 123, 139, 140, 156},
{0, 24, 25, 49, 26, 50, 51, 75, 27, 51, 52, 76, 53, 77, 78, 102, 28, 52, 53, 77, 54, 78, 79, 103, 55, 79, 80, 104, 81, 105, 106, 130, 29, 53, 54, 78, 55, 79, 80, 104, 56, 80, 81, 105, 82, 106, 107, 131, 57, 81, 82, 106, 83, 107, 108, 132, 84, 108, 109, 133, 110, 134, 135, 159, 30, 54, 55, 79, 56, 80, 81, 105, 57, 81, 82, 106, 83, 107, 108, 132, 58, 82, 83, 107, 84, 108, 109, 133, 85, 109, 110, 134, 111, 135, 136, 160, 59, 83, 84, 108, 85, 109, 110, 134, 86, 110, 111, 135, 112, 136, 137, 161, 87, 111, 112, 136, 113, 137, 138, 162, 114, 138, 139, 163, 140, 164, 165, 189, 31, 55, 56, 80, 57, 81, 82, 106, 58, 82, 83, 107, 84, 108, 109, 133, 59, 83, 84, 108, 85, 109, 110, 134, 86, 110, 111, 135, 112, 136, 137, 161, 60, 84, 85, 109, 86, 110, 111, 135, 87, 111, 112, 136, 113, 137, 138, 162, 88, 112, 113, 137, 114, 138, 139, 163, 115, 139, 140, 164, 141, 165, 166, 190, 61, 85, 86, 110, 87, 111, 112, 136, 88, 112, 113, 137, 114, 138, 139, 163, 89, 113, 114, 138, 115, 139, 140, 164, 116, 140, 141, 165, 142, 166, 167, 191, 90, 114, 115, 139, 116, 140, 141, 165, 117, 141, 142, 166, 143, 167, 168, 192, 118, 142, 143, 167, 144, 168, 169, 193, 145, 169, 170, 194, 171, 195, 196, 220},
{0, 32, 33, 65, 34, 66, 67, 99, 35, 67, 68, 100, 69, 101, 102, 134, 36, 68, 69, 101, 70, 102, 103, 135, 71, 103, 104, 136, 105, 137, 138, 170, 37, 69, 70, 102, 71, 103, 104, 136, 72, 104, 105, 137, 106, 138, 139, 171, 73, 105, 106, 138, 107, 139, 140, 172, 108, 140, 141, 173, 142, 174, 175, 207, 38, 70, 71, 103, 72, 104, 105, 137, 73, 105, 106, 138, 107, 139, 140, 172, 74, 106, 107, 139, 108, 140, 141, 173, 109, 141, 142, 174, 143, 175, 176, 208, 75, 107, 108, 140, 109, 141, 142, 174, 110, 142, 143, 175, 144, 176, 177, 209, 111, 143, 144, 176, 145, 177, 178, 210, 146, 178, 179, 211, 180, 212, 213, 245, 39, 71, 72, 104, 73, 105, 106, 138, 74, 106, 107, 139, 108, 140, 141, 173, 75, 107, 108, 140, 109, 141, 142, 174, 110, 142, 143, 175, 144, 176, 177, 209, 76, 108, 109, 141, 110, 142, 143, 175, 111, 143, 144, 176, 145, 177, 178, 210, 112, 144, 145, 177, 146, 178, 179, 211, 147, 179, 180, 212, 181, 213, 214, 246, 77, 109, 110, 142, 111, 143, 144, 176, 112, 144, 145, 177, 146, 178, 179, 211, 113, 145, 146, 178, 147, 179, 180, 212, 148, 180, 181, 213, 182, 214, 215, 247, 114, 146, 147, 179, 148, 180, 181, 213, 149, 181, 182, 214, 183, 215, 216, 248, 150, 182, 183, 215, 184, 216, 217, 249, 185, 217, 218, 250, 219, 251, 252, 284},
{0, 40, 41, 81, 42, 82, 83, 123, 43, 83, 84, 124, 85, 125, 126, 166, 44, 84, 85, 125, 86, 126, 127, 167, 87, 127, 128, 168, 129, 169, 170, 210, 45, 85, 86, 126, 87, 127, 128, 168, 88, 128, 129, 169, 130, 170, 171, 211, 89, 129, 130, 170, 131, 171, 172, 212, 132, 172, 173, 213, 174, 214, 215, 255, 46, 86, 87, 127, 88, 128, 129, 169, 89, 129, 130, 170, 131, 171, 172, 212, 90, 130, 131, 171, 132, 172, 173, 213, 133, 173, 174, 214, 175, 215, 216, 256, 91, 131, 132, 172, 133, 173, 174, 214, 134, 174, 175, 215, 176, 216, 217, 257, 135, 175, 176, 216, 177, 217, 218, 258, 178, 218, 219, 259, 220, 260, 261, 301, 47, 87, 88, 128, 89, 129, 130, 170, 90, 130, 131, 171, 132, 172, 173, 213, 91, 131, 132, 172, 133, 173, 174, 214, 134, 174, 175, 215, 176, 216, 217, 257, 92, 132, 133, 173, 134, 174, 175, 215, 135, 175, 176, 216, 177, 217, 218, 258, 136, 176, 177, 217, 178, 218, 219, 259, 179, 219, 220, 260, 221, 261, 262, 302, 93, 133, 134, 174, 135, 175, 176, 216, 136, 176, 177, 217, 178, 218, 219, 259, 137, 177, 178, 218, 179, 219, 220, 260, 180, 220, 221, 261, 222, 262, 263, 303, 138, 178, 179, 219, 180, 220, 221, 261, 181, 221, 222, 262, 223, 263, 264, 304, 182, 222, 223, 263, 224, 264, 265, 305, 225, 265, 266, 306, 267, 307, 308, 348},
{0, 48, 49, 97, 50, 98, 99, 147, 51, 99, 100, 148, 101, 149, 150, 198, 52, 100, 101, 149, 102, 150, 151, 199, 103, 151, 152, 200, 153, 201, 202, 250, 53, 101, 102, 150, 103, 151, 152, 200, 104, 152, 153, 201, 154, 202, 203, 251, 105, 153, 154, 202, 155, 203, 204, 252, 156, 204, 205, 253, 206, 254, 255, 303, 54, 102, 103, 151, 104, 152, 153, 201, 105, 153, 154, 202, 155, 203, 204, 252, 106, 154, 155, 203, 156, 204, 205, 253, 157, 205, 206, 254, 207, 255, 256, 304, 107, 155, 156, 204, 157, 205, 206, 254, 158, 206, 207, 255, 208, 256, 257, 305, 159, 207, 208, 256, 209, 257, 258, 306, 210, 258, 259, 307, 260, 308, 309, 357, 55, 103, 104, 152, 105, 153, 154, 202, 106, 154, 155, 203, 156, 204, 205, 253, 107, 155, 156, 204, 157, 205, 206, 254, 158, 206, 207, 255, 208, 256, 257, 305, 108, 156, 157, 205, 158, 206, 207, 255, 159, 207, 208, 256, 209, 257, 258, 306, 160, 208, 209, 257, 210, 258, 259, 307, 211, 259, 260, 308, 261, 309, 310, 358, 109, 157, 158, 206, 159, 207, 208, 256, 160, 208, 209, 257, 210, 258, 259, 307, 161, 209, 210, 258, 211, 259, 260, 308, 212, 260, 261, 309, 262, 310, 311, 359, 162, 210, 211, 259, 212, 260, 261, 309, 213, 261, 262, 310, 263, 311, 312, 360, 214, 262, 263, 311, 264, 312, 313, 361, 265, 313, 314, 362, 315, 363, 364, 412},
{0, 56, 57, 113, 58, 114, 115, 171, 59, 115, 116, 172, 117, 173, 174, 230, 60, 116, 117, 173, 118, 174, 175, 231, 119, 175, 176, 232, 177, 233, 234, 290, 61, 117, 118, 174, 119, 175, 176, 232, 120, 176, 177, 233, 178, 234, 235, 291, 121, 177, 178, 234, 179, 235, 236, 292, 180, 236, 237, 293, 238, 294, 295, 351, 62, 118, 119, 175, 120, 176, 177, 233, 121, 177, 178, 234, 179, 235, 236, 292, 122, 178, 179, 235, 180, 236, 237, 293, 181, 237, 238, 294, 239, 295, 296, 352, 123, 179, 180, 236, 181, 237, 238, 294, 182, 238, 239, 295, 240, 296, 297, 353, 183, 239, 240, 296, 241, 297, 298, 354, 242, 298, 299, 355, 300, 356, 357, 413, 63, 119, 120, 176, 121, 177, 178, 234, 122, 178, 179, 235, 180, 236, 237, 293, 123, 179, 180, 236, 181, 237, 238, 294, 182, 238, 239, 295, 240, 296, 297, 353, 124, 180, 181, 237, 182, 238, 239, 295, 183, 239, 240, 296, 241, 297, 298, 354, 184, 240, 241, 297, 242, 298, 299, 355, 243, 299, 300, 356, 301, 357, 358, 414, 125, 181, 182, 238, 183, 239, 240, 296, 184, 240, 241, 297, 242, 298, 299, 355, 185, 241, 242, 298, 243, 299, 300, 356, 244, 300, 301, 357, 302, 358, 359, 415, 186, 242, 243, 299, 244, 300, 301, 357, 245, 301, 302, 358, 303, 359, 360, 416, 246, 302, 303, 359, 304, 360, 361, 417, 305, 361, 362, 418, 363, 419, 420, 476}
};
for (int bite = 0; bite < 8; bite++)
result += lookups[bite][ (int)(word >> (bite * 8)) & 0xff ];
return result;
}
You might clean that up a bit, move the initialization out of the function, and so on, but this crucially removes the branch from your loop, and reduces your best case of 128 instructions (for all zeros) to 56 instructions (rough numbers). The worst case is a bit more pronounced at 192 instructions to 56. Further, a smart compiler might unroll the loop entirely, reducing to 40 instructions.
I think is also important to have less hash collisions together with the hashing performance. Faster hashing calculation can make your program generally slower, because of big amount of hash misses.
It mind be a better idea to use some generic hash function like MurMur3A from Google Guava, instead of inventing your own.
There are many Benchmark about hashing, for example:
http://greenrobot.org/essentials/features/performant-hash-functions-for-java/comparison-of-hash-functions/
https://www.strchr.com/hash_functions
https://github.com/Cyan4973/xxHash
I think you can do some micro-benchmarking using a Google Caliper and check which hash function is better for you case.
BTW. Ask your self why do you need a custom BitSet ?
This is what I did:
int weightedBitCount(long word) {
return (Long.bitCount(word & 0xFFFF_FFFF_0000_0000L) << 5)
+ (Long.bitCount(word & 0xFFFF_0000_FFFF_0000L) << 4)
+ (Long.bitCount(word & 0xFF00_FF00_FF00_FF00L) << 3)
+ (Long.bitCount(word & 0xF0F0_F0F0_F0F0_F0F0L) << 2)
+ (Long.bitCount(word & 0xCCCC_CCCC_CCCC_CCCCL) << 1)
+ (Long.bitCount(word & 0xAAAA_AAAA_AAAA_AAAAL) << 0);
}
It's pretty simple: With a single bit set, e.g., the bit 10, word looks like 0x0000_0000_0000_0400L and only the masks 0xFF00_FF00_FF00_FF00L and 0xCCCC_CCCC_CCCC_CCCCL produce a bit count of 1, so we get
(0 << 5) + (0 << 4) + (1 << 3) + (0 << 2) + (1 << 1) + (0 << 5) = 10
It needs some 6*4 instructions (maybe 6 cycles on modern Intel) per 64 bits, so it's not really slow, but it's still too slow when compared with the bulk bitset operations which need a single instruction (per 64 bits).
So I'm playing with some batch computation over multiple words.

If loop, error in macro

Hi I am trying to make a macro for imagej.
Now I have as input a vector Array1y about 300 elements. If I would like to subdivide the vector in subvectors, how can i do? If for example I want 100 subvectors, so a step of 30, I was thinking to something like that
w=30;
step= Array1y.length/w
for (i=0; i<Array1y.length; i+=(w+1))
{
for (j=w; j<Array1y.length ; j+=w)
a(i)= Array.slice(Array1y,i,w)
Array.print a(i)
}
}
what's wrong?
Can it work if I have no 300 elements but less, let's say 268 and I want as well a step of 30 (so I have the last subvector that is not made by 30 elements?)
I'm not sure what you're trying to do, or if it is actually java, but here's what I think you asked for :
public static void main(String[] args) {
int[] vector = new int[268];
IntStream.range(0, vector.length).forEach(i -> vector[i] = i); // to fill the vector
int subvectorSize = 30;
int[][] subvectors = new int[vector.length/subvectorSize+1][subvectorSize]; // array of arrays
for(int i=0; i< vector.length; i++){ // iterate over vector
int x = i/subvectorSize; // get quotient (xth subvector)
int y = i%subvectorSize; // get reminder (index y of subvector)
subvectors[x][y] = vector[i];
}
System.out.println(Arrays.deepToString(subvectors));
}
That outputs :
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119], [120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149], [150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179], [180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209], [210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239], [240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 0, 0]]

Java: Searching an unsorted integer array for the first occurrence of a user inputted integer

I've noticed two problem and would really appreciate the help!
With (what I believe) to be with how the methods "arrayIndex" and "position" are interacting with each other. I say this because when I enter the same exact numbers for both of the fields they return the same exact values even though they are supposed to handle completely different tasks.
With my "position" method which is supposed to return the location of the searched values first occurrence in the array it seems to return incorrect values and after inspecting it I still can not figure out why. If the user searches for a values position that doesn't exist it's supposed to return a "-1".
My Entire Code:
import java.util.Scanner;
public class Prog9ArrayMethods {
public static void main(String[] args) {
// Daily high temperatures for Portland Maine Jan 1 - Dec 31 2015
int[] tmax = {32, 38, 34, 35, 41, 17, 25, 17, 29, 24, 26, 33, 31, 24,
29, 38, 20, 49, 49, 36, 31, 38, 35, 32, 37, 20, 17, 26,
30, 32, 22, 26, 12, 20, 35, 34, 19, 28, 22, 15, 30, 23,
20, 17, 16, 19, 21, 21, 32, 33, 19, 34, 35, 31, 19, 34,
21, 27, 27, 30, 36, 32, 46, 39, 23, 38, 40, 44, 47, 56,
41, 39, 38, 36, 45, 44, 28, 32, 34, 36, 35, 34, 39, 42,
49, 49, 41, 41, 40, 48, 45, 46, 66, 49, 48, 41, 47, 42,
35, 43, 54, 68, 66, 70, 65, 55, 67, 55, 57, 48, 63, 60,
53, 54, 55, 56, 58, 63, 57, 60, 55, 54, 62, 76, 75, 72,
84, 58, 59, 83, 68, 82, 64, 68, 70, 63, 74, 61, 65, 67,
69, 67, 65, 83, 84, 91, 79, 80, 77, 84, 73, 51, 50, 61,
60, 58, 73, 67, 65, 68, 81, 86, 80, 85, 78, 61, 61, 75,
72, 80, 69, 72, 72, 67, 82, 78, 67, 70, 59, 69, 75, 68,
78, 80, 71, 82, 82, 76, 84, 72, 84, 87, 90, 78, 76, 82,
76, 74, 70, 81, 84, 70, 82, 78, 76, 67, 67, 77, 83, 88,
86, 86, 86, 81, 81, 80, 82, 80, 76, 80, 77, 77, 67, 80,
77, 80, 85, 85, 89, 86, 83, 75, 73, 78, 70, 79, 75, 80,
79, 77, 75, 81, 86, 80, 84, 86, 72, 78, 82, 92, 89, 86,
78, 73, 74, 62, 73, 83, 85, 82, 83, 75, 72, 69, 65, 74,
74, 63, 63, 67, 74, 75, 69, 62, 55, 58, 58, 61, 69, 67,
63, 59, 56, 68, 70, 62, 68, 57, 61, 57, 46, 48, 66, 58,
65, 54, 47, 62, 54, 52, 59, 73, 58, 51, 58, 64, 64, 64,
68, 69, 65, 53, 58, 53, 47, 53, 60, 46, 53, 54, 47, 47,
53, 59, 46, 42, 42, 42, 41, 51, 61, 57, 41, 32, 38, 44,
45, 47, 51, 51, 57, 39, 45, 53, 48, 57, 47, 48, 56, 42,
50, 46, 40, 38, 47, 49, 47, 51, 62, 51, 43, 34, 23, 28,
44};
int max = arrayMax(tmax);
int min = arrayMin(tmax);
double average = arrayAverage(tmax);
int count = arrayIndex(tmax);
int i = arrayIndex(tmax);
System.out.println("Maximum value is: " + max);
System.out.println("Minimum value is: " + min);
System.out.println("Average value is: " + average);
System.out.println("The number of values above the specified value is: " + count);
System.out.println("The first occurence of the searched value is: " + i);
}
// Returns the maximum value in the array
public static int arrayMax(int[] a) {
int max = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] > max)
max = a[i];
return max;
}
// Returns the minimum value in the array
public static int arrayMin (int[] a) {
int min = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] < min)
min = a[i];
return min;
}
// Returns the average value in the array
public static double arrayAverage(int[] a) {
int sum = 0; // Why does it double the decimal value with "int sum = a[0]"?
double average;
for(int i=0; i < a.length; i++){
sum = sum + a[i];
}
average = (double)sum/a.length;
return average;
}
// Returns the number of values greater than the user's indexed values
public static int arrayIndex(int[] a) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
int userSearch = user_input.nextInt();
int count = 0;
for(int i = 0; i < a.length; i++) {
if(a[i] > userSearch) {
++count;
}
}
return count;
}
public static int position(int [ ] a, int match) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
int userSearch = user_input.nextInt();
for (int i = 0; i < a.length; i++)
{
if ( a[i] == userSearch )
return i;
}
return -1;
}
}
In your main method, when you want to get i to be the count, you actually get arrayIndex() instead of position().
Just change int i = arrayIndex(tmax); to int i = position(tmax); and get rid of the int match() as an argument to position()
Here is the solution for your problem. Your were calling arrayIndex method twice instead of calling position method. I rectified your code. Now it works fine.
import java.util.Scanner;
public class Prog9ArrayMethods {
public static void main(String[] args) {
// Daily high temperatures for Portland Maine Jan 1 - Dec 31 2015
int[] tmax = {32, 38, 34, 35, 41, 17, 25, 17, 29, 24, 26, 33, 31, 24,
29, 38, 20, 49, 49, 36, 31, 38, 35, 32, 37, 20, 17, 26,
30, 32, 22, 26, 12, 20, 35, 34, 19, 28, 22, 15, 30, 23,
20, 17, 16, 19, 21, 21, 32, 33, 19, 34, 35, 31, 19, 34,
21, 27, 27, 30, 36, 32, 46, 39, 23, 38, 40, 44, 47, 56,
41, 39, 38, 36, 45, 44, 28, 32, 34, 36, 35, 34, 39, 42,
49, 49, 41, 41, 40, 48, 45, 46, 66, 49, 48, 41, 47, 42,
35, 43, 54, 68, 66, 70, 65, 55, 67, 55, 57, 48, 63, 60,
53, 54, 55, 56, 58, 63, 57, 60, 55, 54, 62, 76, 75, 72,
84, 58, 59, 83, 68, 82, 64, 68, 70, 63, 74, 61, 65, 67,
69, 67, 65, 83, 84, 91, 79, 80, 77, 84, 73, 51, 50, 61,
60, 58, 73, 67, 65, 68, 81, 86, 80, 85, 78, 61, 61, 75,
72, 80, 69, 72, 72, 67, 82, 78, 67, 70, 59, 69, 75, 68,
78, 80, 71, 82, 82, 76, 84, 72, 84, 87, 90, 78, 76, 82,
76, 74, 70, 81, 84, 70, 82, 78, 76, 67, 67, 77, 83, 88,
86, 86, 86, 81, 81, 80, 82, 80, 76, 80, 77, 77, 67, 80,
77, 80, 85, 85, 89, 86, 83, 75, 73, 78, 70, 79, 75, 80,
79, 77, 75, 81, 86, 80, 84, 86, 72, 78, 82, 92, 89, 86,
78, 73, 74, 62, 73, 83, 85, 82, 83, 75, 72, 69, 65, 74,
74, 63, 63, 67, 74, 75, 69, 62, 55, 58, 58, 61, 69, 67,
63, 59, 56, 68, 70, 62, 68, 57, 61, 57, 46, 48, 66, 58,
65, 54, 47, 62, 54, 52, 59, 73, 58, 51, 58, 64, 64, 64,
68, 69, 65, 53, 58, 53, 47, 53, 60, 46, 53, 54, 47, 47,
53, 59, 46, 42, 42, 42, 41, 51, 61, 57, 41, 32, 38, 44,
45, 47, 51, 51, 57, 39, 45, 53, 48, 57, 47, 48, 56, 42,
50, 46, 40, 38, 47, 49, 47, 51, 62, 51, 43, 34, 23, 28,
44};
int max = arrayMax(tmax);
int min = arrayMin(tmax);
double average = arrayAverage(tmax);
int count = arrayIndex(tmax);
int i = position(tmax);
System.out.println("Maximum value is: " + max);
System.out.println("Minimum value is: " + min);
System.out.println("Average value is: " + average);
System.out.println("The number of values above the specified value is: " + count);
System.out.println("The first occurence of the searched value is: " + i);
}
// Returns the maximum value in the array
public static int arrayMax(int[] a) {
int max = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] > max)
max = a[i];
return max;
}
// Returns the minimum value in the array
public static int arrayMin (int[] a) {
int min = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] < min)
min = a[i];
return min;
}
// Returns the average value in the array
public static double arrayAverage(int[] a) {
int sum = 0; // Why does it double the decimal value with "int sum = a[0]"?
double average;
for(int i=0; i < a.length; i++){
sum = sum + a[i];
}
average = (double)sum/a.length;
return average;
}
// Returns the number of values greater than the user's indexed values
public static int arrayIndex(int[] a) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search for arrayIndex: ");
int userSearch = user_input.nextInt();
int count = 0;
for(int i = 0; i < a.length; i++) {
if(a[i] > userSearch) {
++count;
}
}
return count;
}
public static int position(int [ ] a) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
int userSearch = user_input.nextInt();
for (int i = 0; i < a.length; i++)
{
if ( a[i] == userSearch )
return i;
}
return -1;
}
}

Categories