Help with arrays assignment for AP Comp Sci

Shadow Conception

Golden Member
Mar 19, 2006
1,539
1
81
My friend is having trouble with an arrays assignment. It asks to scan an array and put it in reverse order without creating another array. I already looked at it, and I seriously cannot tell what is wrong; working it out on a piece of paper, the first couple instances should put the array in reverse order. Here is the code. Any help would be greatly appreciated.

Code:
import java.util.Scanner;

public class Reverse
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int number;
        int temp;
        System.out.println("Bob Saget");
        System.out.println("How many integers?");
        number = scan.nextInt();
        
        int[] array = new int[number];
        int max = array.length - 1;
        
        for(int i = 0; i < array.length; i++)
        {
            System.out.println("Number " + (i + 1) + " :");
            array[i] = scan.nextInt();
        }
        
        for(int a = 0; a < array.length; a++)
            System.out.println("Number " + (1+a) + " " + array[a]);
            
        for (int b = 0; b < array.length; b++)
        {
            temp = array[b];
            array[b] = array[max];
            array[max] = temp;
            max--;
        }
        
        for(int c = 0; c < array.length; c++)
            System.out.println("Number " + (1+c) + " " + array[c] + " ");
    }
}

Assuming there are 5 integers, being {1, 2, 3, 4, 5}, this is the output:

Code:
Number 1 1
Number 2 2
Number 3 3
Number 4 4
Number 5 5
Number 1 1 
Number 2 2 
Number 3 3 
Number 4 4 
Number 5 5

Top five is the original array, bottom five is the new array. Bottom array SHOULD be the reverse of the top.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
It looks like an easy fix. But it should also be easy to spot.

Since this is for an assignment, I'd suggest you try working through that array-reversing loop manually. (I hope you remember which one it is, because you didn't leave any comments in the code to remind yourself.) It should become obvious that you over-did it a little. :D

Unedited: see later post.
 
Last edited:

Shadow Conception

Golden Member
Mar 19, 2006
1,539
1
81
This is really the only part of the code I'm "sure" about:

Code:
for (int b = 0; b < array.length; b++)
        {
            temp = array[b];
            array[b] = array[max];
            array[max] = temp;
            max--;
        }

So assuming we have an array {1, 2, 3, 4, 5} and b = 0 (at the start of the for loop), it should go like this.

temp = array - this should store the first item of the array into temp
array = array[max] - this should set the first item of the array equal to the last item of the array
array[max] = temp - this should set the last item of the array equal to the original first item of the array
max-- - this should reduce max to the penultimate item of the array

After running the loop once, our array should now look like this: {5, 2, 3, 4, 1}. But it doesn't, and neither my friend nor I can figure out why this is.

Any hints, tips, help?
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Hint: As told, the problem is in your loop to reverse.
Hint 2: Consider 2 cases in that loop. The very first iteration, and the very last iteration. Those two alone should make the problem crystal clear.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
You can better print out the array status with this code:
Code:
for(int a = 0; a < array.length; a++)
    System.out.print(array[a]);
System.out.println();
Now try doing that on every loop. (Edit: I mean every iteration of the one loop, of course.)
 
Last edited:

ArmTheHomeless

Junior Member
Jan 18, 2010
3
0
0
Hey guys, Im the one actually having the problems. Ive been taking your advice, but Im still confused about how to change the logic. I realize that the problem arises as soon as it gets to the second half of the array. I am still thoroughly confused about what the specific problem is, as well as how to fix it. A more direct explanation would be much appreciated.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Hey guys, Im the one actually having the problems. Ive been taking your advice, but Im still confused about how to change the logic. I realize that the problem arises as soon as it gets to the second half of the array. I am still thoroughly confused about what the specific problem is, as well as how to fix it. A more direct explanation would be much appreciated.

Since this is homework, none of us will provide a more direct answer.

This shouldn't be hard - we told you what loop to look at. I found the problem within 1 minute of looking at the code. I don't mean this as a knock to you by any means; however, with the hints that were given you should see it immediately.

What IDE (if any) are you using? Do you have access to a debugger? Have you tried printing out every value upon every iteration of the loop?

-Kevin
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
OK, final clue. It's the first cartoon on this page if you can't see it below:

"Male and female ships that pass in the night."
mmon478l.jpg


Maybe cartoon 5 would be helpful too?
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
This is really the only part of the code I'm "sure" about:

Code:
for (int b = 0; b < array.length; b++)
        {
            temp = array[b];
            array[b] = array[max];
            array[max] = temp;
            max--;
        }

So assuming we have an array {1, 2, 3, 4, 5} and b = 0 (at the start of the for loop), it should go like this.

temp = array - this should store the first item of the array into temp
array = array[max] - this should set the first item of the array equal to the last item of the array
array[max] = temp - this should set the last item of the array equal to the original first item of the array
max-- - this should reduce max to the penultimate item of the array

After running the loop once, our array should now look like this: {5, 2, 3, 4, 1}. But it doesn't, and neither my friend nor I can figure out why this is.

Any hints, tips, help?


These problems are the easiest ones to trace.

Let array.length = 2 (the smallest non-trivial case). Step through the 8 instructions that get executed. Write the array's contents after each new assignment. See the result. Or use a debugger.
 

ArmTheHomeless

Junior Member
Jan 18, 2010
3
0
0
Alright, I see where the issue is. Im trying to use the values in the arrays after those values have been switched around....still kinda stumped on the fix.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Alright, I see where the issue is. Im trying to use the values in the arrays after those values have been switched around....still kinda stumped on the fix.

Ok you just figured out the problem. If you figured out the problem how are you still stumped on the fix. Write out what you need to do.

1. Save first value to a temporary location.
2. Set first value to the last value.
3. Set last value to the temporary location.
4. Increment your one counter
5. Decrement the other counter.
6. Run for n/2 cases

You KNOW this "algorithm", just write the code for it. If you can't figure the code out based on my pseudo code, you need to reevaluate why you are taking this class.

-Kevin
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
I finally got it, I woke up this morning and had an epiphany on what to do. Thanks to all of you.

Welcome to the world of programming ;)

Most of the time that is how it works. I sat in front of my code for about 4 hours one time trying to figure out what was wrong. The minute I get up to go do something else, I thought of what was wrong and came sprinting back to fix it ;)

Let us know if you have any more questions.

-Kevin
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
Debuggers are your friend... actually scratch that debuggers are your lord and savior. If you're not using an IDE with one find one or find a debugger you can use seperately.