Like Tree1Likes

Even / Odd Issues

This is a discussion on Even / Odd Issues within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Salem I call this indented code. Code: #include <stdio.h> #include <stdlib.h> #define N 9 /* MAIN */ ...

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Quote Originally Posted by Salem View Post
    I call this indented code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 9                     /* MAIN */
    
    void revArray(int arr[])
    {
      int temp[N];
      int i;
    
      for (i = 0; i < N; ++i) {
        temp[i] = arr[N - 1 - i];
      }
    
      for (i = 0; i < N; ++i) {
        arr[i] = temp[i];
      }
    }
    
    int *revElement(int arr[])
    {
      static int idx = N;
      idx = --idx;
      return arr + idx;
    }
    
    void evenOdd(int odd[])
    {
      int i;
      int evenTemp[N / 2];
      int oddTemp[(N - 1) / 2];
      char tempOdd;
    
      for (i = 0; i < N - 1; i++) {
        if (odd[i] % 2 == 0)        /* if even integer */
          evenTemp[i] = odd[i];     /*store even numbers temporary */
        else
          oddTemp[i] = odd[i];      /*store odd numbers temporary */
      }
    
      for (i = 0; i < (N / 2); ++i) {
        odd[i] = evenTemp[i];       /*swap even numbers first in the array */
        odd[i - N] = oddTemp[i];    /*swap odd numbers last in the array */
      }
    }
    
    int main(int argc, char **argv)
    {
      int a[N];
      int idx;
      int *p = a;
    
      while (p < a + N)
        *p++ = a + N - p;
    
      printf("Original: ");
      p = a;
      while (p < a + N)
        printf("%2d ", *p++);       /* Part A: Reverse the array */
    
      revArray(a);
      printf("\nReversed: ");
      p = a;
      while (p < a + N)
        printf("%2d ", *p++);
      printf("\n");                 /* Part B: Return elements in reverse order */
    
      printf("Original: ");
      for (idx = 0; idx < N; idx++) {
        printf("%2d ", *revElement(a));
      }
    
      printf("\n");                 /* Part C: Put even numbers first, odd numbers last in the array. Order of
                                       the elements is not important as long as all evens are before first odd */
    
      printf("Even:     ");
      evenOdd(a);
      p = a;
      while (p < a + N)
        printf("%2d ", *p++);
    
      printf("\n");
      system("pause");
    }
    Original: 9 8 7 6 5 4 3 2 1
    Reversed: 1 2 3 4 5 6 7 8 9
    Original: 9 8 7 6 5 4 3 2 1
    Even: 2 4 6 8 5 3 7 9 1

    What is the order of the odd numbers at the end of the array? It seems entirely random to me.

    Code:
      //!! you're assuming an equal balance of even and odd numbers.
      //!! what if the input array was all even numbers?
      int evenTemp[N / 2];
      int oddTemp[(N - 1) / 2];
    
      for (i = 0; i < N - 1; i++) {
        if (odd[i] % 2 == 0)        /* if even integer */
          //!! you need separate subscripts for each array.
          //!! at the moment, you make two copies of the input array (which overflow both local arrays)
          evenTemp[i] = odd[i];     /*store even numbers temporary */
        else
          oddTemp[i] = odd[i];      /*store odd numbers temporary */
      }
    The order of the numbers at the end do not matter, as long as all the even come first.

  2. #17
    Registered User
    Join Date
    May 2009
    Posts
    2,548
    Code:
    Original: 9 8 7 6 5 4 3 2 1
    Even: 2 4 6 8 5 3 7 9 1
    One way you could do it by hand.

    look for leftmost odd value swap with rightmost even value

    Code:
    9 8 7 6 5 4 3 2 1
    2 8 7 6 5 4 3 9 1
    2 8 4 6 5 7 3 9 1
    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #18
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    You could add two integers being an evencount and an oddcount to increment when you save into the respective arrays.

    Code:
    int evenTemp[N/2];
      int oddTemp[(N-1)/2];
      int evencount = 0;
      int oddcount = 0;
      char tempOdd;
         
      for(i=0; i < N-1 ; i++)
      {
        if(odd[i]%2 == 0) /* if even integer*/
        {
          evenTemp[evencount] = odd[i]; /*store even numbers temporary */
          evencount++;
        }
        else
        {
          oddTemp[oddcount]= odd[i]; /*store odd numbers temporary */
          oddcount++;
        }
      }
    You still need to look at why your two arrays add up to 8 in size but your final array needs 9 numbers
    Last edited by Lesshardtofind; 12-06-2012 at 11:08 PM.

  4. #19
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Quote Originally Posted by Salem View Post

    Original: 9 8 7 6 5 4 3 2 1
    Reversed: 1 2 3 4 5 6 7 8 9
    Original: 9 8 7 6 5 4 3 2 1
    Even: 2 4 6 8 5 3 7 9 1

    What is the order of the odd numbers at the end of the array? It seems entirely random to me.
    The order doesnt matter, as long as the even numbers come first, followed by the odd

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having issues with my FFT
    By DavidP in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2010, 01:27 PM
  2. issues
    By OrAnGeWorX in forum C Programming
    Replies: 35
    Last Post: 11-18-2008, 11:18 PM
  3. Issues
    By Tamandt in forum C++ Programming
    Replies: 22
    Last Post: 09-10-2004, 11:42 AM
  4. PC issues....
    By RoD in forum Tech Board
    Replies: 10
    Last Post: 03-11-2004, 04:44 PM
  5. .EXE Issues
    By SpankyTheWalrus in forum Windows Programming
    Replies: 4
    Last Post: 03-07-2004, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21