By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,866 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,866 IT Pros & Developers. It's quick & easy.

Optimizing C++ Case Statement

P: 11
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?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std; 
  3. int foo ();
  4.  
  5. int x;
  6. int showAge ();
  7. int main()
  8. {
  9.  
  10.     foo();
  11.  
  12.  
  13. }
  14. int foo()
  15. {
  16.     cout << "Please Enter Your Age:" << endl;
  17.     cin >> x;
  18.     showAge();
  19. return x;
  20. }
  21. int showAge ()
  22. {
  23. switch ( x )
  24.     {
  25.             case 1:
  26.             cout << endl << "You are still a child." << endl;
  27.             break;
  28.              case 2:
  29.             cout << endl << "You are still a child." << endl;
  30.             break;
  31.              case 3:
  32.             cout << endl << "You are still a child." << endl;
  33.             break;
  34.              case 4:
  35.             cout << endl << "You are still a child." << endl;
  36.             break;
  37.              case 5:
  38.             cout << endl << "You are still a child." << endl;
  39.             break;
  40.              case 6:
  41.             cout << endl << "You are still a child." << endl;
  42.             break;
  43.              case 7:
  44.             cout << endl << "You are still a child." << endl;
  45.             break;
  46.              case 8:
  47.             cout << endl << "You are still a child." << endl;
  48.             break;
  49.              case 9:
  50.             cout << endl << "You are still a child." << endl;
  51.             break;
  52.              case 10:
  53.             cout << endl << "You are still a child." << endl;
  54.             break;
  55.              case 11:
  56.             cout << endl << "You are still a child." << endl;
  57.             break;
  58.             case 12:
  59.             cout << endl << "You are still a child." << endl;
  60.             break;
  61.             case 13:
  62.             cout << endl << "You are a teenager." << endl;
  63.             break;
  64.             case 14:
  65.             cout << endl << "You are a teenager." << endl;
  66.             break;
  67.             case 15:
  68.             cout << endl << "You are a teenager." << endl;
  69.             break;
  70.             case 16:
  71.             cout << endl << "You are a teenager." << endl;
  72.             break;
  73.             case 17:
  74.             cout << endl << "You are a teenager." << endl;
  75.             break;
  76.             case 18:
  77.             cout << endl << "You are a teenager." << endl;
  78.             break;
  79.             case 19:
  80.             cout << endl << "You are a teenager." << endl;
  81.             break;
  82.             case 20:
  83.             cout << endl << "You are an adult" << endl;
  84.             break;
  85.             case 21:
  86.             cout << endl << "You are an adult" << endl;
  87.             break;
  88.             case 22:
  89.             cout << endl << "You are an adult" << endl;
  90.             break;
  91.             case 23:
  92.             cout << endl << "You are an adult" << endl;
  93.             break;
  94.             case 24:
  95.             cout << endl << "You are an adult" << endl;
  96.             break;
  97.             case 25:
  98.             cout << endl << "You are an adult" << endl;
  99.             break;
  100.             case 26:
  101.             cout << endl << "You are an adult" << endl;
  102.             break;
  103.             case 27:
  104.             cout << endl << "You are an adult" << endl;
  105.             break;
  106.             case 28:
  107.             cout << endl << "You are an adult" << endl;
  108.             break;
  109.             case 29:
  110.             cout << endl << "You are an adult" << endl;
  111.             break;
  112.             case 30:
  113.             cout << endl << "You are an adult" << endl;
  114.             break;
  115.             case 31:
  116.             cout << endl << "You are an adult" << endl;
  117.             break;
  118.             case 32:
  119.             cout << endl << "You are an adult" << endl;
  120.             break;
  121.             case 33:
  122.             cout << endl << "You are an adult" << endl;
  123.             break;
  124.             case 34:
  125.             cout << endl << "You are an adult" << endl;
  126.             break;
  127.             case 35:
  128.             cout << endl << "You are an adult" << endl;
  129.             break;
  130.             case 36:
  131.             cout << endl << "You are an adult" << endl;
  132.             break;
  133.             case 37:
  134.             cout << endl << "You are an adult" << endl;
  135.             break;
  136.             case 38:
  137.             cout << endl << "You are an adult" << endl;
  138.             break;
  139.             case 39:
  140.             cout << endl << "You are an adult" << endl;
  141.             break;
  142.             case 40:
  143.             cout << endl << "You are an adult" << endl;
  144.             break;
  145.  
  146.         default:
  147.             cout << endl << "ou are starting to become mature or too old" << endl;
  148.     }
  149.  
  150.  
  151.  
  152. return x;
  153. }
  154.  
