Jump to content

Need help with arrays (JAVA)

CreepyPro

Write a method named collapsePairs that accepts an array of integers as a parameter and modifies the array so that each of its pairs of neighboring integers (such as the pair at indexes 0-1, and the pair at indexes 2-3, etc.) are combined into a single sum of that pair. The sum will be stored at the even index (0,2,4, etc.) if the sum is even and at the odd index (1,3,5, etc.) if the sum is odd. The other index of the pair will change to 0.

 

I can't figure out why I'm not getting the right outpout. For example:

test #1:collapsePairs({7, 2, 8, 9, 4, 22, 7, 1, 9, 10});
icon-question-mark.png exp. params:{0, 9, 0, 17, 26, 0, 8, 0, 0, 19}
your parameters:{0, 9, 0, 17, 0, 26, 0, 8, 0, 19}
 
 
public static int[] collapsePairs(int[]a) {
		int x[] = new int[a.length];
		for (int i=0; i<a.length-1; i=i+2) {
			if (a[i]+a[i+1]%2==0) 
				x[i]=a[i]+a[i+1];
			else
				x[i+1] = a[i]+a[i+1];
		}
    for(int i=0;i<a.length;i++){
            a[i] = x[i];
    }
	return a;
	}
Link to comment
Share on other sites

Link to post
Share on other sites

This is definitely homework so I'll be a bit vague.

 

You're doing something out of order, remember order of operations and think about where it applies in your function.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

if (a[i]+a[i+1]%2==0) 

 

Double check that line. You're missing something.

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

Hey, do you mind marking your other topic as solved before posting another question? Maybe thank the people who helped you while you're at it?

As for this question, there's a mistake in the way you check whether the sum is even or odd. Consider this (check for yourself if you don't believe me):

(4 + 22)%2 = 0 == 0 ✅

4 + 22%2 = 4 != 0 ❌

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Look at Java Operator Precedence : http://www.cs.bilkent.edu.tr/~guvenir/courses/CS101/op_precedence.html

 

tldr: modulus has higher priority so gets calculated first... and you don't want that. Instruct the compiler to do the addition first by grouping the numbers in something that has higher precendence, like paranthesis for example

So your 

if (a[i]+a[i+1]%2==0) 

can be rewritten as

 

if ( (a[i]+a[i+1]) % 2 == 0) 

 

15 ()
[]
·
Parentheses
Array subscript
Member selection
Left to Right
12 *
/
%
Multiplication
Division
Modulus
Left to right
11 +
-

Addition
Subtraction

Left to right

Link to comment
Share on other sites

Link to post
Share on other sites

Look into the
"Binary Search"

Link to comment
Share on other sites

Link to post
Share on other sites

Break points and debuggers are your friend. Learn to use them. I also suggest intellij IDE for java. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×