Originally posted by JosAH
One of the assignment rules is
Do not solve the problem by printing out only the non-zero values in the array
import java.util.Arrays; public class Compact { public static int[] randomArray(int[] n) { for (int i = 0; i < n.length; i++) { int b = (int) (Math.random() * 100); n[i] = b; return n[i]; } } /* for (int i = 0; i < a.length; i++) { } */ public static void main(String args[]) { int a[] = new int[20]; System.out.print(randomArray(a) + " "); } }
import java.util.Arrays; public class Compact { public static void main(String args[]) { int a[] = new int[20]; int sum = 0; for (int i = 0; i < a.length; i++) { int b = (int) (Math.random() * 10); a[i] = b; System.out.print(a[i] + " "); } System.out.println(); for (int k = 0; k < a.length; k++) { if (a[k] == 0) { a[k] = a[k + 1]; sum++; } } for (int j = 0; j < a.length - sum; j++) { System.out.print(a[j] + " "); } } }
import java.util.Arrays; //Imports Array Package public class Compact //Creates Class Compact { public static void main(String args[]) //Main Method { /*Variables*/ int a[] = new int[20]; int sum = 0; /*First for loop prints out the random array*/ for (int i = 0; i < a.length; i++) { int b = (int) (Math.random() * 10); a[i] = b; System.out.print(a[i] + " "); } /*Prints out a blank line*/ System.out.println(); /*Second for loop returns the random array without zeros*/ for (int k = 0; k < a.length - 1; k++) { if (a[k] == 0) { for (int m = k; m < a.length - 1; m++) { for (int n = 0; n < a.length - 1; n++) { } } sum++; } } //Prints out the array without zeros for (int j = 0; j < a.length - sum; j++) { System.out.print(a[j] + " "); } } }
import java.util.Arrays; //Imports Array Package public class Compact //Creates Class Compact { public static void main(String args[]) //Main Method { /*Variables*/ int a[] = new int[20]; int sum = 0; /*First for loop prints out the random array*/ for (int i = 0; i < a.length; i++) { int b = (int) (Math.random() * 10); a[i] = b; System.out.print(a[i] + " "); } /*Prints out a blank line*/ System.out.println(); /*Second for loop returns the random array without zeros*/ for (int k = 0; k < a.length - 1; k++) { if (a[k] == 0) { //It needs to check if the next integer is zero. If it is //then it needs to go to the next one. for (int i = 1; i < a.length - 1; i++) { if (a[k + i] == 0) { a[k] = a[k + i + 1]; } else { a[k] = a[k + 1]; } } sum++; } } //Prints out the array wihtout zeros for (int j = 0; j < a.length - sum; j++) { System.out.print(a[j] + " "); } } }
import java.util.Arrays; //Imports Array Package public class Compact //Creates Class Compact { public static void main(String args[]) //Main Method { /*Variables*/ int a[] = new int[20]; int sum = 0; /*First for loop prints out the random array*/ for (int i = 0; i < a.length; i++) { int b = (int) (Math.random() * 10); a[i] = b; System.out.print(a[i] + " "); } /*Prints out a blank line*/ System.out.println(); /*Second for loop returns the random array without zeros*/ for (int k = 0; k < a.length - 1; k++) { if (a[k] == 0) { //It needs to check if the next integer is zero. If it is //then it needs to go to the next one. for (int i = k; i < a.length; i++) { if (a[k + i] == 0) { if (a[k + i + 1] < a.length); a[k] = a[k + i + 1]; } else { a[k] = a[k + 1]; } } sum++; } } //Prints out the array wihtout zeros for (int j = 0; j < a.length - sum; j++) { System.out.print(a[j] + " "); } } }
Comment