+ Reply to Thread
Results 1 to 8 of 8

Thread: LOLs programming fun #2     submit to reddit submit to twitter

  1. #1
    Sea Torques
    Join Date
    Jul 2005
    Posts
    641
    BG Level
    5

    LOLs programming fun #2

    Im bored in programming class trying to figure out how to inverse an array

    given if i made an array where i gave it random numbers i.e 2,34,43,23,52

    and they are stored in each 0-4 cases in my array, how do i make so 52 is stored in 0, 23 in 1, and so on?


    ::edit:: rofl nvm....

    #include<stdio.h>
    int main(void)
    {
    int numbers[30],n,i,j,c;
    printf("Gimme your numbers yay(Max.30):");
    scanf("%d",&n);
    for(i=0;i<n;i++){
    printf("Numbers:");
    scanf("%d",&numbers[i]);
    }
    printf("The numbers are:\n");
    for(i=0;i<n;i++){
    printf("%d\n",numbers[i]);
    }

    for(i=0,j=n-1;i!=j;i++,j--){
    c=numbers[i];
    numbers[i]=numbers[j];
    numeros[j]=c;
    }
    printf("The inverted numbers are:\n");
    for(i=0;i<n;i++){
    printf("%d\n",numbers[i]);
    }

    }//endmain

  2. #2

    do your own homework.

  3. #3
    Sea Torques
    Join Date
    Jul 2005
    Posts
    641
    BG Level
    5

    It's not homework. It was optional excercises while the rest of the people finished.

  4. #4
    Ridill
    Join Date
    Aug 2004
    Posts
    12,469
    BG Level
    9
    FFXIV Character
    Septimus Atumre
    FFXIV Server
    Gilgamesh
    FFXI Server
    Bahamut

    Actionscript you code would be...

    Code:
    arrayName = arrayName.reverse();
    In PHP it would be

    Code:
    $arrayName = array_reverse($arrayName);
    I would hope C++ would have such a beast.

  5. #5

    for(i=0,j=n-1;i!=j;i++,j--)
    {
    c=numbers[i];
    numbers[i]=numbers[j];
    numeros[j]=c;
    }

    Pretty clunky code. Why count up and down at the same time?

    for(i=0; i <= (int)n/2; ++i)
    {
    c=numbers[i];
    numbers[i]=numbers[n-i-1];
    numeros[n-i-1]=c;
    }

    does the trick just fine and much easier to read. BTW, always use ++x in a loop, not x++. On most systems it doesn't make a difference, but on a system where no (or poor) optimization takes place, ++x is faster than x++. Furthermore, in C++ as opposed to C, you can have an object there with an overloaded ++ operator. In that case, regardless of optimization, using ++x is -much- faster than x++, because the second one has to create a temporary object, then perform the operation, then return the temporary object whereas the first one only has to perform the operation and return it.


    That aside, if you really want to freak your teacher (and yourself) out, change the loop to this:

    for(i=0; i <= (int)n/2; ++i)
    {
    numbers[i] = numbers[i] ^ numbers[j];
    numbers[j] = numbers[i] ^ numbers[j];
    numbers[i] = numbers[i] ^ numbers[j];
    }

    Still gets the job done, but why?

  6. #6

    LETTERS AND NUMBERS

  7. #7
    Melee Summoner
    Join Date
    Sep 2005
    Posts
    40
    BG Level
    1

    LOUD NOISES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  8. #8
    darkeraven
    Guest

    XOR A,B
    XOR B,A
    XOR A,B

Similar Threads

  1. Programming FUN!!!
    By Lasitha in forum General Discussion
    Replies: 26
    Last Post: 2005-09-29, 16:56
  2. LOL!! Just saying hello! from work lol
    By Fruitloopsakaevalime in forum General Discussion
    Replies: 13
    Last Post: 2004-09-29, 12:32
  3. Lol, why'd I quit? :P
    By Isus in forum General Discussion
    Replies: 4
    Last Post: 2004-09-21, 20:10