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; The even/odd function does not work...here is what I have so far. Everything works except for the evenodd function. I ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    12

    Exclamation Even / Odd Issues

    The even/odd function does not work...here is what I have so far. Everything works except for the evenodd function. I am supposed to get the even numbers to list first, then the odd numbers. It should look like this:

    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


    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)/2; i++)
    {
    if(odd[i]%2 == 0) 
    /* if even integer*/
    evenTemp[i] = odd[i]; 
    /*store even numbers temporary */
    elseoddTemp[i]= odd[i]; 
    /*store odd numbers temporary */
    }
    for(i=0; i<(N-1)/2; i++)
    {
    odd[i]=evenTemp[i]; 
    /*swap even numbers first in the array*/
    odd[N-1-i]=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");
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    SourceForge.net: Indentation - cpwiki
    Your code has no indentation, so as a result it's too hard to follow the flow easily (for us, AND for you).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    is this better?

    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");
    }
    Last edited by akemi.kanegae; 12-06-2012 at 10:20 PM.

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    that does not help
    To the contrary. It helps everyone else undestand your code so they are more likely to help debug it. I am not going to lie I looked at this post over an hour ago and saw there was no indentation as Salem has suggested and I immediately hit the Back key.

    Proper indentation will help you alot.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,571
    Quote Originally Posted by akemi.kanegae View Post
    that does not help
    If you're expecting knowledgeable people to help you, for free - could you at least meet them half way, put in some effort, and make it presentable? Demanding help is not a good way to get assistance from those who are willing to volunteer their time for you.

    Edit: Lesshardtofind beat me to it. And I had the same reaction they describe in their third sentence.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    I did not mean it that way, I meant that it did not help me when I indented it. Not that your information was not helpful. I do not know proper indentation. I re-read what I posted and changed it. It sounded really rude. I apologize. I did not know that proper indentation was needed. Did I do it correctly now? I am unsure. Again, I am sorry for sounding rude. I did not mean it that way at all.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Sorry Salem, I did not mean to sound rude. I indented my program, is that way better? Or did I not do it correctly? Please let me know if I need to change it again.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    I really do appreciate all of your help. I did not mean it sound rude Im sorry everyone.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,571
    It's okay, if it's a misunderstanding. I don't think anyone is snubbing you - it does take some time to study a program, after all.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Ok. I appreciate your reply. Did I indent the program correctly after I edited it?

  11. #11
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Code:
    void evenOdd(int odd[])       
    {            
      int i;            
      int evenTemp[N/2];   // size 9/2         or 4
      int oddTemp[(N-1)/2];  // size 8/2    also 4        
      char tempOdd;             
      for(i=0; i < N-1 ; i++)               
      {                       
        if(odd[i]%2 == 0) /* if even integer*/                      
          evenTemp[i] = odd[i]; // <---- What happens here when the loop has run 8 times but you store a variable into the 7th 
                                        //          address in array with a size of 4                    
        else                      
          oddTemp[i]= odd[i]; /*store odd numbers temporary */  <--- same problem 
       }        
       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 */                
       }               
    }
    Salem likes this.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,571
    Second time in an hour that Lesshardtofind beat me to the punch

    And your indentation could be better - but notice how fast you got an answer when the code was somewhat more readable.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    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 */
      }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Thank you for helping. How can I solve the problem? Do I need to mess with the eventemp[N/2] and oddTemp[(N-1)/2] so they are not the same? and also my if / else loop?

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Quote Originally Posted by Matticus View Post
    Second time in an hour that Lesshardtofind beat me to the punch

    And your indentation could be better - but notice how fast you got an answer when the code was somewhat more readable.
    Thank you I see that. I will work on my indentation!!

Page 1 of 2 12 LastLast
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