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

Pages: 1-

A little help please

Name: Anonymous 2007-09-05 15:29 ID:so4w/yz5

void reverseHalves(int a[], int size)
{
    int i, t;

      if((size%2) != 0)  Size ODD
      {
        for(i=0;i<=((size/2)-1);i++)
        {
            t = a[i];
            a[i] = a[(size/2)+i+1];
            a[(size/2)+i+1] = t;
        }
      }

      if((size%2) == 0)  Size EVEN
      {
        for(i=0;i<=((size/2)-1);i++)
         {
              t = a[i];
              a[i] = a[(size/2)+i];
              a[(size/2)+i] = t;
          }
      }

    return;
}


This function swaps the halves of an array.

e.g.

1
2
3
4

comes out as:

3
4
1
2

I handed it in to my professor and he said he wants me to do it over using only one loop. The reason I used two loops in the first place because the program would crash when based on whether or not an odd/even amount of elements were present for the array. He said I could used a nested for loop, but I haven't been able to figure out how to create it.

Name: Anonymous 2007-09-05 15:35 ID:yzo3IaMG

PROTIP: meditate on size%2. It actually means something.

Name: Anonymous 2007-09-05 15:37 ID:yzo3IaMG

Also, if they taught you this crappy coding style in the course you're attending, quit, and go to a proper school.

Name: Anonymous 2007-09-05 15:40 ID:so4w/yz5

Meditate on size%2? What are you talking about? =P Since I have to write only one loop now, I can't use size%2. =(

I asked on another forum and got this one reply:
====
Divide the size by 2 to get the number of elements you need to affect, remembering that integer division will ignore the extra number if we have an odd size.

For this many elements, set i to 0, and j to size - number of elements. Swap i and j using a temp value, then increment the subscript.
=====

Although, I still have no idea what this guy was talking about. I'm unable to create a mental picture of what he's saying.

Name: Anonymous 2007-09-05 15:41 ID:so4w/yz5

>>Also, if they taught you this crappy coding style in the course you're attending, quit, and go to a proper school.


lol. I learned to code out of a book. The title is something like "Learn C in 24 hours".

Name: Anonymous 2007-09-05 15:42 ID:yzo3IaMG

Since I have to write only one loop now, I can't use size%2.
I said that it actually means something. It's not just some magic word you use to determine whether some number is even or not. Find out what values it returns and how they are useful to you.

Name: Anonymous 2007-09-05 15:43 ID:Heaven

>>5
Read SICP and don't come back before you've finished it.

Name: Anonymous 2007-09-05 15:45 ID:so4w/yz5

>>Read SICP and don't come back before you've finished it.

What is SICP?

Name: Anonymous 2007-09-05 15:48 ID:Heaven

>>8
What is SICP?
I never imagined I would see those words on /prog/.

Name: Anonymous 2007-09-05 15:50 ID:Heaven

instead of n % 2 try n & 1
you fag

Name: Anonymous 2007-09-05 15:51 ID:1MmKu1HW

