Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

You are given an array of integers and a boolean variable called positive. If positive is true, return the sum of all the positive numbers. If positive is false, return the sum of all the negative numbers. arrayPosOrNeg({1,2,2}, true) → 5 arrayPosOrNeg({1,2,2}, false) → 0 arrayPosOrNeg({-10,10,0,5,-4}, true) → 15 public int arrayPosOrNeg(int[] nums, boolean positive) { }

OpenStudy (anonymous):

public int arrayPosOrNeg(int[] nums, boolean positive) { int SumNeg=0, SumPos=0; //this way is one of the fastest ways to write this program for (int i=0; i<nums.length; i++) { if (num[i]<0) SumNeg=num[i]+SumNeg; else SumPos=num[i]+SumPos; } if (positive) return SumPos; else return SumNeg; }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!