SWITCH statement to evaluate functions with domains

This is for a class assignment that I have been unable to figure out. I know it can be done using if and else statements but my instructor wants it figured out using specifically the SWITCH statement.

*Write a program that uses a switch statement to evaluate the following function:

x2 ; x <= 3
cos(x) ; 3 < x <= 6
ln(x) ; 6 < x < 20
sqrt(x) ; x >= 20


*This is all I have so far*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{

double x;
cin >> x;


switch ( x <= 3 )
{
case 1:
    cout << x * x << endl; //when x is less than or equal 3 it squares x
    break;

default:
    cout << sqrt(x) << endl;  //when x is 20 it square roots the function
    break;
}
system ("pause")
return 0;
}









Last edited on
Do you compile this? The compiler output could be really instructive.

Do you know what the outline of a basic C++ program looks like?

What do you know about getting input from the user?
This is similar to what I compile, i do use the cin >> x; as well and do have a basic understanding of the math functions and C++ layout, I am also assuming that what my instructor wants is something involving recursion as well. I was also thinking of trying to call the functions using void and call them in the main() body. Any feedback is appreciated, thanks.
*I have also tried to do them as individual cases, the functions still did not follow through with their respective case numbers*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

#include <iostream>
#include <cmath>

using namespace std;
 
 int main()

 {
/* I had to leave x as type double otherwise math functions will not allow program to compile*/
	 
         double x; 
         cin >> x;

	 switch ( static_cast<int>(x)) //cast x as type int since conditions are only tested against integer type
	 {
	 case 1:
		 cout << pow(x,2) << endl;
		 break;
	 case 2:
		 cout << pow(x,2) << endl;
		 break;
	 case 3:
		 cout << pow(x,2) << endl;
		 break;
	 case 4:
		 cout << cos(x) << endl;
		 break;
	 case 5:
		 cout << cos(x) << endl;
		 break;
	 case 6:
		 cout << cos(x) << endl;
		 break;
	 case 7:
		 cout << log(x) << endl;
		 break;
	 case 8:
		 cout << log(x) << endl;
		 break;
	 case 9:
		 cout << log(x) << endl;
		 break;
	 case 10:
		 cout << log(x) << endl;
		 break;
	 case 11:
		 cout << log(x) << endl;
		 break;
	 case 12:
		 cout << log(x) << endl;
		 break;
	 case 13:
		 cout << log(x) << endl;
		 break;
	 case 14:
		 cout << log(x) << endl;
		 break;
	 case 15:
		 cout << log(x) << endl;
		 break;
	 case 16:
		 cout << log(x) << endl;
		 break;
	 case 17:
		 cout << log(x) << endl;
		 break;
	 case 18:
		 cout << log(x) << endl;
		 break;
	 case 19:
		 cout << log(x) << endl;
		 break;
	 default:
		 cout << sqrt(x) << endl;	
	 }
	system ("pause");
	return 0;

 }
Last edited on
the variable x needs to be of an integer type to work in a switch, not double. You can cast it to double inside the case.

More over, you could easily do this with a for loop, rather than 20 cases in a switch - what if you had to do 1000 of these?

It is good to see you have a main function now - that was the cause of my rather direct comments earlier.

I was also thinking of trying to call the functions using void and call them in the main() body.


Not sure what you mean by that.

Hope all goes well, happy to help with any further queries :=D
Last edited on
I know it does seem impractical to use the switch this way but I cannot think of a way to apply the loop continuation conditions to shorten it. I was able to get it to work using static_cast<int> for the testing value in my switch. If interested I will try to post my instructors method of the solution. I appreciate the help, thanks!!
I cannot think of a way to apply the loop continuation conditions to shorten it.


With the casting, I meant the other way around:

1
2
3
4
5
6
7
8
9
double x;
int LogCount;

for(LogCount = 7;LogCount < 19;LogCount++) {
//cast LogCount to double & assign to x
//calc Log value


}


HTH
Topic archived. No new replies allowed.