Thread: Sorting Arrays - Even and Odd Input

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    21

    Sorting Arrays - Even and Odd Input

    I was wondering if I am on the right track with this one.

    I have to sort even and odd input numbers of an array, placing the even numbers at the front of the array, and the odd at the end.


    Code:
     
    
    for (iCntr = 0; iCntr < DIM; iCntr++) { 
          printf("%d > ", iCntr);
          fscanf(stdin, "%d", &iInput[iCntr]); 
    }
    for (iCntr = 0; iCntr < DIM; iCntr++) {
         if (iCntr % 2)
             iOdd = iCntr;
         else
             iEven = iCntr;
        }
    }
    for (iOdd < DIM; iOdd++) {     
         printf(" %d \n", iInput[iOdd]);    
    }
    for (iEven < DIM; iEven++) {      
          printf(" %d \n", iInput[iEven]);    
    }

    Does this look even remotely correct to anyone?
    I know the printf part isn't because I keep getting errors, but I have to work more on that.

    Thanks for your time.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have to sort even and odd input numbers of an array, placing the even numbers at the front of the array, and the odd at the end.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int eo ( const void *a, const void *b )
    {
      int ia = *(int *)a;
      int ib = *(int *)b;
    
      if ( ia % 2 == 0 && ib % 2 != 0 )
        return -1;
      else if ( ia % 2 != 0 && ib % 2 == 0 )
        return +1;
      else
        return 0;
    }
    
    int main ( void )
    {
      int a[] = {1,2,3,4,5,6,7,8,9,0};
      int i;
    
      for ( i = 0; i < 10; i++ )
        printf ( "%d ", a[i] );
      printf ( "\n" );
      qsort ( a, 10, sizeof ( int ), eo );
      for ( i = 0; i < 10; i++ )
        printf ( "%d ", a[i] );
      printf ( "\n" );
    
      return 0;
    }
    Replace qsort with your favorite sorting algorithm if you aren't familiar with it or aren't allowed to use it.
    My best code is written with the delete key.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    is DIM the sizeof the array or the number of elements in the array or what?

    for speed you could do this
    Code:
         if (iCntr & 1 == 0) /*the number is even*/
             iOdd = iCntr;
         else
             iEven = iCntr;
    and why do you have the } at the end of the statement here
    Code:
    for (iCntr = 0; iCntr < DIM; iCntr++) {
         if (iCntr % 2)
             iOdd = iCntr;
         else
             iEven = iCntr;
        }
    }

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Thanks for replying everyone.

    I probably had a typo; I didn't copy and paste but retyped.

    Thanks for the help, I understand more now.

  5. #5
    .
    Join Date
    Nov 2003
    Posts
    307
    Prelude's code shortend up a little
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define DIM 10
    int comp(void *, void *);
    
    int main(int argc, char *argv[]){
        int iInput[DIM];
        int iCntr=0;
        for (iCntr = 0; iCntr < DIM; iCntr++) { 
              printf("%d > ", iCntr);
              fscanf(stdin, "%d", &iInput[iCntr]); 
        }
        qsort(iInput,DIM,sizeof(int),comp);
        for (iCntr = 0; iCntr < DIM; iCntr++) { 
              printf("%d \n", iInput[iCntr]);
        }
        return 0;
    }
    int comp(void *a,void *b){
        return *(int *)a%2 - *(int *)b%2;
    }

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    I'm new to the qsort function also. Is it just a shortcut that I probably haven't read about in my intro book yet?

    Also, my book has void main() everywhere and I noticed you had
    Code:
    int comp (void *, void *);
    and also a return value. do I just place my variables where you have the asterisk?

    Sorry if I am a little slow...haha.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is it just a shortcut that I probably haven't read about in my intro book yet?
    qsort is a function in the standard library. Since sorting is such a common operation, it makes sense to have a generic function do it for you instead of having to continually roll your own. That way we don't have to write and debug complex sorting algorithms all the time.

    >Also, my book has void main() everywhere
    Your book is wrong, it's:
    Code:
    int main ( void )
    >do I just place my variables where you have the asterisk?
    The asterisks are telling you that the variables will be pointers. In function prototypes you can omit the variable names, but they must be there in the definition, as shown by:
    Code:
    int comp(void *a,void *b){
        return *(int *)a%2 - *(int *)b%2;
    }
    My best code is written with the delete key.

  8. #8
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    sounds like you haven't learned about function yet, so here goes a basic explanation:
    in small programs all the code may go in main, but when your program grows, you may find yourself repeating pieces of code over and over, and the idea of functions comes in, a function is a piece of code you define once, and then you may call it whenever you like in you code, example:
    Code:
    int add(int x, int y) {
        return x + y;
    }
    
    void main() {
        int apples = 3, oranges = 7;
        int count = add(apples, oranges);
        cout << cout; // output is 10
    }
    when you call "add(apples, oranges);" code in add function executes, giving x the value of apples, and y the value of oranges, then add returns x+y which is assigned to cout.
    qsort is a function, give an array to sort, and a sort function, it sorts the array, may sound a bit advance for now, more info:
    http://cppreference.com/stdother_details.html#qsort

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    int comp(void *a,void *b){
        return *(int *)a%2 - *(int *)b%2;
    }
    This does not return the correct value when there is integer overflow. The prototype of a function used with qsort should have const pointers.
    Code:
    int comp(const void *a, const void *b);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Thanks for the link, it helped explain the qsort function.


    I am still a little confused about the pointers though. When I typed in the code, it said that I had a redeclaration of 'comp' previously declared at (my program.c).

    What does that mean?

    Also, when I execute it, it does not let me enter ten integers. It just says return value -1 and press any key to continue.

    It could be because the redeclaration error won't let it compile properly though. I am not sure.

  11. #11
    .
    Join Date
    Nov 2003
    Posts
    307
    Dave is correct about const void * - that is the prototype in stdlib.h

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    What kind of function is comp? It says something about a redeclaration of comp as int at line 5 as an error??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define DIM 10
    
    int comp(void *, void *); 
    int main(int argc, char *argv[]){
        
    int iInput[DIM];
    int iCntr=0;
        
        for (iCntr = 0; iCntr < DIM; iCntr++) {
              printf("%d > ", iCntr);
              fscanf(stdin, "%d", &iInput[iCntr]);
        }
        qsort(iInput,DIM,sizeof(int),comp);
        for (iCntr = 0; iCntr < DIM; iCntr++) {
              printf("%d \n", iInput[iCntr]);
        }
        return 0;
        }
      int comp(const void *a,const void *b) {
        return *(int *)a%2 - *(int *)b%2;
    }
    (I also haven't learned about pointers, again, apologies for my slowness)

  13. #13
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    > What kind of function is comp? It says something about a redeclaration of comp as int at line 5 as an error??
    just change the prototype at line 5 to :
    int comp(const void *a,const void *b);

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Geez, now I feel stupid


    Thank you very much for your help.

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Is it possible to add something to this program to make it so the latter half, the odd numbers, are also in the same order they began in at the end of the array?

    Ex:

    before: 1428754744
    after: 4284441757

    Thanks

Popular pages Recent additions subscribe to a feed