i need some help in this problem, there are three columns namely phase#, block# and lot#, i needed to sort the phase# in ascending order while the other two will be sorted accordingly to its phase#:
Problem:
Phase# 1-2-1-3-1-2-1
Block# 1-1-2-1-2-1-1
Lot# 1-2-2-2-3-1-1
What it should be like:
Phase# 1-1-1-1-2-2-3
Block# 1-2-2-1-1-1-1
Lot# 1-2-3-1-2-1-2
here's what I've got so far:
import java.io.*;
import java.util.*;
public class test{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x = 7;
int y = 3;
int[][] phase= new int[x][y];
int swap = 0, temp, i, min = 0;
phase[0][0] = 1;
phase[1][0] = 2;
phase[2][0] = 1;
phase[3][0] = 3;
phase[4][0] = 1;
phase[5][0] = 2;
phase[6][0] = 1;
phase[0][1] = 1;
phase[1][1] = 1;
phase[2][1] = 2;
phase[3][1] = 1;
phase[4][1] = 2;
phase[5][1] = 1;
phase[6][1] = 1;
phase[0][2] = 2;
phase[1][2] = 2;
phase[2][2] = 2;
phase[3][2] = 2;
phase[4][2] = 3;
phase[5][2] = 1;
phase[6][2] = 1;
System.out.println("UNSORTED: \n");
System.out.println("Phase#\tBlock#\tLot#");
for(i = 0; i < phase.length; i++){
System.out.print(phase[i][0] + "\t" + phase[i][1] + "\t"+phase[i][2]);
System.out.print("\n");
}
System.out.println("\nSORTED:\n");
for(i = 0; i <= phase.length- 1; i++){
min = i;
for(int a = i+1; a < phase.length; a++ ){
if(phase[a][0]<phase[a][0]){
temp=phase[i][0];
phase[i][0]=phase[a][0];
phase[a][0]=temp;
}
}
}
System.out.println("Phase#\tBlock#\tLot#");
for(int j = 0; j < phase.length; j++){
System.out.print(phase[j][0] + "\t" + phase[j][1] + "\t"+phase[j][2]);
System.out.print("\n");
}
}
}
Implement Comparator interface as (int [] left, int[] right) -> left[0]-right[0] and pass it to Arrays.sort() method:
Arrays.sort(phase, (int [] left, int[] right) -> left[0]-right[0]);
Related
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++;
}
}
}
How would I make this pascal triangle into a square?
import java.util.*;
public class PascalSquare {
public static void main(String[] args) {
int row = 7;
int[][] pascal = new int[row +1][];
pascal[1] = new int[1 + 2];
pascal[1][1] = 1;
for (int i = 2; i <= row; i++) {
pascal[i] = new int[i + 2];
for (int j = 1; j < pascal[i].length - 1; j++) {
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
}
}
for (int i = 1; i <= row; i++) {
for (int j = 1; j < pascal[i].length - 1; j++) {
System.out.print(pascal[i][j] + " ");
}
System.out.println();
}
}
}
// I want this to be a 7*7 square that will follow the same principle as a pascal triangle(adding the numbers above)
I have 2 1d arrays and i am trying to populate them into a single 2d array in JAVA.
For instance:
a[] = {2,7}
b[] = {9,1}
The results should then be:
result[][] = {{2,9}, {7,1}}
This is my code
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Test Cases:\t");
int t = sc.nextInt();
int[] a;
int[] b;
int i, j, x;
for (int k = 0; k < t; k++) {
System.out.println("Enter 1st Array Limit:\t");
int len = sc.nextInt();
System.out.println("Enter 2nd Array Limit:\t");
int len1 = sc.nextInt();
a = new int[len];
b = new int[len1];
System.out.println("Enter Sum Value");
x = sc.nextInt();
System.out.println("Enter " + len + " elements:\t");
for (i = 0; i < len; i++) {
a[i] = sc.nextInt();
}
System.out.println("Enter " + len1 + " elements:\t");
for (j = 0; j < len1; j++) {
b[j] = sc.nextInt();
}
int [][] c = new int[len][2];
for (i = 0; i < len; i++) {
for (j = 0; j < len1; j++) {
if (a[i] + b[j] == x) {
for(int l = 0; i < a.length; i++){
c[l][0] = a[i];
c[l][1] = b[j];
}
}
}
}
System.out.println(Arrays.deepToString(c));
}
}
}
This still produces wrong output
i want to find Find all pairs with a given sum
int[] a = {2,7};
int[] b = {9,1};
int[][] c = new int[a.length][2];
for(int i = 0; i < a.length; i++){
c[i][0] = a[i];
c[i][1] = b[i];
}
should do the trick
I feel like I'm really close with this implementation of a memoized matrix chain algorithm in Java, but I'm getting an array out of bounds error on line 45 and 53. These, for some reason, really seem to mess me up. Maybe there's something I'm continually messing up with, but I dunno, obviously. Can anyone help me out?
public class Lab2 {
//fields
static int p[];
static int m[][];
final static int INFINITY = 999999999;
public Lab2() {
//
}
public static void main(String[] args) {
Lab2 lab2 = new Lab2();
Lab2.m = new int[7][7];
Lab2.p = new int[7];
Lab2.p[0] = 20;
Lab2.p[1] = 8;
Lab2.p[2] = 4;
Lab2.p[3] = 25;
Lab2.p[4] = 30;
Lab2.p[5] = 5;
Lab2.p[6] = 10;
int n = Lab2.p.length-1;
//initialize m array to infinity
for (int i = 1; i <= n; i++){
for (int j = i; j <= n; j++){
Lab2.m[i][j]= INFINITY;
}
}
lab2.lookUpChain(m, p, 1, n);
for (int i = 0; i < 8; i++){
for (int j = 0; j < 8; j++){
System.out.println(m[i][j]);
}
}
}
//
public int lookUpChain(int m[][], int p[], int i, int j ){
if (m[i][j]<INFINITY){
return m[i][j];
}
if (i == j){
m[i][j] = 0;
}
else{
for (int k = i; k <= j; i++){
int q = (lookUpChain(m,p,i,k)) + (lookUpChain(m,p,k+1,j)) + (p[i]*p[k]*p[j]);
if (q < m[i][j]){
m[i][j] = q;
}
}
}
return m[i][j];
}
}
else{
for (int k = i; k <= j; i++)
Change to:
else{
for (int k = i; k <= j; k++) // change i to k
I have a java code where X is a 2D array and a loop to add data to it. The code is as follows:
public static void main(String [] args0
{
int[] len = new int[3];
double[][] X = null;
double[][] vec = null;
for (int i = 0; i < 3; i++)
{
System.out.println("Enter the len" +(i+1)+":");
len[i] = in.nextInt();
if(i == 0)
{
vec = new double [1][len[i] + 1];
X = new double[1][vec[0].length];
for(int k = 0; k < vec[0].length - 1; k++)
{
Xi[0][k] = 0;
}
Xi[0][ve1[0].length-1] = 1;
}
else
{
vec = new double [1][len[i] + 1];
X = new double[1][vec[0].length];
for(int k = 0; k < vec[0].length - 1; k++)
{
X[0][k] = 0;
}
X[0][vec[0].length-1] = 1;
}
}
}
When I print X, I need it have appended the values added when i=0 and when i>0. But it prints only the value of what is supposedly the final iteration. How do I make it print the data of all iterations appended to the end of data added during each iteration? I understand that since I am creating a new double[][] in each iteration, the value of gets overwritten. But how do i fix it?
You have correctly identified that with X = new double[1][vec[0].length]; you are always overriding the result from previous iterations. In order to fix it, you need to move the initialization out of the for loop. Here is an example of how you can do that:
public static void main(String [] args) {
int[] len = new int[3];
double[][] X = new double[3][];
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Enter the len " +(i+1)+":");
len[i] = in.nextInt();
X[i] = new double[len[i]];
for (int j = 0; j < len[i] - 1; j++) {
X[i][j] = 0;
}
X[i][len[i] - 1] = 1;
}
in.close();
}
As you can see, the array X is initialized in the beginning with size 3, since it will always hold 3 one-dimensional arrays in your case. However, you can initialize the size dynamically as well. You don't need the vec variable, because you can always access the length that was just read and stored in len. In fact, you don't need the len array as well, since it stores information that is anyways contained in X:
public static void main(String [] args) {
double[][] X = new double[3][];
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Enter the len " + (i+1) + ":");
X[i] = new double[in.nextInt()];
for (int j = 0; j < X[i].length - 1; j++) {
X[i][j] = 0;
}
X[i][X[i].length - 1] = 1;
}
in.close();
}