9.1k views
3 votes
Given an array A[a1,a2,a3,...an] of size n.Create an array B[] from A[] where b1=a1,b2=a1+a2,b3=a1+a2+a3, bn=a1+a2+a3+...+an.

(Java or c++)

1 Answer

4 votes

public static void main(String[] args) {

int A[] = {1,2,3,4};

int B[] = new int[A.length];

for (int x = 0; x < A.length; x++){

if (x == 0){

B[x] = A[x];

}

else{

B[x] = B[x-1] + A[x];

}

}

for (int w:B){

System.out.println(w);

}

}

}

I created my solution in java. I hope this helps!

User Notoriousxl
by
4.4k points