Given nA and a0 a1 anA 1 give a segment of code that will
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.
Solution
Answer:
int nB = 0;
for(int i = 0;
i < nA; i++)
{
if(a[i] != 0.0)
{
b[nB] = a[i];
nB++;
}
}
![Given nA and a[0], a[1], ..., a[nA - 1], give a segment of code that will copy the non-zero elements of array \ Given nA and a[0], a[1], ..., a[nA - 1], give a segment of code that will copy the non-zero elements of array \](/WebImages/31/given-na-and-a0-a1-ana-1-give-a-segment-of-code-that-will-1089090-1761573152-0.webp)