954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Separating Odd & Even Numbers from Array

Hi the aim of my program is to allow a user to enter the size of an array and then allows the user to enter the values into an array. The bit im having a problem with is I want to count how many odd and even numbers there are in the array. I then want to print out the odd and even numbers. I've had a go but its not working-I was hoping that someone could look at the code and see where Im going wrong or if you've got another way to do it please reply.

# include <stdio.h>
# define MAX 100
int main(void)
{
    int oddcount, evencount;
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); // User enters Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("\n %d", &array[i]); // Allows user to enter values into the Array
       }
    
    putchar('\n');
    printf("\nArray Values: ");
    for(i = 0; i < n; i++)
    {
       printf("%3d ",array[i]); //Prints out the array
    }
     //**************************************//
  {
    for (i = 0; i < n; i++);   // This section of the code is meant to determine 
                               // whether the values in the array are odd or even.
   
     oddcount = 0;
    evencount = 0;
 
      if (i%2 != 0)
        {
            printf("\nThe number %d Is Odd.\n", i);
            oddcount++;
        }
        else
        {
            printf("\nThe number %d Is Even.\n",i);
            evencount++;
    
    getch();
    return 0;
}
}


Thanks
TrueCoding

TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

So were you suppose to fill in the blank space loop with your code?

You need to actual encase the loop in brackets, and add one to your else statement.

You need to initialize those variables as zero, if you do it in the loop you reset it every time.

You can place the printfs outside of the loop at the end of the program so they don't repeat everytime you add a number.

Simplified, something like this:

for(...) {
    if(array[i] & 1)
        odd++;
    else
        even++;
}
...
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

Ok Ive done what you said but its still not working...I think there something im doing with the if statement? Thanks for your help by the way

# include <stdio.h>
# define MAX 100
int main(void)
{
    int oddcount, evencount;
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); // User enters Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("\n %d", &array[i]); // Allows user to enter values into the Array
       }
    
    putchar('\n');
    printf("\nArray Values: ");
    for(i = 0; i < n; i++)
    {
       printf("%3d ",array[i]); //Prints out the array
    }
     //**************************************//
  {
    for (i = 0; i < n; i++);   // This section of the code is meant to determine 
                               // whether the values in the array are odd or even.
 
     oddcount = 0;
    evencount = 0;}
 
      {if (array[i]&1);}
        {
            printf("\nThe number %d Is Odd.\n", i);
            oddcount++;
            }
       
       
           { else
        
            printf("\nThe number %d Is Even.\n",i);
            evencount++;}
    
    getch();
    return 0;
}
}
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

A couple of things:

for (i = 0; i < n; i++) << ; >>   // This section of the code is meant to determine 
                               // whether the values in the array are odd or even.
 
//remove the above semi-colon from the for loop 

    oddcount = 0;
    evencount = 0;<strong>}</strong>


Don't put closing braces on the ends of the line of code. Closing braces go ONLY directly underneath (in the same column), as the opening brace OR the first letter of the line of code that they are part of:

for(......) {
  //other code here
}
//or

for(......)
{
   //other code here
}
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Ok so Ive done what you've told me but there is still something wrong with the code-it keeps coming up with an error with the else statement ?

# include <stdio.h>
# define MAX 100
int main(void)
{
    int oddcount, evencount;
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); // User enters Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("\n %d", &array[i]); // Allows user to enter values into the Array
       }
    
    putchar('\n');
    printf("\nArray Values: ");
    for(i = 0; i < n; i++)
    {
       printf("%3d ",array[i]); //Prints out the array
    }
     //**************************************//
  
     for (i = 0; i < n; i++) // This section of the code is meant to determine 
                               // whether the values in the array are odd or even.
    oddcount = 0;
    evencount = 0;
{
   
if (array[i]&1);

            printf("\nThe number %d Is Odd.\n", i);
            oddcount++;     


 else
            printf("\nThe number %d Is Even.\n",i);
            evencount++;


    getch();
    return 0;
}
}
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

The semi-colon inside the << >> is incorrect. Delete it.

the red closing brace is something that will cause a lot of headaches if you keep putting them on the end of a line of code.

Don't do that -- they get over-looked. Employer's dislike it, for that reason.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Sorry I edited that earlier post-I changed the post..Forget what i wrote earslier

TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

If the code following is only a single action, then you don't need brakets but if you're using more than one you need:

if(logic) 
{
    func();
    func2();
}
else
    only_one_func();

something_now_outside_the_scope();
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

Ok Ive done the layout you've suggested Mosaic but I keep getting error with that else statement?

