I need to find the max and min for every 3 elements of a data using java array.
For double data[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14} , what is the max and min in every 3 elements? In other words, what is the max and min for {1,2,3}, {4, 5, 6}, {7, 8, 9}, etc.
I have the following method, but it is not getting the right results.
for (int i = 0; i < data.length; i+=3) {
for (int r = i; r < i + 3 && r < data.length; r++) {
if (data[r] < lowest) { lowest = data[r]; }
if (data[r] > highest) { highest = data[r]; }
}
lowest = highest;
highest = 0;
How about this?
int[] data = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int processed = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < data.length; i++) {
if (data[i] < min) {
min = data[i];
}
if (data[i] > max) {
max = data[i];
}
processed++;
if (processed == 3) {
System.out.println("Min is: " + min);
System.out.println("Max is: " + max);
System.out.println("--------");
processed = 0;
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
}
}
Output:
Min is: 1
Max is: 3
--------
Min is: 4
Max is: 6
--------
Min is: 7
Max is: 9
--------
Min is: 10
Max is: 12
--------
It's unclear what to do with the numbers 13 and 14, as they don't form a block of 3, but I guess you get the idea of the algorithm.
Related
I am trying to create my own program to do selection sort on an array of integers. I have come up with the following program, which works on some arrays, but not on others, such as this one. I have been trying to trace the problem, and I think it might have to do with where I am placing the min = num [x]; line. However, I am not sure where I should move it in order to fix the problem. Does anyone have any suggestions? Thank you.
p.s. I have provided some of my test cases and their results at the bottom.
int [] num = {4, 9, 7, 6, 0, 1, 3, 5, 8, 2};
int min = num [0];
int temp;
int index = 0;
for (int x = 0; x < num.length; x ++)
{
min = num [x];
temp = num [x];
for (int y = x + 1; y < num.length; y ++)
{
if (num [y] < min)
{
min = num [y];
index = y;
}
}
num [x] = min;
num [index] = temp;
min = num [x];
}
Output Test Cases:
array: {4, 9, 7, 6, 0, 1, 3, 5, 8, 2}
result: [0, 1, 2, 3, 4, 4, 5, 7, 8, 8]
array: {7, 5, 8, 9, 1, 6, 3, 0, 2, 4}
result: [0, 1, 2, 3, 4, 5, 6, 7, 7, 8]
array: {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
result: [0, 1, 2, 3, 4, 9, 6, 7, 8, 9]
there is one big problem and multiple small ones, see comments:
int [] num = {4, 9, 7, 6, 0, 1, 3, 5, 8, 2};
int min = num [0]; // you don't need this variable and assignment here, you can declare it inside loop
int temp; // the same for this variable
int index = 0; // the same here
for (int x = 0; x < num.length; x ++)
{
min = num [x];
temp = num [x];
// here you need to re-initialize index variable to some value, otherwise it could be some garbage from previous iterations
for (int y = x + 1; y < num.length; y ++)
{
if (num [y] < min)
{
min = num [y];
index = y;
}
}
num [x] = min;
num [index] = temp;
min = num [x]; // this assignment does not make sense as you're overwriting it next iteration
}
you can simplify code a little bit:
for (int x = 0; x < num.length; x++) {
int index = x;
for (int y = x + 1; y < num.length; y++)
if (num[y] < num[index])
index = y;
int temp = num[x];
num[x] = num[index];
num[index] = temp;
}
Need to add all numbers in an array that is greater than an inputted number. The seed is just so the output can be replicated.
Example:
[12,16,45,3,32]
Inputted Value: 30
Output:
77
import java.util.*;
public class SumAbove {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int seed = scnr.nextInt();
Random rand = new Random(seed);
System.out.println("Enter a positive integer between 1-100 to search above:");
int minVal = scnr.nextInt();
int[] arr = new int[rand.nextInt(100)+1];
for (int i=0; i<arr.length; i++) {
arr[i] = rand.nextInt(100)+1;
}
System.out.println(Arrays.toString(arr));
int sum = 0;
for (int i=0; i<arr.length; i++) {
if (arr[i]>minVal) {
sum += i;
}
}
System.out.println(sum);
}
}
Instead of sum += i; you want sum += arr[i]; (as already noted), you also only need one loop (since you know the minimum before the first loop). Like,
int minVal = scnr.nextInt(), sum = 0;
int[] arr = new int[rand.nextInt(100) + 1];
for (int i = 0; i < arr.length; i++) {
arr[i] = rand.nextInt(100) + 1;
if (arr[i] > minVal) {
sum += arr[i];
}
}
System.out.println(Arrays.toString(arr));
System.out.println(sum);
Replace sum += i with sum += arr[i].
The variable i is just the position. arr[i] is the value at that position.
public static void main(String[] args) {
int nums[] = { 12, 16, 45, 3, 32 };
int value;
int sum = 0;
System.out.println("Enter a positive integer between 1-100 to search above: ");
Scanner sc = new Scanner(System.in);
value = sc.nextInt();
for (int i = 0; i < nums.length; i++) {
if (nums[i] > value)
sum = nums[i] + sum;
}
System.out.println(sum);
}
Here is how you can do it with streams in Java 8+.
int nValues = 5;
int minValue = 1;
int maxValue = 30;
Random r = new Random();
for (int i = 0; i < 10; i++) {
int[] values = r.ints(nValues, minValue, maxValue + 1).toArray();
// min to sum is the threshold
int minToSum = r.nextInt(7) + 10; // between 10 an 16 inclusive
int sum = Arrays.stream(values).filter(m -> m > minToSum).sum();
System.out.println("sum = " + sum + " for greater than " + minToSum
+ " : " + Arrays.toString(values));
}
The following output.
sum = 65 for values greater than 11 : [2, 10, 14, 23, 28]
sum = 92 for values greater than 10 : [13, 18, 15, 19, 27]
sum = 94 for values greater than 12 : [25, 6, 14, 25, 30]
sum = 54 for values greater than 10 : [14, 8, 14, 26, 5]
sum = 22 for values greater than 15 : [15, 8, 13, 22, 14]
sum = 28 for values greater than 13 : [3, 28, 9, 6, 5]
sum = 87 for values greater than 13 : [5, 18, 25, 21, 23]
sum = 31 for values greater than 13 : [16, 7, 12, 2, 15]
sum = 42 for values greater than 15 : [7, 22, 20, 10, 5]
sum = 40 for values greater than 12 : [2, 2, 13, 27, 9]
Currently working on an algorithm that replaces every element with the next greatest element, but unlike some other questions here, this one is not concerned with replacing values with -1 if no such number exists, and it must be in ascending order.
Given this input: {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 }
Have to get this output: 1 5 5 5 8 8 8 10 10 11 11
This is what I have so far:
class Playground {
static void nextGreatest(int arr[]) {
int size = arr.length;
// Initialize the next greatest element
int max_from_right = arr[size - 1];
// Replace all other elements with the next greatest
for (int i = 1; i < size; i++)
{
// Store the current element (needed later for
// updating the next greatest element)
int temp = arr[i];
// Replace current element with the next greatest
arr[i] = max_from_right;
// Update the greatest element, if needed
if(max_from_right < temp) {
max_from_right = temp;
}
}
}
// prints the array
static void printArray(int arr[])
{
for (int i=0; i < arr.length; i++)
System.out.print(arr[i]+" ");
}
public static void main (String[] args) {
int arr[] = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2 };
nextGreatest (arr);
printArray (arr);
}
}
And I get the following right now:
1 2 5 5 5 8 8 8 10 10 11
Thoughts?
Thanks
(Not sure I understand your question exactly, but based on the clarification from the comments here is my answer...)
Seems you just need to change the initial max initialization to the first element rather than the last element.
int currentMax = arr[0];
for (int i = 1; i < size; i++) {
int temp = arr[i];
arr[i] = currentMax;
if(currentMax < temp) {
currentMax = temp;
}
}
The solution ends up as For each index i, what is the maximum element seen so far.
Consider the following:
int currMax = -1;
int[] input = {1, 5, -3, 2, 8, 4, 7, 10, 3, 11, 2};
for (int i = 0; i < input.length; i++){
if (input[i] > currMax){ // if we find a new max element
currMax = input[i];
}
else if (input[i] < currMax){ // if value is less then max we replace it
input[i] = currMax;
}
}
System.out.println(Arrays.toString(input));
> [1, 5, 5, 5, 8, 8, 8, 10, 10, 11, 11]
i just created an array have 100 elements, so now i want get 10 elements print first, secondly i want continuing 10 elements print out, and 10 elements third keep going. My code is below:
int[] array = { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,..., 100 };
int count = 0;
for (int i = 0; i < array.length; i++) {
count++;
if (count == 10) {
System.out.println(array[i]);
count = 0;
}
}
Your logic is correct, only issue is with the printing of array values.
int[] array = { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,....,100};
int count = 0;
for (int i = 0; i < array.length; i++) {
count++;
System.out.print(array[i]+" ");
if (count == 10) {
System.out.println();
count = 0;
}
}
This can be done fairly easily by iterating through the array with a double for-loop.
One loop will iterate through the modulo index and the other will print the 10 elements with that modulo value.
int[] array = {1,2,3,4,5,6,7,8,9,10,...,100};
for (int offset = 0; offset < array.length/10; offset++)
for (int i = 0 + offset; i < array.length; i+= array.length/10)
System.out.println(array[i]);
Based on this answer I've tried to do my roulette wheel selection in genetic algorithm.
private static final int NUMBER_OF_TOURISTS = 20;
private static int[] roulette(int population[]) {
int sumProb = 0;
for (int i = 0; i < population.length; i++) {
sumProb += population[i];
}
int[] rouletteIndex = new int[NUMBER_OF_TOURISTS];
Random r = new Random();
for (int i = 0; i < NUMBER_OF_TOURISTS; i++) {
int numberRand = r.nextInt(sumProb);
//-------------------------------------------------------
int j = 0;
while (numberRand > 0) {
numberRand = numberRand - population[j];
j++;
}
rouletteIndex[i] = j-1;
//-------------------------------------------------------
}
return rouletteIndex;
}
after this I get:
[6, 2, -1, 19, 13, 2, 14, 2, 6, 19, 7, 14, 18, 0, 1, 9, 13, 10, 7, 2]
"-1"? But how, when j should be always greater than 0.
Is this happen when numberRand = 0 and than while loop doesn't start even once? But how to fix this?
Random.nextInt(int bound) returns 0 (inclusive) to specified bound (exclusive).
So your loop:
while (numberRand > 0) {
numberRand = numberRand - population[j];
j++;
}
Will not run if nextInt(int bound) returns 0, resulting in j being 0 at: rouletteIndex[i] = j-1;