Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Array Help

Name: Anonymous 2007-03-07 13:14 ID:dVhKGr1O

I know arrays are easy and everything.. but something which I think should be perfectly logical isn't working in my program.

I'm trying to make a method to reverse the order of my array, so that, for example
1 2 3 4 5
will become
5 4 3 2 1

I use something basic:
int left=array[0];
    int right=array.length;
    while(left<right){
        int temp=array[left];
        array[left]=array[right];
        array[right]=temp;
        left++;
        right--;

But array.length isn't number at the end of the array, it's the size of the array! If I have:

int num[] = new int[10] where num[10]=23, then num.length will return 10, not 23.

So I've tried using something like num[num.length], but I keep getting an error message about something being out of bounds. What am I doing wrong?

In b4 pointless java flames.

Name: Anonymous 2007-03-07 13:28 ID:5A+0zV0H

array.length - 1 you idiot

Name: Anonymous 2007-03-07 13:37 ID:dVhKGr1O

>>2
I've already tried using that but it still gave me bounding errors. I'll do it again, but I really don't know why it's not working. I get the logic but it's like.. I dunno.

Name: Anonymous 2007-03-07 13:55 ID:Heaven

>>3
if you do int num[]=new int[10], there is no num[10]. only num[0] through num[9], you idiot.

Name: Anonymous 2007-03-07 14:23 ID:Heaven

num[num.length-1], dumbshit

Name: Anonymous 2007-03-07 15:17 ID:cE0iBqJE

int left=array[0];
Tell me, what do you think this statement does?

Name: Anonymous 2007-03-07 15:29 ID:4DVER/ly

>>6
invoke the composition of the int, left, =, and array functions on the list [0].  I suspect it evaluates into _|_ though :/

Name: Anonymous 2007-03-07 15:50 ID://3lYfL/

>>4
I know that, dickhead. And if I do a System.out.println(array[array.length-1]); it gives me the number I want. But for some reason I can't assign it to another variable.

Name: Anonymous 2007-03-07 15:51 ID:qKeOGIiz

try that in Haskell: reverse [1, 2, 3]
or this in Erlang: lists:reverse([1, 2, 3]).

Name: Anonymous 2007-03-07 16:03 ID:Heaven

reverse []     = []
reverse (x:xs) = (reverse xs) ++ [x]

Name: Anonymous 2007-03-07 16:36 ID:UtHGJwOb

Python, with standard library:
reversed(l) #A lazy iterator

Python, recursive implementation:
reverse = lambda l: l and [l[-1]] + reverse(l[:-1]) or []

Python, with functional module:
reverse = lambda l: foldl(lambda x, y: [y] + x, [], l)

Python, with builtin reduce:
reverse = lambda l: reduce(lambda x, y: type(x) is int and [y] + [x] or [y] + x, l)

Name: Anonymous 2007-03-07 16:43 ID:5A+0zV0H

>>10
reverse = foldl (flip (:)) []
Superior!

>>11
Inferior. Fuck you and your failure of a language.

Name: Anonymous 2007-03-07 16:50 ID:Heaven

>>11
Ok, ok, now show us a pythoic solution that doesn't look like ass.

Name: Anonymous 2007-03-07 19:07 ID:qKeOGIiz

>>11
lol Python tries very hard to be functional like the real functional languages but fails hard. Even C++ is more useful than Python with the STL and Boost :D

Name: Anonymous 2007-03-08 2:36 ID:5rVKwRVE

>>8
And if I do a System.out.println(array[array.length-1]); it gives me the number I want. But for some reason I can't assign it to another variable.

Java: the only C-like language whose users do not understand the concept of zero-indexed arrays.

int bleh = array[array.length - 1];

Only a lousy programmer blames his tools.

Name: Anonymous 2007-03-08 2:40 ID:wEb/pPtP

>>13
reverse = lambda l: l[::-1]

Name: Anonymous 2007-03-08 2:47 ID:Js+ylac1

One word, the forced indentation of code. Thread over.

Name: Anonymous 2007-03-08 5:53 ID:3Kr4Q6x0

>>12
FYAD

>>13
Refer to >>11. The first and third solutions in particular are pretty nice. Oh and >>16 owned me, forgot about that.

>>15
Java: the only C-like language whose users do not understand the concept of zero-indexed arrays.
Programmers of a language are of the same quality of the language. Java gets Java-quality programmers. Go figure.

Only a lousy programmer blames his tools.
Ever used bash, Visual Basic, ...?

>>16
I have been owned, I had forgotten about step in slices. That's much better, and BTW >>12-13, bow to >>16.

>>17
reverse = (
    lambda
      l:
  l[
   : :-1
       ]
 ) #Is this forced indentation?

Name: Anonymous 2007-03-09 17:11 ID:pID4R5Fe

One word, the forced indentation of code. Thread over.

Name: Anonymous 2007-03-09 17:18 ID:q5lbvi5f

Perl:
reverse @l # lol @ shitty unreadable languages ITT

Name: Anonymous 2007-03-09 21:14 ID:pID4R5Fe

(define (reverse list)
 (if (null? list) '()
     (append (reverse (cdr list)) (list (car list)))))

Name: Anonymous 2007-03-09 21:18 ID:pID4R5Fe


(define (last list)
 (if (null? (cdr list))
  (car list)
  (last (cdr list))))

(define (butlast list)
 (if (null? (cdr list))
 '()
  (cons (car list) (butlast (cdr list)))))

(define (reverse list)
 (if (null? list)
 '()
  (cons (last list) (reverse (butlast list)))))

Name: Anonymous 2007-03-09 21:35 ID:pID4R5Fe

>>21
>>22

fucking noobs..

(fold cons '() list)

Name: Anonymous 2007-03-09 21:42 ID:pID4R5Fe

>>23
WOAH?.. I didnt know you could do stuff like that in lisp... cool
I might actually bother learning it someday

Name: Anonymous 2007-03-10 0:03 ID:QV7888IK

If the OP is still here:

You need to count along the indices of the array, not its contents.
   int right = array.length - 1;
is correct, because the last element is at index 4.  The first element is at index 0, so you need this:
   int left = 0;

With left = array[0], left starts out as 1 with the array you gave, so the first element is never swapped in this case.  As well,
   temp = array[left];
becomes
   temp = array[array[0]];
the first time through the loop, and this definitely doesn't look right.

Name: Anonymous 2007-03-10 2:11 ID:3OWcXcQm

why don't you just write a for loop starting from the last cell of the array going to the first cell
ie for(int i = i.length; i > -1; i++)

Name: Anonymous 2009-01-24 18:32

tes

Name: Sgt.Kabu⚓�kimanꗙ뒔 2012-05-28 23:07

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List