Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java Beginner Program

Loudpakad
Loudpakad 


My program is supposed to take a provided array and create a new array where each element is the sum of the previous elements in original array. For example element one in new array is element one in original array. Element two in new array is sum of element one an element two in original array. Element three in new array is sum of element one, two and three in original array. I wrote this but I know it is incomplete. Please help! public class PrefixSum { public static void main(String[] args) { int[] array = new int[]{0,5,1,-3,2,0,4}; int[] newArray = new int[7]; int x = 0; for(int i = 0; i < array.length; i++) { x = array[i]; x = x + i; } newArray[0] = 0; System.out.println(" " + newArray[x]); } } Attachments
Tags Clarifications

Answers

Prefix Sum

uetian_cute
uetian_cute 



The program is doing what you described in question. A screenshot of output is attached. Attachments
PrefixSum.zip (10 K) Download
Preview

Preview
PrefixSum { public static void main(String[] args) { int[] array = new int[]{0,5,1,-3,2,0,4}; int[] newArray

Screenshots
Screenshot
Purchase Answer

PrefixSum

twillenbring
twillenbring 



Adds Elements of original array to new array. Just like the description in the question Attachments
JavaBeginnerPrefixSum.zip (2 K) Download




Preview
public static void main(String[] args) { int[] theArray = {1,2, 3, 4 ,5 ,6 }; //= 21 int[] secondArray = {0,5,1,-3,2,0,4}; int[] result = sumArray(theArray); int[] secondResult = sumArray(secondArray); System.out.println(Arrays.toString(result));

Screenshots
Purchase Answer