Question

I need help in java use anythitg except​ (Method , bollean ) Given nA and a[0],...

I need help in java 
use anythitg except​ (Method , bollean )
Given nA and a[0], a[1], ..., a[nA - 1], give a segment of code that will
    copy the non-zero elements of array "a" into an array "b", "squeezing out the zeroes",
    and setting nB equal to the number of non-zero elements in b.
    That is, if nA=13 and the contents of a[i], where i = 0 to nA - 1 are 
    initially 
    0.0, 1.2, 0.0, 0.0, 0.0, 2.3, 0.0, 9.7, 5.6, 0.0, 3.6, 0.0, 0.0 
    then after execution of the code the contents should be nB=5, b[0]=1.2, 
    b[1]=2.3, b[2]=9.7, b[3]=5.6, and b[4]=3.6.
  
        Note:
        You may assume both arrays are of type double.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans)

public class ArrayToArray {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       double[] a = {0.0,1.2,0.0,0.0,0.0,2.3,0.0,9.7,5.6,0.0,3.6,0.0,0.0};//Elements in array A
       int nA = a.length; //length of array a
       System.out.println("Size of array a is "+nA);//printing the size of an array a
       int count = 0,i; //initializing count and i
       for(i=0; i<a.length;i++){ //iterating the array a
           if(a[i] != 0){ //checking the non zero elements in array a
               count++; //if condition satisfies increment the count
           }
       }
       double[] b = new double[count]; //initialize an array b with size of the count which gives the size of num of non zero elements
       int y =0; //initialize y
           for(int k=0;k<a.length;k++){ //iterate the array a
               if(a[k] != 0 ){ //checking for non zero element in array a
                   b[y] = a[k]; //assigning the non zero element to array b
                   System.out.println(y+1 + " Element of array in b is "+b[y]); //printing the elements of array b
                   y++;// incrementing the y value
               }
           }
           int nB = b.length; // assigning thr length of array b to nB
           System.out.println("Size of array b is "+nB);//printing length of array b
       }
}

output:

Size of array a is 13 1 Element of array in b is 1.2 2 Element of array in b is 2.3 3 Element of array in b is 9.7 4 Element

Add a comment
Know the answer?
Add Answer to:
I need help in java use anythitg except​ (Method , bollean ) Given nA and a[0],...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with java code, this is all in a method 1) 3 different arrays are...

    Need help with java code, this is all in a method 1) 3 different arrays are brought in 2) I create local array called arrayWithLargestValues which is the size of the largest array from the 3 brought in 3) I need to somehow fill the local 'arrayWithLargestValues' with the all different largest values from the 3 different arrays hopefully I made sense sample input/output int [ ] arrayWithLargestValues = new int [ ] // this has to be set to...

  • I need help with some java code concerning the comparison of two arrays. Let's say I...

    I need help with some java code concerning the comparison of two arrays. Let's say I have two arrays. Array A and Array B. Array A has 7 integers on each line, and there are hundreds of lines. Array B is the same as Array A except every line of integers have been sorted in ascending order. In Array A there are already lines that are sorted, so some lines in Array A will be equal in Array B. What...

  • JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...

  • Using Java solve recursively without the use of loops or modifying the method signatures. /** *...

    Using Java solve recursively without the use of loops or modifying the method signatures. /** * Given a sorted input array a, return a sorted array of size a.length + 1, * consisting of all elements of array a and integer i. * * @param a an array that is sorted in a non-descending order * @param i an integer * @return a sorted array of size a.length + 1, consisting of all elements of * array a and integer...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • Java programming: I need to create a method that is given a 2D char array and...

    Java programming: I need to create a method that is given a 2D char array and a String that returns a part of the original 2D array. The input string will tell the method which rows from the input array need to be returned. For example, a 5x5 char array input along with a string "0 4" would return rows 1 and 5 of the input array. The String input will always be numbers divided by spaces and will not...

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular a...

    Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular array implementation of the bounded queue by growing the elements array when the queue is full. Add assertions to check all preconditions of the methods of the bounded queue implementation. My code is! public class MessageQueue{ public MessageQueue(int capacity){ elements = new Message[capacity];...

  • Java question Given an array of integer numbers, write a linear running time complexity program in...

    Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...

  • Given an array of integer numbers, write a linear running time complexity program in Java to...

    Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT