User Tag List

Results 1 to 2 of 2

Thread: I'm learning C++... Is this the smallest?

  1. #1
    I'm learning C++... Is this the smallest?

    User Info Menu

    I'm learning C++... Is this the smallest?

    Well I'm learning C++ and according to the site I'm using this code can be shortened to half the size.

    Code:
    // declaring functions prototypes
    #include <iostream>
    using namespace std;
    
    void odd (int a);
    void even (int a);
    
    int main ()
    {
      int i;
      do {
        cout << "Type a number (0 to exit): ";
        cin >> i;
        odd (i);
      } while (i!=0);
      return 0;
    }
    
    void odd (int a)
    {
      if ((a%2)!=0) cout << "Number is odd.\n";
      else even (a);
    }
    
    void even (int a)
    {
      if ((a%2)==0) cout << "Number is even.\n";
      else odd (a);
    }
    This example is indeed not an example of efficiency. I am sure that at
    this point you can already make a program with the same result, but
    using only half of the code lines that have been used in this example.
    Anyway this example illustrates how prototyping works. Moreover, in this
    concrete example the prototyping of at least one of the two functions
    is necessary in order to compile the code without errors.
    This is what I am came up with:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int i;
      do {
        cout << "Type a number (0 to exit): ";
        cin >> i;
    
        if ((i%2)!=0) cout << "Number is odd.\n";
      else cout << "Number is even.\n";   
    }
      while (i!=0);
      return 0;
     }
    Is this the shortest it can be? If not how could I optimize it further?

    Also, even though when I enter 0 and it closes how come this works? != is not equal to. :/

    Code:
    while (i!=0);
      return 0;
    9 sections and that's the only thing I don't understand.
    Last edited by S2h6699; 04-12-2010 at 05:13 PM. Reason: Typo.
    Quote Originally Posted by Epic of HITB
    get... 1) a mini fridge | 2) A toilet installed in your room | 3) a closet full of non-perishable foods | 4) Lyscol ( who needs showers anyway?) | 5) tons of books articles lectures zines etc etc

    Dont leave your room...EVER... AND I MEAN EVER
    Damned six line limit! *NERD RAGE*

  2. #2
    The Destroyer

    User Info Menu

    Re: I'm learning C++... Is this the smallest?

    You can make it smaller. When I make programs I don't add the line do{ and the space above the int main is not required. It's just to make it look good. And I don't really understand the i!=0 either. I usually use namespace Std and the system pause.

    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
      int i;
        cout << "Type a number (0 to exit): \n";
        cin >> i;
        if ((i%2)!=0) cout << "Number is odd.\n";
      else cout << "Number is even.\n";   
      while (i!=0);
      return 0;
     }

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •