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

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++;
}
...

Edited 5 Years Ago by MosaicFuneral: n/a

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;
}
}

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;[B]}[/B]

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
}

Edited 5 Years Ago by Adak: n/a

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;
}
}

Edited 5 Years Ago by TrueCoding: n/a

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.

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

Edited 5 Years Ago by TrueCoding: n/a

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();

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;
}
}

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?

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.

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,

Edited 5 Years Ago by Adak: n/a

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;
}

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");
}

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;
}

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

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

I dont know why can anyone tell me it keeps skipping the code after printing the array?

# 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 -- this is where program stops it should carry on down 
    }
     //**************************************//
    oddcount = 0;
    evencount = 0;

     for (i = 0; i < n; i++) // Code determines wheter 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;
}

Ok thanks for that...But to be honest its not helping me. Its just simply ending the program after printing the array...Isnt there anything I can do other than debugging?

uhh.... No. Debugging is the act of resolving those issues and no one's going to do it for you(unless you like spending money).

Umm Ok its just frustating that the code isnt working though it looks fine. Ill try debgging again but I dont understand it..your link is a bit confusing.
But thanks for all your help.

Try this code..

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

    printf("\n Enter Array Size (Between 1 to %d ): ",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("\n [%d] value is %3d ", i+1, array[i]); //Prints out the array -- this is where program stops it should carry on down
    }
     //**************************************//
    oddcount = 0;
    evencount = 0;

     for (i = 0; i < n; i++) // Code determines wheter 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 ++;
          }
     }
     printf("\n Odd Num: %d \n Even Num: %d", oddcount , evencount );

     return 0;
}

If you are still getting some problem then post your problem here..

# 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 -- this is where program stops it should carry on down 
    }
    oddcount = 0;
    evencount = 0;

    for (i = 0; i < n; i++) // Code determines wheter 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;
}

Your code isn't stopping. It just finishes - you never added the print statement for how many even and odd numbers you had in the array.

Add that, and it should be fine - if not, post back. Don't you know how to single step through your program yet?

That's a MUST to learn, and right away. You have some real work to do to get yourself up to speed on this. Get cracking, dude! ;)

Edit: With n, you aren't setting up the size of the array - it has MAX number of elements. You are setting up how many elements your data will have, in the array. You don't want to go working with data that isn't yours, so that's important to have in there.

Edited 5 Years Ago by Adak: n/a

Thankyou very much Vinitmittal that code works perfectly now! Thanks to everyoner who helped me with this

HOPE IT CAN HELP TO US :D

#include<stdio.h>
#include<conio.h>
int main()
{
int list[5];
int i;
clrscr();
for(i=0;i<=4;i++)
{
printf("Enter no. %d:",i+1);
scanf("%d",&list);
}
printf("The even nos.:");
for(i=0;i<=4;i++)
{
if(list%2==0)
{
printf("%d\t",list);
}
}
printf("\n The odd nos.:");
for(i=0;i<=4;i++)
{
if(list%2==1)
{
printf("%d\t",list);
}
}
getch();
return 0
}

This question has already been answered. Start a new discussion instead.