I need to implement a method that returns the alternating sum of all elements with odd indexes minus the sum of all elements with even indexes. The total sum returned should be -1. 1 - 4 + 9 - 16 + 9 = -1.
Here is my code:
public class Arrays
{
public static void main(String[] args){
int [] data = {1 ,4, 9, 16, 9};
oddAndEven(data);
}
public static int[] oddAndEven(int[] data){
int sum = 0;
int sumA = 0;
int index = data.length;
for(int i:data){
if(index % sumA == 1){
sum = sum-i;
}
else{
sum = sum+i;
}
}
System.out.println(sum);
return sum;
}
}
Can someone tell me where I am going wrong please?
This is a class session, so forgive my basic code and errors.
This is how I would do it:
public class test {
public static void main(String[] args) {
int [] data = {1 ,4, 9, 16, 9};
oddAndEven(data);
}
public static void oddAndEven(int[] data) {
int total = 0;
for (int i = 0; i < data.length; i++)
{
if (i%2==0)
total = total + data[i];
else
total = total - data[i];
}
System.out.println(total);
}
I've gotten rid of the return in the method and changed it to void (as you are printing out the result within it, so there is no need to return it.
You don't need the two different sum values, or the length of the array stored.
The total value is used and set to 0. The for loop then goes through the length of the array. The %2 divides the number by 2 and determines the remainder. So for the first loop, it will calculate 0/2 and work out the remainder (obviously 0). As it ==0, the first if statement in the for loop is executed (adding the numbers).
The second time through, it calculates 1/2, which is 0 with 1 remaining - so the else statement is executed and so on.
Additionally, note how I've gotten rid of the braces around the if and else statements. As long as these statements are a single line, the braces aren't needed - taking the out tends to make the program easier to read (in my opinion). Obviously, if more than one line were needed under them, the braces need to be readded.
What about this ?
public class ArrayMeNow {
public static void main(String[] args) {
int [] data = {1 ,4, 9, 16, 9};
int result = oddAndEven(data);
System.out.println(result);
}
private static int oddAndEven(int[] data) {
int multiplier = 1;
int result = 0;
for(int v:data){
result += v * multiplier;
multiplier *= -1;
}
return result;
}
}
public static int oddAndEven(int[] data) {
int sum = 0;
for (int i=0;i<data.length;i++) {
if (i % 2 == 1) {
sum = sum - data[i];
} else {
sum = sum + data[i];
}
}
System.out.println(sum);
return sum;
}
for(int i:data) doesn't change the value of index. And sumA is supposed to be 2.
Change your for-loop to something like:
for (int i = 0; i < data.length; i++)
if (i % 2 == 1)
sum -= data[i];
else
sum += data[i];
You have to return sum which is of type int NOT int[]. Here is another way to do it.
public static int doAlternateAddSubOn(int[] array) {
int sum = 0;
for(int i=0; i<array.length; i++) {
// When index 'i' is Even, the position is Odd
sum = (i%2==0) ? sum+array[i] : sum-array[i];
}
return sum;
}
Related
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 3 years ago.
I am trying to add all numbers in an array that are more than the last number it checked. I am getting this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at RaySumLast.sum(RaySumLast.java:16)
at RaySumLast.main(RaySumLast.java:25)
Here is my code:
class RaySumLast
{
static int arr[] = {-99, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5};
static int sum()
{
int sum = 0;
for (int i : arr) {
if (i < arr[i-1])
sum += i;
}
return sum;
}
public static void main(String[] args)
{
cSystem.out.println(sum());
}
}
How can I fix this?
You can't use the value in your array as an index into your array. Even if you skip the initial value of the array, it will fail as soon as there's a value that exceeds the bounds, as in this example (notice the 20):
static int arr[] = {-99,1,2,3,4,5,6,7,8,9,20,5};
If you want to use the enhanced for loop, you can use this:
int prev = arr[0];
int sum = prev;
for (int i : arr) {
if (i > prev)
sum += i;
prev = i;
}
However, I much prefer to use the answer by #Domin0 that loops over the indexes.
class RaySumLast
{
static int arr[] = {-99,1,2,3,4,5,6,7,8,9,10,5};
static int sum()
{
int sum = arr[0];
for (int i=1; i<arr.length; ++i) {
if (arr[i] > arr[i-1])
sum += arr[i];
}
return sum;
}
public static void main(String[] args)
{
System.out.println(sum());
}
}
This solution assumes the array is not empty and that the 1st element should always be part of the sum.
This may help you:
class RaySumLast
{
static int arr[] = {-99,1,2,3,4,5,6,7,8,9,10,5};
static int sum()
{
int sum = 0;
int index = 0;
for (int i : arr) {
if(index == 0) sum += i;
else {
if (i > arr[i-1])
sum += i;
}
index ++;
}
return sum;
}
public static void main(String[] args)
{
System.out.println(sum());
}
}
I had to make some assumptions.
The first number will always be included in the sum.
All numbers will be checked. So all the 5's in the sequence (10, 5, 10, 5, 10, 5, 10)
would be skipped and the sum would be 40.
I also think it would be better style to supply the array as an argument. It also makes it easier to test different cases.
public static int sum(int[] arr) {
int sum = 0;
// set the last to the smallest possible integer.
int last = Integer.MIN_VALUE;
for (int n : arr) {
if (n > last) {
// sum if current value is > than last.
sum += n;
}
// always update last to current value.
last = n;
}
return sum;
}
Here is a somewhat different approach by using streams. Same assumptions as before. The indices start at 1 and go to arr.length -1 Thus the first array element must be added to the final sum.
public static int sum(int[] arr) {
return arr[0] + IntStream.range(1, arr.length)
.filter(i -> arr[i] > arr[i - 1])
.map(i -> arr[i]).sum();
}
I'm new to this forum and to programming and I have my first question :)
The objective is to return the sum of the first two integers in the array. My solution is in the picture I've added. Is there a way to solve this with less if cases? (I know I did not have to do the for loop, but I just learned it and wanted to try it out :D)
Thank you for all answers!
https://i.stack.imgur.com/hx06s.png
Edit: Here's my code:
public int sum2(int[] nums) {
int sum = 0;
if(nums.length == 0){
return sum;
}
if(nums.length == 1){
return nums[0];
}
for(int i = 0; i < 2; i++){
sum += nums[i];
}
return sum;
}
you can use limit() method in stream
public long sum2(int[] nums) {
return Arrays.stream(nums).limit(2).sum();
}
You can handle it in this simple way:
public int sum2(int[] nums) {
if (nums.length == 0)
return 0;
if (nums.length == 1)
return nums[0];
return nums[0] + nums[1];
}
You can't remove the two if statements form the code, unless you want to unnecessarily complicate it, for example, as #axurefrog wrote in his comment:
public int sum2(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length && i < 2; i++)
sum += nums[i];
return sum;
}
This returns the sum of the first n value of the array:
public int sum(int[] arr, int n) {
int sum = 0;
int num = Math.min( arr.length, n );
for( int i=0; i < num; i++ ) {)
sum += arr[i];
}
return sum;
}
Here are two alternative solutions which somewhat look "better":
sum2() with switch so no if statements
sum3() with nested ternary expressions, makes it very short.
It's good practice to avoid multiple return so you can keep the single entry - simple exit point of the algorithm, supporting debugging.
I've left the snippet wrapped up so you can try them by a single copy/paste.
public class Main{
public static int sum2(int[] nums) {
int sum = 0;
switch (nums.length) {
case 0:
break;
case 1:
sum = nums[0];
break;
default:
sum = nums[0] + nums[1];
break;
}
return sum;
}
public static int sum3(int[] nums) {
return nums.length == 1 ? nums[0] : nums.length >= 2 ? nums[0] + nums[1] : 0;
}
public static void main(String []args){
int[] nums1 = {11, 13, 1, 0}; // expected: 24
int[] nums2 = {3}; // expected: 3
int[] nums3 = {}; // expected: 0
System.out.println("sum2: " + sum2(nums1));
System.out.println("sum2: " + sum2(nums2));
System.out.println("sum2: " + sum2(nums3));
System.out.println(' ');
System.out.println("sum3: " + sum3(nums1));
System.out.println("sum3: " + sum3(nums2));
System.out.println("sum3: " + sum3(nums3));
}
}
The output:
sum2: 24
sum2: 3
sum2: 0
sum3: 24
sum3: 3
sum3: 0
You can simply do this without if-else headache
public int sum2(int[] nums)
{
int sum = 0;
for(int i = 0; i < nums.length && i < 2; i++) sum+=nums[i];
return sum;
}
Edit:
Adding i < nums.length in for loop condition to take care of empty array as per comment from Luca Murra
If you're using Java8 or above, and want to have a more generic solution (meaning sum of N numbers) you can use streams as follow:
public long sum2(int[] nums) {
return Arrays.stream(nums).sum();
}
I'm trying to sum up all of the elements in an array.
What do I need to put in for blank space of int i = ______; to make this work?
public static int sumArray(int [] A){
int sum = 0;
int i = ___________ ;
while(i>=0){
sum = sum + A[i];
i--;
}return sum;
}
I'm trying to sum up all of the elements in an array. What do I need to put in for blank space of 'int i = ______;' to make this work?
You should specify the size of array A to i by doing i = A.length - 1;.
Alternatively, you can use a for loop instead of while.
Here is the code snippet:
public static int sumArray(int [] A){
int sum = 0;
for(int x : A) {
sum += x;
}
return sum;
}
You've already written the code to get the sum. i just needs to start out at A.length.
There are many other ways to get the sum: using an enhanced "for each" loop; using IntStream.of(A).sum(); other library methods.
To answer your question, you should set int i to A.length - 1. You want length - 1 because Arrays are indexed at 0 which means using length alone will cause you an IndexOutOfBoundsException.
Your method should look like this:
public static int sumArray(int[] A) {
int sum = 0;
int i = A.length - 1;
while (i >= 0) {
sum = sum + A[i];
i--;
}
return sum;
}
However, you can do this in much more cleaner ways.
1) Using Java 8 you can use IntStream like so:
public static int sumArray(int[] A) {
return IntStream.of(A).sum();
}
2) You can use a for loop:
public static int sumArray(int[] A) {
int sum = 0;
for(int i = 0; i < A.length; i++){
sum += A[i];
}
return sum;
}
3) You can use an enhanced for loop:
public static int sumArray(int[] A) {
int sum = 0;
for(int i : A){
sum += i;
}
return sum;
}
I have to write a function "alternatingSum(a)" that takes an array of numbers and returns the alternating sum (where the sign alternates from positive to negative or vice versa).
For example:
int[] a = { 5, 3, 8, 4 };
Assert(alternatingSum(a) == 6); // because 5-3+8-4 == 6
So far I have tried to take the array and check to see if the index is odd (not including i=0) then subtract it from the i+1, and if it is even do the same thing except add the two. Am I on the right track with this?
Here is my code:
class foo {
public static int alternatingSum(int[] a){
int sum=0;
for (int i=0;i<a.length;i++){
if (i==0){
sum+=a[0]-a[1];
}
if (i%2 == 0){
sum+=(a[i]+a[i+1]);
}
if (i%2 == 1){
sum+=(a[i]-a[i+1]);
}
}
return sum;
}
public static void testAlternatingSum(){
System.out.println("testing code");
int[] a = { 5, 3, 8, 4 };
assert (alternatingSum(a) == 6); // because 5-3+8-4 == 6
}
public static void main(String[] args){
testAlternatingSum();
}
}
a for loop
I would just keep a boolean flag for even (and toggle it with every loop iteration). If it's an even number, than we're performing addition. But if it's not an even number (it's odd) then we can perform an unary negative operation. That might look something like,
int sum = 0;
boolean even = true;
for (int i = 0; i < a.length; i++) {
sum += even ? a[i] : -a[i];
even = !even
}
return sum;
for-each loop
and you could also use a for-each loop and write it like
boolean even = true;
int sum = 0;
for (int value : a) {
sum += even ? value : -value;
even = !even;
}
return sum;
Easy to read..
int[] alternatingSums(int[] a) {
int sum1 = 0;
int sum2 = 0;
for(int i =0; i < a.length; i++){
if((i % 2) != 1){
sum1 += a[i];
}else{
sum2 += a[i];
}
}
int[] result = {sum1 , sum2};
return result;
}
import java.util.*;
public class FirstOddOccurrence {
public static void main(String[] args) {
int[] x = {2, 4, 6, 7, 4, 3, 2, 7, 6, 7, 7};
int i;
display(x);
System.out.printf("# Occurrences of first odd = %3d\n", firstOddCount(x));
}
private static void display(int[] x) {
int i;
System.out.print("Array: ");
for (i = 0; i < x.length; i++) {
if (i < x.length - 1)
System.out.printf("%3d, ", x[i]);
else
System.out.printf("%3d\n", x[i]);
}
}
public static int odd(int[] x) {
int i;
int y;
for (i = 0; i < x.length; i++) {
y = x[i] % 2;
if (y == 1) {
return x[i];
} else {
return 0;
}
}
return x[i];
}
public static int firstOddCount(int x[]) {
int i;
int c = 0;
for (i = 0; i < x.length; i++) {
if (x[i] == odd(x))
c++;
}
return c;
}
}
I'm trying to find the first occurrence of an odd number in the array that has been provided. What is wrong with my program? I can't seem to get the program to count the first odd occurrences.
Your code here:
if (y == 1) {
return x[i];
} else {
return 0;
}
does not work - if a tested number is even, you immediately return 0. Instead you want to skip these even numbers and wait until an odd number comes up. In the end, if you don't find any odd number, you return 0. Here is the corrected version of odd():
int i;
int y;
for (i = 0; i < x.length; i++) {
y = x[i] % 2;
if (y == 1) {
return x[i];
}
}
return 0;
Andr's solution fixes your issue; odd(x) will return 0 if x[0] is even, and x[0] if it is odd.
You could also improve firstOddCount like so: odd(x) will always return the same value, so only calculate it once.
public static int firstOddCount(int x[]) {
int firstOdd = odd(x);
int c=0;
for(int i=0; i < x.length; i++) {
if (x[i]==firstOdd)
c++;
}
return c;
}
Your particular problem is that you return 0 if you find an even number. That means that the list {2, 4, 6, 8, 1} will give you 0, rather than 1, as the first odd number.
What you should do is ignore leading even numbers and continue to process the list.
However, the way you've organised your program, you're processing the first all-even part of the list twice, once in odd() to find the first odd number, then again in firstOddCount() to count how many of that number there are - that's totally unnecessary.
Once you find the first odd number, I think you can be reasonably certain that number (or any other odd number for that matter) does not exist in the space you've already searched. Otherwise it would have been the first odd number. Hence it makes little sense to go back and look at that initial part of the list again.
A way in which you can easily just process the list once is as follows:
public static int firstOddCount (int numbers[]) {
// Find first odd number or end of list.
int idx = 0, len = numbers.length;
while ((idx < len) && ((numbers[idx] % 2) == 0)
idx++;
// If at end of list, everything is even => count(first(odd)) is 0.
if (idx == len)
return 0;
// Otherwise, start counting from current position.
int count = 1, oddnum = numbers[idx];
while (++idx < len)
if (numbers[idx] == oddnum)
count++;
return count;
}
If you are trying to get one element from group you should use 'break' when your condition matched first time else it will give all...