(define (reverse-halves a)
  (define (loop middle end first-halve)
    (if (or (null? end)
            (null? (cdr end)))
        (append middle (reverse first-halve))
        (loop (cdr middle) (cdr (cdr end)) (cons (car middle) first-halve))))
  (loop a a '()))


(reverse-halves '(1 2 3 4 5 6 7 8 9))
(5 6 7 8 9 1 2 3 4)

Name: Anonymous 2007-09-05 15:51 ID:Heaven

>>10
FUCK YEAH. 35% FASTER.

Name: Anonymous 2007-09-05 15:54 ID:Heaven

def reverse_halves(l):
    mid = len(l) // 2
    return l[mid:] + l[:mid]

Name: Anonymous 2007-09-05 16:06 ID:B+7iYOuT

reverse = lambda a: a[len(a) >> 1:] + a[:len(a) >>1]

Name: Anonymous 2007-09-05 16:09 ID:Heaven

>>14
The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague. In the case of a well-known conversational programming language I have been told from various sides that as soon as a programming community is equipped with a terminal for it, a specific phenomenon occurs that even has a well-established name: it is called "the one-liners". It takes one of two different forms: one programmer places a one-line program on the desk of another and either he proudly tells what it does and adds the question "Can you code this in less symbols?" —as if this were of any conceptual relevance!— or he just asks "Guess what it does!". From this observation we must conclude that this language as a tool is an open invitation for clever tricks; and while exactly this may be the explanation for some of its appeal, viz. to those who like to show how clever they are, I am sorry, but I must regard this as one of the most damning things that can be said about a programming language.

Name: Anonymous 2007-09-05 16:37 ID:Heaven

>>15
My other car is a cdr

Name: Anonymous 2007-09-05 16:50 ID:GNRoWW3k

>>12
I lol'd

Name: Anonymous 2007-09-05 17:03 ID:so4w/yz5

lol. Still trying. I haven't figured it out. I'll post back and let you know if I figured it out.

Name: Anonymous 2007-09-05 17:51 ID:so4w/yz5

This should be OK, right?
===========================================
void reverseHalves(int a[], int size)
{
    int i, t;

    for(i=0;i<=((size/2)-1);i++)
    {
        if(size%2 != 0)
        {
            t = a[i];
            a[i] = a[(size/2)+i+1];
            a[(size/2)+i+1] = t;
        }
        else
        {
            t = a[i];
            a[i] = a[(size/2)+i];
            a[(size/2)+i] = t;
        }
    }
===========================================


He SAID write a function that will switch the halves of an array. When I handed in what I posted in the original code, he said redo it with ONE loop only. I then asked him if it could be done with a nested FOR loop. He said yes but didn't elude to any hints.

Also, since our class only contains six people, he said we're not allowed to help each other so I can't see what anyone else did.

I don't think he wanted me to do it with a nested for loop. I mean, TECHNICALLY, isn't a nested for loop more than ONE loop? It's freaking loops inside of loops.

Name: Anonymous 2007-09-05 21:28 ID:aW8NGxYB

void swap_halves(int *a,int size){
 size_t s=sizeof(int)*size/2
 int t[size/2];
 int *p=a+size/2+size%2;
 memcpy(t,p,s);
 memcpy(p,a,s);
 memcpy(a,t,s);
}

Name: Anonymous 2007-09-05 21:43 ID:j5es3+LP

>>20
there is a loop inside the memcpy, I don't think it counts.
what you need to see is that you need to switch elements. so it's a single loop to the middle of the array.
the algorithm is something like
1 2 3
 
let i = 0, half = (size+1)/2
switch array[i] with array[i+half]
increment i and loop if i < half - 1

I think it works.

Name: Anonymous 2007-09-05 21:45 ID:j5es3+LP

actually, it doesn't. let the loop condition be

i < half - (size % 2 ? 1 : 0)

I think that really works.

Name: Anonymous 2007-09-05 22:59 ID:Heaven

>>20
int t[size/2];
that's not valid C/C++. you can't statically allocate a variable amount - you have to throw that shit on the heap, unfortunately.

void swap_halves( int *a, unsigned int s ) {
    for ( int i = 0; i < ( ( s >> 1 ) + ( s & 1 ) ); ++i ) {
        a[i] ^= a[ i + ( s >> 1 ) + ( s & 1 ) ];
        a[ i + ( s >> 1 ) + ( s & 1 ) ] ^= a[i];
        a[i] ^= a[ i + ( s >> 1 ) + ( s & 1 ) ];
    }
}

tee hee.

Name: Anonymous 2007-09-05 23:08 ID:Heaven

>>23
Actually, I fucked up. That isn't going to work for odd numbers.

Name: Anonymous 2007-09-05 23:23 ID:Heaven

that's not valid C/C++. you can't statically allocate a variable amount - you have to throw that shit on the heap, unfortunately.
just change that to int *t=malloc(s) and add free(t) at the end and then it'll work.

Name: Anonymous 2007-09-06 5:13 ID:dwXb23Kn

>>23
that's not valid C/C++. you can't statically allocate a variable amount - you have to throw that shit on the heap, unfortunately.
C99, MOTHEFUCKER, DO YOU KNOW IT?

Name: Anonymous 2007-09-06 10:39 ID:Heaven

>>26
JESUS CHRIST C99 WHAT THE FUCK IS WRONG WITH ME?!

Also, fixed my implementation. It lost all it's pretty operators, unfortunately.

void swap_halves( int* a, unsigned int s ) {
   for( int i = 0; i < ( s >> 1 ); ++i ) {
      for( int j = s-1; j > 0; --j ) {
         a[0] ^= a[j];
         a[j] ^= a[0];
         a[0] ^= a[j];
      }
   }
}


Blah.

Name: Anonymous 2007-09-06 12:26 ID:xG3dNEZB

>>27
Lose the XOR trick. Temporary variables are easier to read and easier for the compiler to optimize.

Name: Anonymous 2007-09-06 12:30 ID:Heaven

>>28
Not to mention temporary variables actually work in situations other than a raw 1-word data swap. I just did it for the wank.

Name: Anonymous 2007-09-06 15:39 ID:Heaven

Fair enough. Can't argue with the wank factor.

Name: Anonymous 2011-01-31 20:09

<-- check em dubz

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