Sep 25 '07 #1
Share this Question
Share on Google+
24 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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...
Sep 25 '07 #2

P: 11
I GUESS I WILL DO THIS:


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std; 
  3. int foo ();
  4.  
  5. int x;
  6. int showAge ();
  7. int main()
  8. {
  9.  
  10.     foo();
  11.  
  12.  
  13. }
  14. int foo()
  15. {
  16.     cout << "Please Enter Your Age:" << endl;
  17.     cin >> x;
  18.     showAge();
  19. return x;
  20. }
  21. int showAge ()
  22. {
  23. switch ( x )
  24.     {
  25.             case 1:
  26.             case 2:
  27.             case 3:
  28.             case 4:
  29.             case 5:
  30.             case 6:
  31.             case 7:
  32.             case 8:
  33.             case 9:
  34.             case 10:
  35.             case 11:
  36.             case 12:
  37.                cout << "You are still a child" << endl;    
  38.                 break;
  39.             case 13:
  40.             case 14:
  41.             case 15:
  42.             case 16:
  43.             case 17:
  44.             case 18:
  45.             case 19:
  46.                 cout << "You are a teenager." << endl;
  47.                 break;
  48.             case 20:                   
  49.             case 21:           
  50.             case 22:
  51.             case 23:           
  52.             case 24:        
  53.             case 25:
  54.             case 26:           
  55.             case 27:        
  56.             case 28:         
  57.             case 29:          
  58.             case 30:          
  59.             case 31:          
  60.             case 32:         
  61.             case 33:          
  62.             case 34:       
  63.             case 35:     
  64.             case 36:       
  65.             case 37: 
  66.             case 38: 
  67.             case 39:
  68.             case 40:
  69.                 cout << "You are an adult" << endl;
  70.               break;
  71.  
  72.         default:
  73.             cout << "ou are starting to become mature or too old" << endl;
  74.     }
  75.  
  76.  
  77.  
  78. return x;
  79. }
  80.  
Sep 25 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
I GUESS I WILL DO THIS:
That works too. Did you look at the ternary operator?
Sep 25 '07 #4

Expert 10K+
P: 11,448
I GUESS I WILL DO THIS:
Much better and tell your teacher that he sucks big times.

kind regards,

Jos ( <--- 50 years old ;-)
Sep 25 '07 #5

P: 11
ok i guess im not aloud to do int x; in global, how would i pass X throughout?
Sep 25 '07 #6

kreagan
100+
P: 153
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!
Sep 25 '07 #7

sicarie
Expert Mod 2.5K+
P: 4,677
Have a look at passing variables to functions.
Sep 25 '07 #8

Expert 10K+
P: 11,448
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
Sep 25 '07 #9

P: 11
i read that tutorial, i still dont understand.
Sep 25 '07 #10

sicarie
Expert Mod 2.5K+
P: 4,677
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?
Sep 25 '07 #11

P: 11
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2.        /*
  3.        Write a function called foo that asks the user for their age.
  4.        Pass the age value to a function called showAge that uses a select case structure to print out the following:
  5.        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.
  6.       If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
  7.       */
  8.  
  9.       #include <iostream>
  10.       using namespace std;
  11.       int foo ();
  12.       int showAge (int &x);
  13.       int main()
  14.       {
  15.  
  16.           foo();
  17.  
  18.       return 0;
  19.       }
  20.  
  21.       int foo()
  22.       {
  23.           int x;
  24.           cout << "Please Enter Your Age:" << endl;
  25.           cin >> x;
  26.           showAge();
  27.  
  28.       }
  29.  
  30.       int showAge (int x)
  31.       {
  32.       switch ( x )
  33.           {
  34.  
  35.                   case 1:
  36.                   case 2:
  37.                   case 3:
  38.                   case 4:
  39.                   case 5:
  40.                   case 6:
  41.                   case 7:
  42.                   case 8:
  43.                   case 9:
  44.                   case 10:
  45.                   case 11:
  46.                   case 12:
  47.                      cout << "You are still a child" << endl; 
  48.                       break;
  49.                   case 13:
  50.                   case 14:
  51.                   case 15:
  52.                   case 16:
  53.                   case 17:
  54.                   case 18:
  55.                   case 19:
  56.                       cout << "You are a teenager." << endl;
  57.                       break;
  58.                   case 20:                   
  59.                   case 21:           
  60.                   case 22:
  61.                   case 23:           
  62.                   case 24:       
  63.                   case 25:
  64.                   case 26:           
  65.                   case 27:       
  66.                   case 28:         
  67.                   case 29:         
  68.                   case 30:         
  69.                   case 31:         
  70.                   case 32:         
  71.                   case 33:         
  72.                   case 34:       
  73.                   case 35:     
  74.                   case 36:       
  75.                   case 37:
  76.                   case 38:
  77.                   case 39:
  78.                   case 40:
  79.                       cout << "You are an adult" << endl;
  80.                     break;
  81.  
  82.               default:
  83.                   cout << "ou are starting to become mature or too old" << endl;
  84.           }
  85.  
  86.       }
