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 \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site