Optimizing C++ Case Statement | Newbie | | Join Date: Sep 2007
Posts: 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? |  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Optimizing C++ Case Statement
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...
| | Newbie | | Join Date: Sep 2007
Posts: 11
| | | re: Optimizing C++ Case Statement
I GUESS I WILL DO THIS: -
#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;
-
}
-
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Optimizing C++ Case Statement Quote:
Originally Posted by clockworx05 I GUESS I WILL DO THIS:
That works too. Did you look at the ternary operator?
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Optimizing C++ Case Statement Quote:
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 ;-)
| | Newbie | | Join Date: Sep 2007
Posts: 11
| | | re: Optimizing C++ Case Statement
ok i guess im not aloud to do int x; in global, how would i pass X throughout?
|  | Familiar Sight | | Join Date: Aug 2007 Location: New Hampshire
Posts: 153
| | | re: Optimizing C++ Case Statement Quote:
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!
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Optimizing C++ Case Statement |  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Optimizing C++ Case Statement Quote:
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
| | Newbie | | Join Date: Sep 2007
Posts: 11
| | | re: Optimizing C++ Case Statement
i read that tutorial, i still dont understand.
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Optimizing C++ Case Statement Quote:
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?
| | Newbie | | Join Date: Sep 2007
Posts: 11
| | | re: Optimizing C++ Case Statement
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: -
-
/*
-
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;
-
}
-
-
}
| | Newbie | | Join Date: Sep 2007
Posts: 11
| | | re: Optimizing C++ Case Statement
its inconsistant? or im not passing is correctly?
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Optimizing C++ Case Statement Quote:
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() -
int showAge (int &x);
-
int main()
-
{
-
//...
-
}
-
-
int foo()
-
{
-
//...
-
cin >> x;
-
showAge();
-
}
-
-
int showAge (int x)
-
{
-
//...
-
}
-
Do you see anything odd there?
|  | Expert | | Join Date: Feb 2007
Posts: 1,737
| | | re: Optimizing C++ Case Statement
He can use switch statements,but after: - int k;
-
if(age>0&&age<=12) k=1;
-
if(age>=13&&age<=19) k=2;
-
.
-
.
-
.
-
-
switch(k)
-
{
-
-
//blah,blah,blah..
-
-
.
-
.
-
.
-
Savage
| | Newbie | | Join Date: Sep 2007
Posts: 11
| | | re: Optimizing C++ Case Statement
ok here is my code now: Same error as above: -
/*
-
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()
-
{
-
-
foo();
-
-
return 0;
-
}
-
int foo(int x)
-
{
-
-
cout << "Please Enter Your Age:" << endl;
-
cin >> x;
-
showAge();
-
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;
-
}
-
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Optimizing C++ Case Statement Quote:
Originally Posted by Savage He can use switch statements,but after: - int k;
-
if(age>0&&age<=12) k=1;
-
if(age>=13&&age<=19) k=2;
-
.
-
.
-
.
-
-
switch(k)
-
{
-
-
//blah,blah,blah..
-
-
.
-
.
-
.
-
Savage That'd be silly because why postpone what you can deal with right after the
if-clause itself?
kind regards,
Jos
|  | Expert | | Join Date: Feb 2007
Posts: 1,737
| | | re: Optimizing C++ Case Statement Quote:
Originally Posted by clockworx05 ok here is my code now: Same error as above: -
/*
-
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()
-
{
-
-
foo();
-
-
return 0;
-
}
-
int foo(int x)
-
{
-
-
cout << "Please Enter Your Age:" << endl;
-
cin >> x;
-
showAge();
-
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;
-
}
-
Anything odd here: -
int foo(int x)
-
{
-
-
cout << "Please Enter Your Age:" << endl;
-
cin >> x;
-
vvvvvvvvvvvvvvv
-
--------------->\\showAge();//<-------------------
-
^^^^^^^^^^^^^^^
-
return x;
-
}
???
Savage
|  | Expert | | Join Date: Feb 2007
Posts: 1,737
| | | re: Optimizing C++ Case Statement Quote:
Originally Posted by JosAH 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
| | Newbie | | Join Date: Sep 2007
Posts: 11
| | | re: Optimizing C++ Case Statement
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;
}
}
| | Newbie | | Join Date: Sep 2007
Posts: 11
| | | re: Optimizing C++ Case Statement
the error is:
uninitialized local variable 'x' used
|  | Moderator | | Join Date: Nov 2006 Location: USA
Posts: 3,929
| | | re: Optimizing C++ Case Statement Quote:
Originally Posted by clockworx05 the error is:
uninitialized local variable 'x' used Move your cin to main.
| | Newbie | | Join Date: Sep 2007
Posts: 11
| | | re: Optimizing C++ Case Statement
Ok i fixed it :)
Thanks everyone for the help; -
/*
-
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 = 0;
-
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<40) 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 << "You are starting to become mature or too old" << endl;
-
-
}
-
-
-
return x;
-
}
-
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Optimizing C++ Case Statement Quote:
Originally Posted by Savage 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
|  | Expert | | Join Date: Feb 2007
Posts: 1,737
| | | re: Optimizing C++ Case Statement Quote:
Originally Posted by JosAH 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|