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

Given nA and a[0], a[1], ..., a[nA - 1], give a segment of code that will copy t

ID: 3930502 • Letter: G

Question

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.

Explanation / Answer

Answer:

int nB = 0;

for(int i = 0;

i < nA; i++)

{

if(a[i] != 0.0)

{

b[nB] = a[i];

nB++;

}

}

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