We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,445 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion
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…

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;
}
TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
Skill Endorsements: 0

You should try Code::Blocks, Dev-C++ has become very out-of-date, but the debugger on both works the same.

http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks

You can setup break-points to step through, and a watch on those variables.

MosaicFuneral
Posting Virtuoso
1,699 posts since Nov 2008
Reputation Points: 888
Solved Threads: 118
Skill Endorsements: 7

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?

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

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).

MosaicFuneral
Posting Virtuoso
1,699 posts since Nov 2008
Reputation Points: 888
Solved Threads: 118
Skill Endorsements: 7

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.

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

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..

vinitmittal2008
Junior Poster
135 posts since Oct 2010
Reputation Points: 50
Solved Threads: 20
Skill Endorsements: 0
# 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.

Adak
Posting Virtuoso
1,641 posts since Jun 2008
Reputation Points: 456
Solved Threads: 196
Skill Endorsements: 7

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

TrueCoding
Junior Poster in Training
65 posts since Apr 2010
Reputation Points: 17
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 2 Years Ago by MosaicFuneral, Adak and vinitmittal2008

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
}

Rushirl Quiño
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1078 seconds using 2.71MB