-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsolution.c
More file actions
35 lines (27 loc) · 768 Bytes
/
Copy pathsolution.c
File metadata and controls
35 lines (27 loc) · 768 Bytes
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
// PWR080: Conditionally initialized variables can lead to undefined behavior
#include <stdio.h>
typedef enum {
OPTION_HALF,
OPTION_DOUBLE,
OPTION_UNKNOWN,
} TransformOption;
__attribute__((pure)) double transform_and_sum(const double *array, size_t size,
TransformOption option) {
double sum = 0.0;
// Identity transformation by default
double factor = 1.0;
if (option == OPTION_HALF) {
factor = 0.5;
} else if (option == OPTION_DOUBLE) {
factor = 2.0;
}
for (size_t i = 0; i < size; ++i) {
sum += array[i] * factor;
}
return sum;
}
int main() {
double array[] = {0.25, 0.25, 0.25, 0.25};
printf("Sum is: %f\n", transform_and_sum(array, 4, OPTION_UNKNOWN));
return 0;
}