Switch Statements

Hi! I am still a student and I am having problems making a solution to the problem that was given by our professor. Here it is:

"Make a program by using SWITCH STATEMENT, to determine if an integer is NEGATIVE or POSITIVE."

In addition to that, we are not allowed to use any type of If-Else Statement.

How could I do this when an integer could go up so large and the Switch Statement doesn't accept ranges. And another thing, the compiler that we are using doesn't have an extension that could accept ranges for Switch Statements and I believe we are also not allowed to add an extension, if there is any.

I hope you could help.
Have you thought about performing some (math) operation on the number that will yield a small set of things you can test in a swith statement?
Hi,

I think following code might sole your probleam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
         ..........................
         int  n, value;
         cin>> n;
         n >o ?  value=1 : value=0;

         switch  ( value)
                    
                      {

                      case 1:
                                cout<<" The given value is positieve";
                                break;
                     case 2:
                               cout<<"The given value is negatieve";
                               break;
                    default:
                               cout<<" Value is out of range enter correct value";
                               break;
                       }
           ...........                 

Be clear with our c++ concepts .

http://www.wiziq.com/tutorial/763-C-Concepts


Thanks
Kolla Sanjeeva Rao
Except for the fact that you wrote n >o instead of >0. Another fact is that default will never get executed for integer values of n. Since n cast statically to an integer, the worst case scenario would be the throwing of an exception. Remove the default: section. Next it's "positive" and "negative". Also, one can re-write positive as unsigned and negative as signed. Therefor, 0 should be within case 1. The next and final thing is that value will be either 1 or 0. Not 1 or 2.
Last edited on
hi,

yes i agree this o and 0 is atyping mistake and
instead of case 1,2 it should be 0,1


Thanks
Kolla Sanjeeva RAo
Just a last note, using the conditional operator for this is not really necessary, since the tested value is a boolean, and the inputted value for the switch is a boolean as well. Lastly I'd like to note that in your posted code, value is an int, whereas a bool is more to-the-point and accurate.The following solution would be the most obvious:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    switch( n >= 0 )
    {
         case 0: cout << "Integer is below 0 and therefor negative." << endl; break;
         case 1: cout << "Integer is equal to or above 0 and therefor positive." << endl; break;
    }
}
Last edited on
Topic archived. No new replies allowed.