I have trouble converting negative array elements into absolute. So far ive made array containing both negative and positive, but i have to convert them into absolute, in order to calculate array square root. Any suggestions?
public static void main(String[] args) {
int min = -100;
int max = 100;
int[] array = new int[201];
for (int i = 0; i < array.length; i++) {
array[i] = min + (int) (Math.random() * ((max - min) + 1));
//System.out.println(array[i]);
}
int[] array2 = new int[array.length];
for (int j = 2; j < array.length; j += 3) {
array2[j] = array[j];
//System.out.println(array[j]);
double[] result = new double[array2.length];
for (int i = 0; i < array2.length / 3; i++) {
array2[i] = Math.abs(i);
result[j] = Math.sqrt(array[j]);
System.out.println(array[i]);
}
your third-last line is wrong, you need:
result[j] = Math.sqrt(array2[j]);
You're calling Math.abs() on i (the index) not on the array element. Also, you are storing the result of abs into array2[i] and then calling Math.sqrt() on a different array and element (array[j]). Also check if your for loop indexes iterate the fields in the way you want (to me the for loop statements seem somewhat strange).
You have to clearly describe your intentions,
it is no clear that you want each third element of the array and not every one of them.
However there are multiple ways to achive the result.
You can create a List to store square root of each third element
List<Double> result1 = new ArrayList<>();
for (int j = 2; j < array.length; j += 3) {
result1.add(Math.sqrt(Math.abs(array[j])));
}
If you would like to use arrays make it like so
double[] result2 = new double[array.length / 3];
for (int j = 2; j < array.length; j += 3) {
result2[j / 3] = Math.sqrt(Math.abs(array[j]));
}
In both examples we call Math.abs to get the absolute value and Math.sqrt to get square root.
Here is the full code
public static void main(String args[])
{
int min = -100;
int max = 100;
int[] array = new int[201];
for (int i = 0; i < array.length; i++) {
array[i] = min + (int) (Math.random() * ((max - min) + 1));
}
List<Double> result1 = new ArrayList<>();
for (int j = 2; j < array.length; j += 3) {
result1.add(Math.sqrt(Math.abs(array[j])));
}
double[] result2 = new double[array.length / 3];
for (int j = 2; j < array.length; j += 3) {
result2[j / 3] = Math.sqrt(Math.abs(array[j]));
}
System.out.println(result1);
System.out.println(Arrays.toString(result2));
}
Related
I'm trying to figure out whats the most simple way to add to fill this 2D Array with only non duplicate numbers.
I've tried to use a method with boolean to check the value before adding it, if it already exists but I couldn't make it work.
int[][] x = new int[R][C];
for (int i = 0; i < x.length; i++) {
for (int j = 0; j < x[i].length; j++) {
double z = (Math.random() * ((30 * (j + 1)) - ((30 * j) + 1)) + 1 + ((30 * j) + 1));
card[i][j] = (int) z;
}
}
The best way i will say is to use Set data structure
int row = 2;
int col = 2;
Set<Set<Integer>> rowSet = new HashSet<>();
for (int i = 0; rowSet.size() < row; i++) {
Set<Integer> colSet = new HashSet<>();
for (int j = 0; colSet.size() < col; j++) {
double x = (Math.random() * ((15 * (j + 1)) - ((15 * j) + 1)) + 1 + ((15 * j) + 1));
colSet.add((int) x);
}
rowSet.add(colSet);
}
And finally convert them to array
int[][] arr = set.stream()
.map(i->i.stream()
.mapToInt(Integer::intValue)
.toArray())
.toArray(int[][]::new);
You definitely want to use Random.nextInt() to get a uniform distribution of your int. Most likely you want a Set to track which numbers were already generated.
int[][] cards = new int[ROW][COL];
Random r = new Random();
Set<Integer> generated = new HashSet<>();
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
int n;
do {
n = r.nextInt();
} while (generated.contains(n));
generated.add(n);
cards[i][j] = n;
}
}
My code is supposed to take the random number generated in the random method and sort them but it's only giving me one number.
My program is a random number generator that is supposed to make 1000 numbers that I can sort but my code only inserts one number into the array.
public static void main(String[] args) {
// write
int max = 1000;
int min=0;
int range = max - min + 1;
// generate random numbers within 1 to 10
for (int i = 0; i < 1000; i++) {
int rand = (int) (Math.random () * range) + min;
System.out.println ( rand );
int array[] = {rand};
int size = array.length;
for ( i = 0; i < size - 1; i++) {
int min1 = i;
for (int j = i + 1; j < size; j++) {
if (array[j] < array[min1]) {
min = j;
}
}
int temp = array[min1];
array[min1] = array[i];
array[i] = temp;
}
for (int k = 0; k < size; i++) {
System.out.print(" " + array[i]);
}
}
}
You need to break your program into separate steps:
Insert all the random numbers into the array
Sort the array
Print the contents of the array
Few problems I noticed:
Since you want to generate 1000 numbers from 1-10, max and min should have values of 10 and 1, respectively.
array should be declared before you start inserting values. It should also have a fixed size of 1000.
Your bubble sort algorithm also had some errors which led to incorrect output. If you wish to sort the array from greatest to least instead, simply change the > to < in the condition of the if statement.
I also decided to use Arrays.toString() to print the array instead of the loop.
public static void main(String[] args) {
int max = 10;
int min = 1;
int range = max - min + 1;
int size = 1000;
int[] array = new int[size];
for (int i = 0; i < size; i++) {
int rand = (int) (Math.random() * range + min);
array[i] = rand;
}
int temp = 0;
for (int i = 0; i < size; i++) {
for (int j = 1; j < size - i; j++) {
if (array[j - 1] > array[j]) {
temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
System.out.println(Arrays.toString(array));
}
your code will result an ArrayIndexOutException. below is the code change from your code ,i dont change too much so you can compare them and find your mistakes,wish good :D
public static void main(String[] args) {
int max = 1000;
int min=0;
int range = max - min + 1;
int[] array = new int[range];
// generate random numbers within 1 to 10
for (int i = 0; i < 1000; i++) {
int rand = (int) (Math.random () * range) + min;
array[i] = rand;
}
int size = array.length;
for (int i = 0; i < size; i++) {
int min1 = i;
for (int j = i + 1; j < size; j++) {
if (array[j] < array[min1]) {
min1 = j;//here min1
}
}
int temp = array[min1];
array[min1] = array[i];
array[i] = temp;
}
for (int k = 0; k < size; k++) {
System.out.print(" " + array[k]);
}
}
let me explain it more clearly,in the OP's code there has some questions,two majors:
one:
for ( i = 0; i < size - 1; i++) {
int min1 = i;
for (int j = i + 1; j < size; j++) {
if (array[j] < array[min1]) {
min = j;
}
}
int temp = array[min1];
array[min1] = array[i];
array[i] = temp;
}
will never run,because the array size is 1 ,so the for loop phrase will be ignore without running(mean for(int i = 0; i < 0; i++){....}).
two:
for (int k = 0; k < size; i++) {
System.out.print(" " + array[i]);
}
beacause the array size is 1,so when array[1] will throw index out exception.so the outermost loop will just run once then throw a exception.
:D
I know I have to do it with a while or do while loop, but I can't get it working. I also tried with a for loop, but it always gives me an error because I don't know the exact length of the vectors because they are random.
int a = (int)(Math.random() * 3 + 1);
int b = (int)(Math.random() * 3 + 1);
int c = a + b;
int[] arrA = new int[a];
int[] arrB = new int[b];
int[] arrC = new int[c];
for (int i = 0; i < a; i ++) {
arrA[i] = (int)(Math.random() * 10 + 1);
for (int j = 0; j < b; j ++) {
arrB[j] = (int)(Math.random() * 10 + 1);
}
}
Arrays.sort(arrA);
Arrays.sort(arrB);
System.out.println(Arrays.toString(arrA));
System.out.println(Arrays.toString(arrB));
System.out.println(Arrays.toString(arrC));
Take values from arrays arrA and arrB, and insert to arrC
int index = arrA.length;
for (int i = 0; i < arrA.length; i++) {
arrC[i] = arrA[i];
}
for (int i = 0; i < arrB.length; i++) {
arrC[i + index] = arrB[i];
}
Sort arrC
Arrays.sort(arrC);
Reverse the order and store in arrD
for(int l = 0; l < arrC.length; l++) {
arrD[l] = arrC[arrC.length - (l+1)];
}
Remove duplicate (simplified)
Set<Integer> remove=new LinkedHashSet<Integer>();
for(int i = 0;i < arrD.length;i++){
remove.add(arrD[i]);
}
Remove duplicate (usual)
int index2 = 0;
for (int i = 0; i < arrD.length; i++) {
for (int k = 0; k < arrD.length; k++) {
if (arrD[i] != arrD[k]) {
arrE[index2] = arrD[i];
index2++;
}
}
}
I have to make a program that extract every 3th element from array. So far i have made the basic array, but im stuck at extracting every 3th element into separate array. How can i do that?
public static void main(String[] args) {
int min = -100;
int max = 100;
int[] array = new int[201];
for( int i = 0; i < array.length; i++) {
array[i] = min + (int)(Math.random()*((max - min) + 1));
To fill in a new array (named array2) with every 3rd item from your array:
int[] array2 = new int[array.length / 3];
int k = 2;
for(int j = 0; j < array2.length; j++) {
array2[j] = array[k];
k += 3;
}
just make your for loop jump by three:
int[] newArray = new int[array.length / 3];
for (int i = 2 ; i < array.length ; i+=3) {
newArray[i/3] = array[i];
}
I've been battling with this for some time and seem to be getting nowhere. The set up is so; I have a 2D array. For this array I need to iterate through each value and return the diagonal neighbours (5 values). These neighbours will be put into a new 1D [5] array and bubblesorted. The middle value (median) will then be returned and put into a new array of medians.
So far I have methods for extracting the diagonal neighbours:
//get diagonals from original DEM
double [] getDiagonals(int i, int j) {
double [] tempArray = new double [5];
tempArray[0] = data[i -1][j +1];
tempArray[1] = data[i -1][j -1];
tempArray[2] = data[i][j];
tempArray[3] = data[i +1][j -1];
tempArray[4] = data[i +1][j +1];
return tempArray;
}
I've then used this method in an iteration to get diagonals for each value in the original array:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
//do something with the tempArray
I think this is where I'm coming unstuck. Through testing the getDiagonals method works fine. I'm struggling to get the tempArray out of the bubbles() method. If I set the output to be the tempArray it only returns the 5 values calculated for the bottom right corner of the original array.
I've tried calling other methods into the bubbles() method in order to do all the processing there and return a new array:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
double sorted [] = sort(tempArray);
double median = sorted[2];
for (int z = 0; z < datap.length; z++){
for (int y = 0; y < datap[z].length; y++){
datap[z][y] = median;
}
}
}
}
}
}
return datap;
}
Again this fails and the output datap is just zeros. The sort() method above passed out the diagonals to a bubble sort method (which I know works on its
I guess my question is how to process within a method that is iterating and populate a new array?
I hope this makes sense but if you need more details please let me know. And yes, the sort I'm using is a bubble sort. I know they are rubbish but this is for a course I'm doing so it has to be used. And yes, I'm pretty new to java.
Any help would be greatly appreciated (and I'll even reference you if I need to use some code you provide ;)
The main issue that I see, is that with each traversal through your inner loop of:
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
Where you call:
double [] tempArray = getDiagonals(i, j);
You are resetting all of the values of datap to be the current calculated median. To fix, you would need some way to indicate only the indices of the particular datap value that you want to populate.
You need to replace this section of your code:
for (int z = 0; z < datap.length; z++){
for (int y = 0; y < datap[z].length; y++){
datap[z][y] = median;
}
}
You could declare int y, z at the top of the method and do something like this:
if (y < datap.length){
if (z == datap.length[y] - 1){
y++;
z = 0;
}
datap[y][z] = median;
z++;
}
That way you are only assigning to the specific index in datap that you are trying to reach, instead of resetting each of its values.
Finally cracked it. To populate the whole array the following code works a peach.
//Diagonal to 1dArray and sorting
double [] [] bubbles()
{
double [][] tempArray = new double [300][300];
int y = 0;
int z = 0;
double median = 0;
for (int i = 0; i < data.length; i++)
{
for (int j = 0; j < data[i].length; j++)
{
if ((i > 0) && (j > 0))
{
if ((i +1 < data[i].length) && (j +1 < data[j].length))
{
double [] diagonals = getDiagonals(i, j);
//Need to sort here
median = diagonals[2];
tempArray[i][j] = median;
}
}
}
}
return tempArray;
}
The sorting was taken out and I haven't tested with it back in yet; but so far this is providing new values for all the cells in the temp array.