Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code does not seem to work. I need this method to take an array as an argue

ID: 3879176 • Letter: T

Question


This code does not seem to work. I need this method to take an array as an arguement and change the array so that it has all the zeroes at the begining. I'm trying to get it to run and only print "tests pass" in console. Could someone help fix the code and highlights the neccessary changes. I'm using Java on Netbeans and am not that savy with it yet.
Example:
[-1,-2,-2,4] => [-1,-2,-2,-4]

[4,3,2,1,0] => [0,4,3,2,1]

[0, 5, 0, -1, 10, 11] => [0,0,5,-1,10,11]

[-4, 5, -6, 7, 0, 4, 3, 0, 0] => [0,0,0,-4,5,-6,7,4,3]

[0,0,1] => [0,0,1]

import java.util.Arrays;
class ZeroFirst {
public static void main(String[] args) {
int[] a1 = {-1,-2,-2,4}; // input
int[] a1_exp = {-1,-2,-2,4}; // expected contents of a1 after calling zero_first
zero_first(a1);
if (!Arrays.equals(a1, a1_exp)) {
System.out.println("FAILED TEST 1");
System.exit(1);
}
int[] a2 = {4,3,2,1,0};
int[] a2_exp = {0,4,3,2,1};
zero_first(a2);
if (!Arrays.equals(a2, a2_exp)) {
System.out.println("FAILED TEST 2");
System.exit(1);
}
int[] a3 = {0, 5, 0, -1, 10, 11};
int[] a3_exp = {0,0,5,-1,10,11};
zero_first(a3);
if (!Arrays.equals(a3, a3_exp)) {
System.out.println("FAILED TEST 3");
System.exit(1);
}
int[] a4 = {-4, 5, -6, 7, 0, 4, 3, 0, 0};
int[] a4_exp = {0,0,0,-4,5,-6,7,4,3};
zero_first(a4);
if (!Arrays.equals(a4, a4_exp)) {
System.out.println("FAILED TEST 4");
System.exit(1);
}
int[] a5 = {0,0,1};
int[] a5_exp = {0,0,1};
zero_first(a5);
if (!Arrays.equals(a5, a5_exp)) {
System.out.println("FAILED TEST 5");
System.exit(1);
}
/* We will test your code on additional test
cases, so make sure it really works. (e.g., you can
add more of your own test cases, just copy one of the
above ones and modify it).
*/
System.out.println("Tests passed");
}
public static void zero_first(int[] arr) {
/* Your solution here */
}
}

Explanation / Answer

/*All code is well commented so please copy the whole code and execute

* Solution Approach (1):

* You can solve this problem by just counting number of zeros in the array

* Steps:

* (1) Iterate the array and count the number of zeros in the array

* (2) Shift the all non-zero element to the last one by one

* (3) Fill the array with zeros from starting to the Number of zeros in the array

*

* Solution Approach (2):

* You can also solve this problem by using extra array of same size

* Steps:

* (1)Create a new array and initialize with all 0s

* (2)Traverse the input array from back and start filling new array from back when you

* find non-zero element

* (3) Copy new array to original array

* I am going to use First approach

* */

import java.util.Arrays;

class ZeroFirst {

public static void main(String[] args) {

int[] a1 = { -1, -2, -2, 4 }; // input

int[] a1_exp = { -1, -2, -2, 4 }; // expected contents of a1 after

// calling zero_first

zero_first(a1);

if (!Arrays.equals(a1, a1_exp)) {

System.out.println("FAILED TEST 1");

System.exit(1);

}

int[] a2 = { 4, 3, 2, 1, 0 };

int[] a2_exp = { 0, 4, 3, 2, 1 };

zero_first(a2);

if (!Arrays.equals(a2, a2_exp)) {

System.out.println("FAILED TEST 2");

System.exit(1);

}

int[] a3 = { 0, 5, 0, -1, 10, 11 };

int[] a3_exp = { 0, 0, 5, -1, 10, 11 };

zero_first(a3);

if (!Arrays.equals(a3, a3_exp)) {

System.out.println("FAILED TEST 3");

System.exit(1);

}

int[] a4 = { -4, 5, -6, 7, 0, 4, 3, 0, 0 };

int[] a4_exp = { 0, 0, 0, -4, 5, -6, 7, 4, 3 };

zero_first(a4);

if (!Arrays.equals(a4, a4_exp)) {

System.out.println("FAILED TEST 4");

System.exit(1);

}

int[] a5 = { 0, 0, 1 };

int[] a5_exp = { 0, 0, 1 };

zero_first(a5);

if (!Arrays.equals(a5, a5_exp)) {

System.out.println("FAILED TEST 5");

System.exit(1);

}

/*

* We will test your code on additional test cases, so make sure it

* really works. (e.g., you can add more of your own test cases, just

* copy one of the above ones and modify it).

*/

System.out.println("Tests passed");

}

public static void zero_first(int[] arr) {

//Getting length of the array

int len=arr.length;

int zero_count=0;

//counting the number of zeros in array

for(int i=0;i<len;i++)

{

//comparing array's ith element with zero and increasing count

if(arr[i]==0)

zero_count++;

}

int count=len-1;

//Now shifting all non-zero elements towards right of the array

for(int i=len-1;i>=0;i--)

{

if(arr[i]!=0)

arr[count--]=arr[i];

}

//Now filling starting zero_count positions with zeros

for(int i=0;i<zero_count;i++)

arr[i]=0;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote