Optimizing C++ Case Statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clockworx05
    New Member
    • Sep 2007
    • 11

    Optimizing C++ Case Statement

    Hey guys i have this program that i need to write for class. here are the instructions:

    Write a function called foo that asks the user for their age. Pass the age value to a function called showAge that uses a select case structure to print out the following: If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager. If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature


    This is what i have, its works but i dont see why my teach wont let me use an if else..... please help me. Is this the right solution?

    Code:
    #include <iostream>
    using namespace std; 
    int foo ();
    
    int x;
    int showAge ();
    int main()
    {
    	
    	foo();
    	
    
    }
    int foo()
    {
    	cout << "Please Enter Your Age:" << endl;
    	cin >> x;
    	showAge();
    return x;
    }
    int showAge ()
    {
    switch ( x )
        {
    			case 1:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 2:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 3:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 4:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 5:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 6:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 7:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 8:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 9:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 10:
                cout << endl << "You are still a child." << endl;
                break;
    			 case 11:
                cout << endl << "You are still a child." << endl;
                break;
    			case 12:
                cout << endl << "You are still a child." << endl;
                break;
    			case 13:
                cout << endl << "You are a teenager." << endl;
                break;
    			case 14:
                cout << endl << "You are a teenager." << endl;
                break;
    			case 15:
                cout << endl << "You are a teenager." << endl;
                break;
    			case 16:
                cout << endl << "You are a teenager." << endl;
                break;
    			case 17:
                cout << endl << "You are a teenager." << endl;
                break;
    			case 18:
                cout << endl << "You are a teenager." << endl;
                break;
    			case 19:
                cout << endl << "You are a teenager." << endl;
                break;
    			case 20:
                cout << endl << "You are an adult" << endl;
                break;
    			case 21:
                cout << endl << "You are an adult" << endl;
                break;
    			case 22:
                cout << endl << "You are an adult" << endl;
                break;
    			case 23:
                cout << endl << "You are an adult" << endl;
                break;
    			case 24:
                cout << endl << "You are an adult" << endl;
                break;
    			case 25:
                cout << endl << "You are an adult" << endl;
                break;
    			case 26:
                cout << endl << "You are an adult" << endl;
                break;
    			case 27:
                cout << endl << "You are an adult" << endl;
                break;
    			case 28:
                cout << endl << "You are an adult" << endl;
                break;
    			case 29:
                cout << endl << "You are an adult" << endl;
                break;
    			case 30:
                cout << endl << "You are an adult" << endl;
                break;
    			case 31:
                cout << endl << "You are an adult" << endl;
                break;
    			case 32:
                cout << endl << "You are an adult" << endl;
                break;
    			case 33:
                cout << endl << "You are an adult" << endl;
                break;
    			case 34:
                cout << endl << "You are an adult" << endl;
                break;
    			case 35:
                cout << endl << "You are an adult" << endl;
                break;
    			case 36:
                cout << endl << "You are an adult" << endl;
                break;
    			case 37:
                cout << endl << "You are an adult" << endl;
                break;
    			case 38:
                cout << endl << "You are an adult" << endl;
                break;
    			case 39:
                cout << endl << "You are an adult" << endl;
                break;
    			case 40:
                cout << endl << "You are an adult" << endl;
                break;
           
            default:
                cout << endl << "ou are starting to become mature or too old" << endl;
        }
    
    
    
    return x;
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Wow, that is one hell of a case statement. I would recommend the ternary operator, though. It's a nifty workaround to if-elses.

    ::Edit:: You could also use an if statement to call a function. If you if statements were set up properly, one would not overlap with the other, and you could leave out the else statement. Then you're not using if-elses...
    Last edited by sicarie; Sep 25 '07, 07:31 PM. Reason: Another cheat

    Comment

    • clockworx05
      New Member
      • Sep 2007
      • 11

      #3
      I GUESS I WILL DO THIS:


      Code:
      #include <iostream>
      using namespace std; 
      int foo ();
      
      int x;
      int showAge ();
      int main()
      {
      	
      	foo();
      	
      
      }
      int foo()
      {
      	cout << "Please Enter Your Age:" << endl;
      	cin >> x;
      	showAge();
      return x;
      }
      int showAge ()
      {
      switch ( x )
          {
      			case 1:
      			case 2:
      			case 3:
      			case 4:
      			case 5:
      			case 6:
      			case 7:
      			case 8:
      			case 9:
      			case 10:
      			case 11:
      			case 12:
      			   cout << "You are still a child" << endl;	
      			    break;
      			case 13:
      			case 14:
      			case 15:
      			case 16:
      			case 17:
      			case 18:
      			case 19:
      				cout << "You are a teenager." << endl;
      				break;
      			case 20:                   
      			case 21:           
      			case 22:
                  case 23:           
      			case 24:        
      			case 25:
      			case 26:           
      			case 27:        
      			case 28:         
      			case 29:          
      			case 30:          
      			case 31:          
      			case 32:         
      			case 33:          
      			case 34:       
      			case 35:     
      			case 36:       
      			case 37: 
      			case 38: 
      			case 39:
      			case 40:
      				cout << "You are an adult" << endl;
      			  break;
      		
              default:
                  cout << "ou are starting to become mature or too old" << endl;
          }
      
      
      
      return x;
      }

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by clockworx05
        I GUESS I WILL DO THIS:
        That works too. Did you look at the ternary operator?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by clockworx05
          I GUESS I WILL DO THIS:
          Much better and tell your teacher that he sucks big times.

          kind regards,

          Jos ( <--- 50 years old ;-)

          Comment

          • clockworx05
            New Member
            • Sep 2007
            • 11

            #6
            ok i guess im not aloud to do int x; in global, how would i pass X throughout?

            Comment

            • kreagan
              New Member
              • Aug 2007
              • 153

              #7
              Originally posted by clockworx05
              This is what i have, its works but i dont see why my teach wont let me use an if else..... please help me. Is this the right solution?
              These are just guesses:

              1.) From what I heard, switch statements run faster than if else statement (just barely).
              2.) To force you to try something new for a change.
              3.) If she told you to use a switch statement, then she's either silly, stupid, or enjoys seeing her students suffer!

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Have a look at passing variables to functions.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by kreagan
                  These are just guesses:

                  1.) From what I heard, switch statements run faster than if else statement (just barely).
                  That is not true; for tests against an integer, switches are quite a bit faster than
                  a cascade of if-statements. The latter always run in O(n) while switch statemtents
                  can run in O(1) for 'dense' switches and O(log(n)) for sparse switches. Both of
                  which are way better than O(n).

                  kind regards,

                  Jos

                  Comment

                  • clockworx05
                    New Member
                    • Sep 2007
                    • 11

                    #10
                    i read that tutorial, i still dont understand.

                    Comment

                    • sicarie
                      Recognized Expert Specialist
                      • Nov 2006
                      • 4677

                      #11
                      Originally posted by clockworx05
                      i read that tutorial, i still dont understand.
                      Wow, that was really fast for you to read it and understand it. Did you run the example? Did you change the values when you ran the example? Did you change the datatypes? What else did you try?

                      Comment

                      • clockworx05
                        New Member
                        • Sep 2007
                        • 11

                        #12
                        i read that tutorial earlier today, i just get this one error that says:


                        "error C2660: 'showAge' : function does not take 0 arguments"
                        This is my code now:
                        [Code=cpp]

                        /*
                        Write a function called foo that asks the user for their age.
                        Pass the age value to a function called showAge that uses a select case structure to print out the following:
                        If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager.
                        If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
                        */

                        #include <iostream>
                        using namespace std;
                        int foo ();
                        int showAge (int &x);
                        int main()
                        {

                        foo();

                        return 0;
                        }

                        int foo()
                        {
                        int x;
                        cout << "Please Enter Your Age:" << endl;
                        cin >> x;
                        showAge();

                        }

                        int showAge (int x)
                        {
                        switch ( x )
                        {

                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                        case 10:
                        case 11:
                        case 12:
                        cout << "You are still a child" << endl;
                        break;
                        case 13:
                        case 14:
                        case 15:
                        case 16:
                        case 17:
                        case 18:
                        case 19:
                        cout << "You are a teenager." << endl;
                        break;
                        case 20:
                        case 21:
                        case 22:
                        case 23:
                        case 24:
                        case 25:
                        case 26:
                        case 27:
                        case 28:
                        case 29:
                        case 30:
                        case 31:
                        case 32:
                        case 33:
                        case 34:
                        case 35:
                        case 36:
                        case 37:
                        case 38:
                        case 39:
                        case 40:
                        cout << "You are an adult" << endl;
                        break;

                        default:
                        cout << "ou are starting to become mature or too old" << endl;
                        }

                        }[/code]
                        Last edited by sicarie; Sep 25 '07, 08:18 PM. Reason: Sorry, I hit 'edit' instead of reply - i'll fix it in a sec

                        Comment

                        • clockworx05
                          New Member
                          • Sep 2007
                          • 11

                          #13
                          its inconsistant? or im not passing is correctly?

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            Originally posted by clockworx05
                            i read that tutorial earlier today, i just get this one error that says:


                            "error C2660: 'showAge' : function does not take 0 arguments"
                            Okay, let's take a look at all the places you use showAge()
                            [code=cpp]
                            int showAge (int &x);
                            int main()
                            {
                            //...
                            }

                            int foo()
                            {
                            //...
                            cin >> x;
                            showAge();
                            }

                            int showAge (int x)
                            {
                            //...
                            }
                            [/code]

                            Do you see anything odd there?

                            Comment

                            • Savage
                              Recognized Expert Top Contributor
                              • Feb 2007
                              • 1759

                              #15
                              He can use switch statements,but after:

                              [CODE="cpp"]int k;
                              if(age>0&&age<= 12) k=1;
                              if(age>=13&&age <=19) k=2;
                              .
                              .
                              .

                              switch(k)
                              {

                              //blah,blah,blah. .

                              .
                              .
                              .
                              [/CODE]
                              Savage

                              Comment

                              Working...