# include <stdio.h>
# define MAX 100
int main(void)
{
    int oddcount, evencount;
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); // User enters Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("\n %d", &array[i]); // Allows user to enter values into the Array
       }
    
    putchar('\n');
    printf("\nArray Values: ");
    for(i = 0; i < n; i++)
    {
       printf("%3d ",array[i]); //Prints out the array
    }
     //**************************************//
  
     for (i = 0; i < n; i++) // This section of the code is meant to determine 
                               // whether the values in the array are odd or even.
    oddcount = 0;
    evencount = 0;


if (array[i]%2 != 0);
{
            printf("\nThe number %d Is Odd.\n", i);
            oddcount++;     
}

 else
            printf("\nThe number %d Is Even.\n",i);
            evencount++;


    getch();
    return 0;
}
}
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

You and your semi-colons!

if (array[i]%2 != 0); //delete this semi-colon!
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Oh sorry it was that semi coloon! Thanks but the program runs but it only goes up to the bit where it prints out the array it seems to have ignored the code after the printing of the array?

TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

You still using two actions in a scope that only uses one...

Why don't you just put the printfs at the end of program as I suggested? This eliminated this issue, and doesn't print the message a dozen times on the console.

Your loop still doesn't even encase the conditions, and you still haven't moved the variable initialization to the top.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

Mosaic has kindly made several suggestions about your code problems.

Let's look at your code.

# include <stdio.h>
# define MAX 100
int main(void)
{
    int oddcount, evencount;
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); // User enters Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 

     // things like /n don't go inside scanf()
       scanf("%d", &array[i]); // Allows user to enter values into the Array
   }
    
    putchar('\n');
    printf("\nArray Values: ");
    for(i = 0; i < n; i++)
    {
       printf("%3d ",array[i]); //Prints out the array
    }
     //**************************************//
  
    // whether the values in the array are odd or even.
    for (i = 0; i < n; i++) // This section of the code is meant to determine 
       

/* 8888888888888888888888888888888888888888888888888888
move these two lines to ABOVE the start of this for loop it's inside of now 
    oddcount = 0;
    evencount = 0;

They don't belong down here.
8888888888888888888888888888888888888888888888888888
*/
       if (array[i]%2 != 0)  //removed your semi-colon
       {
          printf("\nThe number %d Is Odd.\n", i);
          oddcount++;     
       }
       else {  //added a brace
          printf("\nThe number %d Is Even.\n",i);
          evencount++;
       }  //and it's matching brace

    } //end of your for loop
    getch();
    return 0;
  }
}


That has several corrections and a note for you to move some assignments, so make that move of the assignments, and see if that isn't better.

You'll want to add the print statement for the number of odd and even numbers that were found, near the end of the program,

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Ok thanks for all your help Ive changed it now like you said but its still skipping the code at the end and I dont know why?

# include <stdio.h>
# define MAX 100
int main(void)
{
    int oddcount, evencount;
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); // User enters Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("%d", &array[i]); // Allows user to enter values into the Array
       }
    
    putchar('\n');
    printf("\nArray Values: ");
    for(i = 0; i < n; i++)
    {
       printf("%3d ",array[i]); //Prints out the array
    }
     //**************************************//
    oddcount = 0;
    evencount = 0;

     for (i = 0; i < n; i++) // This section of the code is meant to determine 
                               // whether the values in the array are odd or even.


if (array[i]%2 != 0)
{
            printf("\nThe number %d Is Odd.\n", i);
            oddcount++;     
}

 else {
            printf("\nThe number %d Is Even.\n",i);
            evencount++;
      }

    
    return 0;
}
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

You need opening and closing braces around the body of the last for loop.

Opening - line 29
Closing - on line 43

Any multiple line statement block of code, requires braces around it.
For example:

for(i=0;i<3;i++) {
  printf("\nThis is loop number: %d", i+1);
  printf("And this is being printed only once without the braces");
}
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Ok as you can see Ive dont that as well but still the program closes after printing the array?

oddcount = 0;
    evencount = 0;

     for (i = 0; i < n; i++) // This section of the code is meant to determine 
                               // whether the values in the array are odd or even.
{
if (array[i]%2 != 0)
{
            printf("\nThe number %d Is Odd.\n", i);
            oddcount++;     
}

 else {
            printf("\nThe number %d Is Even.\n",i);
            evencount++;
      }
}
    
    return 0;
}
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

Use a debugger and follow the code.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

I dont what a debugger is? WHere do I get it?

TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

What IDE are you using? Most have an easy to use one that requires nothing more than clicking on a line to watch it.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

Umm Im using Dev C++. Oh Ive found it now but its not helping it just stops at line where it prints the array and it stops there

TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: