-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitchcase.cpp
72 lines (63 loc) · 2.19 KB
/
switchcase.cpp
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
/*Problem statement
Programming languages have some conditional / decision-making statements that execute when some specific condition is fulfilled.
Switch-case is one of the ways to implement them.
In a menu-driven program, the user is given a set of choices of things to do (the menu) and then is asked to select a menu item.
There are 2 choices in the menu:
Choice 1 is to find the area of a circle having radius 'r'.
Choice 2 is to find the area of a rectangle having dimensions 'l' and 'b'.
You are given the choice 'ch' and an array 'a'.
If ‘ch’ is 1, ‘a’ contains a single number ‘r’. If ‘ch’ is 2, ‘a’ contains 2 numbers, ‘l’ and ‘b’.
Consider the choice and print the appropriate area.
Example :
Input: ‘ch’ = 2 and ‘a’ = [3, 2]
Output: area = 6
Explanation: Since the choice ‘ch’ is 2, we have to print the area of the rectangle having ‘l’ = 3 and ‘b’ = 2, which is 6.
Detailed explanation ( Input/output format, Notes, Images )
Sample Input 1:
2
3 2
Sample Output 1:
6.00000
Explanation of sample input 1 :
Since the choice ‘ch’ is 2, we have to print the area of the rectangle having ‘l’ = 3 and ‘b’ = 2, which is 6.
Sample Input 2:
1
3
Sample Output 2:
28.27433
Explanation of sample input 2 :
Since the choice ‘ch’ is 1, we have to print the area of the circle having ‘r’ = 3, which is approximately 28.274333882308138. Rounded off to 5 decimal places, we get 28.27433.
Expected time complexity :
The expected time complexity is O(1).
Constraints :
1 <= ‘ch’ <= 2
1 <= ‘r’ <= 100
1 <= ‘l’, ‘b’ <= 100
Time limit: 1 second
*/
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double areaSwitchCase(int ch, vector<double> a) {
switch (ch) {
case 1: {
double r = a[0];
return M_PI * r * r;
}
case 2: {
double l = a[0];
double b = a[1];
return l * b;
}
}
}
int main() {
int ch1 = 2;
vector<double> a1 = {3, 2};
cout << "Area: " << areaSwitchCase(ch1, a1) << endl;
int ch2 = 1;
vector<double> a2 = {3};
cout << "Area: " << areaSwitchCase(ch2, a2) << endl;
return 0;
}