Like Tree1Likes
  • 1 Post By AndiPersti

Printing out odd and even values of an array

This is a discussion on Printing out odd and even values of an array within the C Programming forums, part of the General Programming Boards category; Hi guys, I am trying to print the odd and even values of an array 10 per row. I keep ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    41

    Printing out odd and even values of an array

    Hi guys, I am trying to print the odd and even values of an array 10 per row. I keep getting the same number printing out can anyone see if my logic is wrong for identifying the odd and even values? Thank you.
    Code:
     int array[1000] = {0};
     int i = 0;
     int j = 0;
     int k = 0;
     
      
     for(i = 0; i<1000; i++)
     {
      array[i] = rand();
     }
      i =0;  
     
      for(j = 0; j<100; j++)
      {
        for(k = 0; k<10; k++)
        {
         printf("%6d", array[i]);
         i++;
        }
       printf("\n");
       
      }
      printf("Odd and even\n\n\n");
      i = 0;
      j = 0;
      k = 0;
       
     
       if (array[i] == (array[i]+1)/2 != 0 )
        { 
        
          for(j = 0; j<100; j++)
           {
            for(k = 0; k<10; k++)
             {
              printf("%6d", array[i]);
              i++;
             }
            printf("\n");
           }
        
       
          printf("&6d", array[i]);
          
        
        }
       else
       {
        for(j = 0; j<100; j++)
        {
         for(k = 0; k<10; k++)
         {
         printf("%6d", array[i]);
         
         system("pause");
         }
        printf("\n");
        }
    
     
       }
    
      system("pause");
     }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,284
    For an odd even test you can use:

    Code:
     if ((array[i]%2) != 0 )
    


    or

    Code:
     if ((array[i]&1) != 0 )
    



  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    Thanks I'll update that

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    Trying to figure out what I have done wrong but the second part were it is supposed to determine if it is a odd or even just prints the number 41

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    333
    Quote Originally Posted by phillyflyers View Post
    Trying to figure out what I have done wrong but the second part were it is supposed to determine if it is a odd or even just prints the number 41
    That's because you set i to zero, but never loop on i.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    Ok I've worked it a little further Malcolm should I change the odd and even counter to 1? I dont think I have the code right. I'm tyring to put the odd and even values in separate arrays. Thanks guys
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
     
     int array[1000] = {0};
     int i = 0;
     int j = 0;
     int k = 0;
     int oddcounter = 0;
     int evencounter = 0;
     int oddarray[500];
     int evenarray[500];
    
     for(i = 0; i<1000; i++)
     {
      array[i] = rand();
     }
      i =0;  
     
      for(j = 0; j<100; j++)
      {
        for(k = 0; k<10; k++)
        {
         printf("%6d", array[i]);
         i++;
        }
       printf("\n");
       
      }
      printf("Odd and even\n\n\n");
      system("pause");
      i = 0;
      j = 0;
      k = 0;
       
    
      for(j = 0; j<100; j++)
      {
        for(k = 0; k<10; k++)
        {
         printf("%6d", array[i]);
         i++;
        }
       printf("\n");
     
       if (array[i] %2 == 0 )
       {
        evenarray[evencounter] = array[i];
        evencounter++;
       }
       else
       {
        oddarray[oddcounter] = array[i];
        oddcounter++;
       }
       printf("%6d", oddarray[oddcounter]); //just to see if it was doing it right
       
      }
      
      
      
      
      system("pause");
    }

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,284
    Quote Originally Posted by phillyflyers View Post
    should I change the odd and even counter to 1?
    C arrays are indexed starting with 0, so you already have it correct.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    Thanks but I must have something wrong it doesnt seem to be passing the odd or even values to the odd/even arrays

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,284
    Quote Originally Posted by phillyflyers View Post
    Thanks but I must have something wrong it doesnt seem to be passing the odd or even values to the odd/even arrays
    In the loop where you count evens and odds, you're incrementing i in the loop that prints, so your program only tests 1 out of every 10 values in array.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int oddarray[500];
    int evenarray[500];
    You have 1000 random numbers and it's very likely that the split between odd and even numbers won't be exactly 500:500. Hence, there will be a buffer overflow in one array.
    Since you are a beginner I suggest both arrays should be able to hold 1000 elements.

    On line 57 you are printing an uninitialized value in case of an odd number because "oddcounter" is already incremented.

    It's also better to not mix processing with output. So write a loop which puts each number in the correct array. And then write a function which outputs an array of any size and use this function to print the even and the odd array.

    Bye, Andreas
    laserlight likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble printing x and y values of struct points in an array?
    By arcadedragon in forum C Programming
    Replies: 6
    Last Post: 10-03-2012, 10:15 AM
  2. Replies: 3
    Last Post: 10-21-2010, 12:39 PM
  3. Printing values from Enum
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 02-16-2009, 09:28 PM
  4. stuck printing values from array
    By Guti14 in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2003, 09:56 PM
  5. printing values loop.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-02-2002, 02:56 PM

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