Sep 25 '07 #12

P: 11
its inconsistant? or im not passing is correctly?
Sep 25 '07 #13

sicarie
Expert Mod 2.5K+
P: 4,677
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()
Expand|Select|Wrap|Line Numbers
  1.       int showAge (int &x);
  2.       int main()
  3.       {
  4.      //...
  5.       }
  6.  
  7.       int foo()
  8.       {
  9.           //...
  10.           cin >> x;
  11.           showAge();
  12.       }
  13.  
  14.       int showAge (int x)
  15.       {
  16.       //...
  17.       }
  18.  
Do you see anything odd there?
Sep 25 '07 #14

Savage
Expert 100+
P: 1,764
He can use switch statements,but after:

Expand|Select|Wrap|Line Numbers
  1. int k;
  2. if(age>0&&age<=12) k=1;
  3. if(age>=13&&age<=19) k=2;
  4. .
  5. .
  6. .
  7.  
  8. switch(k)
  9.  
  10. //blah,blah,blah..
  11.  
  12. .
  13. .
  14. .
  15.  
Savage
Sep 25 '07 #15

P: 11
ok here is my code now: Same error as above:

Expand|Select|Wrap|Line Numbers
  1. /*
  2. Write a function called foo that asks the user for their age. 
  3. Pass the age value to a function called showAge that uses a select case structure to print out the following: 
  4. 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. 
  5. If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
  6. */
  7.  
  8. #include <iostream>
  9. using namespace std; 
  10. int foo (int x);
  11.  
  12.  
  13. int showAge (int &x);
  14. int main()
  15. {
  16.  
  17.     foo();
  18.  
  19. return 0;
  20. }
  21. int foo(int x)
  22. {
  23.  
  24.     cout << "Please Enter Your Age:" << endl;
  25.     cin >> x;
  26.     showAge();
  27. return x;
  28. }
  29. int showAge (int  &x)
  30. {
  31. if(x>0&&x<12) x=1;
  32. if(x>=13&&x<19) x=2;
  33. if(x>20) x=3;
  34. switch ( x )
  35.     {
  36.             case 1:
  37.                   cout << "You are still a child" << endl;    
  38.                 break;
  39.             case 2:
  40.                     cout << "You are a teenager." << endl;
  41.                 break;
  42.             case 3:
  43.                 cout << "You are an adult" << endl;
  44.               break;
  45.  
  46.  
  47.         default:
  48.             cout << "ou are starting to become mature or too old" << endl;
  49.  
  50.             return x;
  51.     }
  52.  
Sep 25 '07 #16

Expert 10K+
P: 11,448
He can use switch statements,but after:

Expand|Select|Wrap|Line Numbers
  1. int k;
  2. if(age>0&&age<=12) k=1;
  3. if(age>=13&&age<=19) k=2;
  4. .
  5. .
  6. .
  7.  
  8. switch(k)
  9.  
  10. //blah,blah,blah..
  11.  
  12. .
  13. .
  14. .
  15.  
Savage
That'd be silly because why postpone what you can deal with right after the
if-clause itself?

kind regards,

Jos
Sep 25 '07 #17

Savage
Expert 100+
P: 1,764
ok here is my code now: Same error as above:

Expand|Select|Wrap|Line Numbers
  1. /*
  2. Write a function called foo that asks the user for their age. 
  3. Pass the age value to a function called showAge that uses a select case structure to print out the following: 
  4. 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. 
  5. If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
  6. */
  7.  
  8. #include <iostream>
  9. using namespace std; 
  10. int foo (int x);
  11.  
  12.  
  13. int showAge (int &x);
  14. int main()
  15. {
  16.  
  17.     foo();
  18.  
  19. return 0;
  20. }
  21. int foo(int x)
  22. {
  23.  
  24.     cout << "Please Enter Your Age:" << endl;
  25.     cin >> x;
  26.     showAge();
  27. return x;
  28. }
  29. int showAge (int  &x)
  30. {
  31. if(x>0&&x<12) x=1;
  32. if(x>=13&&x<19) x=2;
  33. if(x>20) x=3;
  34. switch ( x )
  35.     {
  36.             case 1:
  37.                   cout << "You are still a child" << endl;    
  38.                 break;
  39.             case 2:
  40.                     cout << "You are a teenager." << endl;
  41.                 break;
  42.             case 3:
  43.                 cout << "You are an adult" << endl;
  44.               break;
  45.  
  46.  
  47.         default:
  48.             cout << "ou are starting to become mature or too old" << endl;
  49.  
  50.             return x;
  51.     }
  52.  

Anything odd here:

Expand|Select|Wrap|Line Numbers
  1. int foo(int x)
  2. {
  3.  
  4.     cout << "Please Enter Your Age:" << endl;
  5.     cin >> x;
  6.                      vvvvvvvvvvvvvvv
  7.     --------------->\\showAge();//<-------------------
  8.                      ^^^^^^^^^^^^^^^
  9.     return x;
  10. }
???

Savage
Sep 25 '07 #18

Savage
Expert 100+
P: 1,764
That'd be silly because why postpone what you can deal with right after the
if-clause itself?

kind regards,

Jos
Writing around 40 case statements is boring,silly and everything bad you can think of.

Savage
Sep 25 '07 #19

P: 11
now when i do that to foo it says:

variable "x" is being used without defined:

/*
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 x);


int showAge (int &x);
int main()
{
int x;
foo(x);

return 0;
}
int foo(int x)
{

cout << "Please Enter Your Age:" << endl;
cin >> x;
showAge(x);
return x;
}
int showAge (int &x)
{
if(x>0&&x<12) x=1;
if(x>=13&&x<19) x=2;
if(x>20) x=3;
switch ( x )
{
case 1:
cout << "You are still a child" << endl;
break;
case 2:
cout << "You are a teenager." << endl;
break;
case 3:
cout << "You are an adult" << endl;
break;


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

return x;
}


}
Sep 25 '07 #20

P: 11
the error is:

uninitialized local variable 'x' used
Sep 25 '07 #21

sicarie
Expert Mod 2.5K+
P: 4,677
the error is:

uninitialized local variable 'x' used
Move your cin to main.
Sep 25 '07 #22

P: 11
Ok i fixed it :)

Thanks everyone for the help;


Expand|Select|Wrap|Line Numbers
  1. /*
  2. Write a function called foo that asks the user for their age. 
  3. Pass the age value to a function called showAge that uses a select case structure to print out the following: 
  4. 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. 
  5. If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
  6. */
  7.  
  8. #include <iostream>
  9. using namespace std; 
  10. int foo (int x);
  11. int showAge (int &x);
  12. int main()
  13. {
  14. int x = 0;
  15.     foo(x);
  16.  
  17. return 0;
  18. }
  19. int foo(int x)
  20. {
  21.  
  22.     cout << "Please Enter Your Age:" << endl;
  23.     cin >> x;
  24.     showAge(x);
  25. return x;
  26. }
  27. int showAge (int  &x)
  28. {
  29. if(x>0&&x<12) x=1;
  30. if(x>=13&&x<19) x=2;
  31. if(x>20&&x<40) x=3;
  32. switch ( x )
  33.     {
  34.             case 1:
  35.                   cout << "You are still a child" << endl;    
  36.                 break;
  37.             case 2:
  38.                     cout << "You are a teenager." << endl;
  39.                 break;
  40.             case 3:
  41.                 cout << "You are an adult" << endl;
  42.               break;
  43.  
  44.  
  45.         default:
  46.             cout << "You are starting to become mature or too old" << endl;
  47.  
  48.     }
  49.  
  50.  
  51.             return x;
  52. }
  53.  
Sep 25 '07 #23

Expert 10K+
P: 11,448
Writing around 40 case statements is boring,silly and everything bad you can think of.

Savage
You didn't read my reply (in the context of yours) carefully enough; btw, the
amount of typing is nowhere proportional to the (in)efficiency of an algorithm
big-Oh wise speaking.

kind regards,

Jos
Sep 25 '07 #24

Savage
Expert 100+
P: 1,764
You didn't read my reply (in the context of yours) carefully enough; btw, the
amount of typing is nowhere proportional to the (in)efficiency of an algorithm
big-Oh wise speaking.

kind regards,

Jos
That's just what they want us to believe. :P

Just for fun using high accuracy counter(QueryPerformanceFreuqency and QueryPerformanceCounter),I discovered that my solution is faster for 142 ticks.(Avg for my sol:2483,Avg. for pure case sol.:2625 dif=-142 ticks)


:P

regards,

Savage
Sep 26 '07 #25

Post your reply

Sign in to post your reply or Sign up for a free account.