***********JAVA************** HW3_P3 - Push zero to front Given an array A, push
ID: 3889564 • Letter: #
Question
***********JAVA**************
HW3_P3 - Push zero to front
Given an array A, push all the zeros of the array to the front. The order of all other elements should remain same.
Write a function:
void pushZero(int array[])
that accepts an integer array A and size of the array, N. The function should convert the original array as per above requirement.
Input
10
2 0 3 4 0 4 0 10 9 0
where,
-First line represents size of an array N.
-Second line represents array elements. There must be single space between each element.
There should be one space after the last element.
Output
0 0 0 0 2 3 4 4 10 9
There must be single space between each element of output array.
There should be one space after the last element.
N is an integer within the range [1 to 1,000,000].
*******Use the following class driver with no modifications to it!********
import java.util.*;
import java.lang.*;
import java.io.*;
class DriverMain{
public static void main(String args[]){
HW3_P3 hw3P3 = new HW3_P3();
int[] array = hw3P3.getArray();
hw3P3.pushZero(array);
//Print the result array
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
}
}
******************************************************************************
---And use the following class HW3_P3 that was refered to in main----
class HW3_P3{
public int[] getArray(){
Scanner scanner = new Scanner(System.in);
//write your code to get the array and return it
}
public void pushZero(int array[]){
// write your code
}
}
Explanation / Answer
------------------------------------------------------------------------------------------------------
package com.chegg;
import java.util.*;
import java.lang.*;
import java.io.*;
class DriverMain {
public static void main(String args[]) {
HW3_P3 hw3P3 = new HW3_P3();
int[] array = hw3P3.getArray();
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
hw3P3.pushZero(array);
// Print the result array
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
-------------------------------------------------------------------------------------------------
package com.chegg;
import java.util.Scanner;
class HW3_P3 {
public int[] getArray() {
Scanner scanner = new Scanner(System.in);
// write your code to get the array and return it
int i;
int N;
int a[] = new int[10];
System.out.println("Enter the value of N");
N = scanner.nextInt();
System.out.println("Enter the element of array");
for (i = 0; i < N; i++) {
a[i] = scanner.nextInt();
}
return a;
}
public void pushZero(int array[]) {
System.out.println(" After Push all the zeros to the front of array");
int i, j;
for (j = i = array.length - 1; i >= 0; i--) {
if (array[i] == 0)
continue;
array[j] = array[i];
j--;
}
while (j >= 0) {
array[j] = 0;
j--;
}
}
}
--------------------------------------------------------------------------------------------
output sample:-
Enter the value of N
10
Enter the element of array
1
0
9
0
7
0
4
3
7
0
1 0 9 0 7 0 4 3 7 0
After Push all the zeros to the front of array
0 0 0 0 1 9 7 4 3 7
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.