Simple Program...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MabZiCLe
    New Member
    • Nov 2006
    • 7

    Simple Program...

    Hey guys, can you help me with this? C programming major in Turbo c. LOL


    Well, this is my problem.

    Create a program that accepts 10 positive integers. The program will group and sort all even and odd numbers. Even numbers will be sorted in ascending while odd numbers will be sorted in descending order. If theres is no even numbers found the program will print "NO even numbers" yet it prints the odd numbers, and sort it vice versa.

    Use if statements or loops only.(turbo c)(no arrays or gotoxy() ]
    please do help me.....

    SAMPLE OUTPUT 1:
    Enter 10 integers: 1 2 3 4 5 6 7 8 9 10

    DISPLAYS:
    even numbers:
    +--------------------------+
    | 2 4 6 8 10 |
    +--------------------------+
    odd numbers:
    +---------------------------+
    | 1 3 5 7 9 |
    +---------------------------+
    sorted even numbers:
    +---------------------------+
    | 2 4 6 8 10 |
    +---------------------------+
    sorted odd numbers:
    +---------------------------+
    | 9 7 5 3 1 |
    +---------------------------+

    SAMPLE OUTPUT 2:

    ENTER TEN INTEGERS: 2 4 6 8 10 24 12 26 56 90

    DISPLAYS:

    NO ODD NUMBERS!

    EVEN NUMBERS: 2 4 6 8 10 24 12 26 56 90
    SORTED EVEN: 2 4 6 8 10 12 24 26 56 90


    Thanks MORE POwer!
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Hi,

    Do u have any code?
    If have please post it.

    Regards,
    M.Sivadhas.

    Comment

    • rashmisn
      New Member
      • Nov 2006
      • 2

      #3
      Code:
       //this programmay help you 
       
      #include<stdio.h>
      main()
      {
      int j=0,temp=0,ea[10],oa[10],i=0,e=0,o=0,n;
       
      for(i=0;i<10;i++)
      {
      printf("Enter number");
      scanf("%d",&n);
       
      if(n%2==0)
      ea[e++]=n;
      else
      oa[o++]=n;
      }
      if(e==0)
       
      printf("No Even numbers:\n");
      else
      {
      printf("Even numbers:\n");
      for(i=0;i<e-1;i++)
      {
      for(j=i+1;j<e;j++)
      {
      if(ea[i]>ea[j])
      {
      temp=ea[i];
      ea[i]=ea[j];
      ea[j]=temp;
      }
      }
      }
      for(i=0;i<e;i++)
      printf("%d\t",ea[i]);
      printf("\n");
      }
      if(o==0)
       
      printf("No odd numbers:\n");
      else
      {
      printf("Odd numbers:\n");
       
      for(i=0;i<o-1;i++)
      {
      for(j=i+1;j<o;j++)
      {
      if(oa[i]>oa[j])
      {
      temp=oa[i];
      oa[i]=oa[j];
      oa[j]=temp;
      }
      }
      }
      for(i=0;i<o;i++)
      printf("%d\t",oa[i]);
      }
      }

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        And what is the exact problem.....

        Comment

        • Manjiri
          New Member
          • Nov 2006
          • 40

          #5
          Originally posted by rashmisn
          //this programmay help you

          #include<stdio. h>
          main()
          {
          int j=0,temp=0,ea[10],oa[10],i=0,e=0,o=0,n;

          for(i=0;i<10;i+ +)
          {
          printf("Enter number");
          scanf("%d",&n);

          if(n%2==0)
          ea[e++]=n;
          else
          oa[o++]=n;
          }
          if(e==0)

          printf("No Even numbers:\n");
          else
          {
          printf("Even numbers:\n");
          for(i=0;i<e-1;i++)
          {
          for(j=i+1;j<e;j ++)
          {
          if(ea[i]>ea[j])
          {
          temp=ea[i];
          ea[i]=ea[j];
          ea[j]=temp;
          }
          }
          }
          for(i=0;i<e;i++ )
          printf("%d\t",e a[i]);
          printf("\n");
          }
          if(o==0)

          printf("No odd numbers:\n");
          else
          {
          printf("Odd numbers:\n");

          for(i=0;i<o-1;i++)
          {
          for(j=i+1;j<o;j ++)
          {
          if(oa[i]>oa[j])
          {
          temp=oa[i];
          oa[i]=oa[j];
          oa[j]=temp;
          }
          }
          }
          for(i=0;i<o;i++ )
          printf("%d\t",o a[i]);
          }
          }

          Hi Rashmi...

          your program gives the output...
          But Plz put the statement"Enter your numbers" outside the for loop...
          Otherwise for each and every number it will output "Enter you number"..
          And the person has also asked you to group the odd numbers and even numbers saparately then print them in order...

          Hope my suggestion is ok for you..
          Regards
          Manjiri

          Comment

          • rashmisn
            New Member
            • Nov 2006
            • 2

            #6
            hi manjiri
            thanks for your suggestion
            here it toring even and odd numbers in seperate arrays(ea and oa) and then i am sorting.

            Comment

            • Manjiri
              New Member
              • Nov 2006
              • 40

              #7
              Originally posted by rashmisn
              hi manjiri
              thanks for your suggestion
              here it toring even and odd numbers in seperate arrays(ea and oa) and then i am sorting.
              Hi Rashmi

              Ya ya you are right... but see the problem also tells you to print them separately before you sort them... You see the required output once again...
              you are not outputting the group of odd and even numbers(before sorting) where it is required to do that...

              Hope you got me now...

              Comment

              • Manjiri
                New Member
                • Nov 2006
                • 40

                #8
                Originally posted by rashmisn
                hi manjiri
                thanks for your suggestion
                here it toring even and odd numbers in seperate arrays(ea and oa) and then i am sorting.

                Hi Rashmi

                One more thing.. you are printing the odd numbers in ascending order... where problem asks you to print it in descending order...

                Regards
                Madhuri

                Comment

                • MabZiCLe
                  New Member
                  • Nov 2006
                  • 7

                  #9
                  w000.... i need while/if statements..not using arrays.. =((

                  Comment

                  • DeMan
                    Top Contributor
                    • Nov 2006
                    • 1799

                    #10
                    You could store them all in a string,....wait a minute that's an array too. That means you qwould have to use some other type of data structure (which to me seems a peculiar requirement, unless thew idea is for you to implement some data type). Have you considered this approach, and if so please post any progress

                    Comment

                    • MabZiCLe
                      New Member
                      • Nov 2006
                      • 7

                      #11
                      hmm..well, if no odd/even numbers? still, it will sort?

                      Comment

                      • DeMan
                        Top Contributor
                        • Nov 2006
                        • 1799

                        #12
                        Depending on how you implemented your data structure, it could sort irrelevant of what the actual valueas are (that is, whether they are odd, even or a random mixture of both).
                        Even with arrays you could use almost any type of sort, but a structure like a linked list (which is reasonably easy to implement) may be more eficient. If you are really keen you could try creating a simple binary tree - a structure which beasically stores, adds and deletes elements in a way that is always sorted (if you really wanted to do some work, you could implement a balanced binary tree, which is space efficient, while still holding all the properties of the binary tree.....)

                        Comment

                        • tavianator
                          New Member
                          • Dec 2006
                          • 38

                          #13
                          I think the best way to do that without using arrays is to have ten ints (i0, i1 ... i9) and instead of loops, just modify each directly. Since you only have to input ten numbers, this will work.

                          Comment

                          • DeMan
                            Top Contributor
                            • Nov 2006
                            • 1799

                            #14
                            sorting ten integers in ten variables can become REALLY messy (though not impossible), so I would think you need at least 11 variables (a temporary one is always nice), I'm not entirely sure how
                            Originally posted by tavianator
                            instead of loops, just modify each directly
                            would work for sorting.....

                            Of course, one option would be to sort the numbers dynamically as they're entered (we put them all in order irrespective of odd/even and then as needed iterate through all testing if they are odd <or even>). To do this :

                            entry 1 goes in i1
                            if entry2 > i1 it goes in i2, else i1 goes in i2 and entry 2 goes in i1 (watch out!!! this can get complex even with temp variables......

                            TBC

                            Comment

                            • reon
                              New Member
                              • Feb 2007
                              • 80

                              #15
                              You can simply use this method for entering just one number and know it is odd or even


                              #include<iostre am.h>
                              #include<conio. h>
                              void main()
                              {
                              clrscr();
                              int a;
                              cout<<"Enter a number";
                              cin>>a;
                              if(a%2==0)
                              {
                              cout<<"Even numbers are :"<<a;
                              }
                              else
                              {
                              cout<<"\nNo even number";
                              }


                              if(a%2!=0)
                              {
                              cout<<"odd numbers are :"<<a;
                              }
                              else
                              {
                              cout<<"\nNo odd numbers";
                              }
                              getch();
                              }

                              Comment

                              Working...