-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ16.cpp
More file actions
135 lines (106 loc) · 2.39 KB
/
Q16.cpp
File metadata and controls
135 lines (106 loc) · 2.39 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 16. Check if all the elements in arr = [3, 5, 9, 1, 7] are positive numbers, and print true or false.
#include <iostream>
using namespace std;
bool checkPositive(int arr[], int size) {
for (int i = 0; i < size; i++) {
if (arr[i] < 0) {
return false;
}
}
return true;
}
int main(){
int arr[] = {3, 5, 9, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);
cout << (checkPositive(arr, size) ? "true" : "false") << endl;
return 0;
}
//
#include <iostream>
using namespace std;
bool areAllPositiveBruteForce(int arr[], int size) {
for (int i = 0; i < size; i++) {
if (arr[i] <= 0) {
return false;
}
}
return true;
}
int main() {
int arr[] = {3, 5, 9, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);
if (areAllPositiveBruteForce(arr, size)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
//
#include <iostream>
using namespace std;
bool areAllPositiveEasy(int arr[], int size) {
bool allPositive = true;
for (int i = 0; i < size; i++) {
if (arr[i] <= 0) {
allPositive = false;
break;
}
}
return allPositive;
}
int main() {
int arr[] = {3, 5, 9, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);
if (areAllPositiveEasy(arr, size)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
//
#include <iostream>
using namespace std;
bool isNonPositive(int num) {
return num <= 0;
}
bool areAllPositiveModerate(int arr[], int size) {
for (int i = 0; i < size; i++) {
if (isNonPositive(arr[i])) {
return false;
}
}
return true;
}
int main() {
int arr[] = {3, 5, 9, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);
if (areAllPositiveModerate(arr, size)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
//
#include <iostream>
using namespace std;
bool areAllPositiveOptimal(int arr[], int size) {
for (int i = 0; i < size; i++) {
if (!(arr[i] > 0)) {
return false;
}
}
return true;
}
int main() {
int arr[] = {3, 5, 9, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);
if (areAllPositiveOptimal(arr, size)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}