Skip to content

Commit 08415b0

Browse files
authored
Merge pull request #565 from kvedala/lgtm-fixes
[LGTM alerts] fixes to some of the LGTM alerts
2 parents 81f4428 + 47958fb commit 08415b0

File tree

3 files changed

+201
-49
lines changed

3 files changed

+201
-49
lines changed

misc/cartesian_to_polar.c

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,66 @@
1+
/**
2+
* @file
3+
* @brief Function to convert a Cartesian co-ordinate to polar form.
4+
*/
5+
#define _USE_MATH_DEFINES /**< required for MS Visual C */
6+
#include <assert.h>
17
#include <math.h>
28
#include <stdio.h>
3-
4-
const double pi = 3.141592653589793238462643383279502884;
9+
#include <stdlib.h>
510

611
/**
7-
give as arguments to the executable two x and y coordinates
8-
outputs a polar coordinate
9-
*/
10-
int main()
12+
* @brief Function to convert cartesian coordinates to polar.
13+
*\f{eqnarray*}{
14+
r &=& \sqrt{x^2+y^2}\\
15+
\theta &=& \atan\frac{y}{x}
16+
\f}
17+
* @param [in] x absicca value
18+
* @param [in] y ordinate value
19+
* @param [out] r pointer to store polar radius
20+
* @param [out] theta pointer to store polar angle (in radian)
21+
*/
22+
void to_polar(double x, double y, double *r, double *theta)
1123
{
12-
double x, y;
13-
double r, theta, thetaFinal;
14-
scanf("%lf %lf", &x, &y);
15-
r = hypot(x, y);
24+
double thetaFinal = 0.f;
25+
26+
*r = sqrt(x * x + y * y);
27+
1628
if (x != 0)
1729
{
1830
if (y != 0)
1931
{
20-
theta = atan(y / x);
32+
*theta = atan(y / x);
2133
if ((x > 0 && y > 0) || (x == -y))
2234
{ // Q1
23-
thetaFinal = theta;
35+
thetaFinal = *theta;
2436
}
2537
else if (x < 0 && y > 0)
2638
{ // Q2
27-
thetaFinal = theta + pi;
39+
thetaFinal = *theta + M_PI;
2840
}
2941
else if (x < 0 && y < 0)
3042
{ // Q3
31-
thetaFinal = theta - pi;
43+
thetaFinal = *theta - M_PI;
3244
}
3345
else if (x > 0 && y < 0)
3446
{ // Q4
35-
thetaFinal = 2 * pi - theta;
47+
thetaFinal = 2 * M_PI - *theta;
48+
}
49+
else
50+
{
51+
fprintf(stderr, "Should not reach here!\n");
3652
}
3753
}
3854
}
39-
if (x == 0)
55+
else
4056
{ // exceptions when no actual angle is present
4157
if (y > 0)
4258
{
43-
thetaFinal = pi / 2;
59+
thetaFinal = M_PI / 2;
4460
}
4561
else
4662
{
47-
thetaFinal = -(pi / 2);
63+
thetaFinal = -(M_PI / 2);
4864
}
4965
}
5066
if (y == 0)
@@ -55,8 +71,53 @@ int main()
5571
}
5672
else
5773
{
58-
thetaFinal = -pi;
74+
thetaFinal = -M_PI;
5975
}
6076
}
61-
printf("%.2f %.2f\n", r, atan2(y, x));
77+
78+
*theta = thetaFinal;
79+
}
80+
81+
/**
82+
* @brief Generate a random number in the given limits
83+
*
84+
* @param lim1 lower limit
85+
* @param lim2 upper limit
86+
* @return random number in the given range
87+
*/
88+
double get_rand(double lim1, double lim2)
89+
{
90+
double r = (double)rand() / RAND_MAX; // value in [0,1)
91+
return (lim2 - lim1) * r + lim1; // scale to range
92+
}
93+
94+
/**
95+
* @brief Test implementation
96+
*
97+
*/
98+
void test()
99+
{
100+
srand(10);
101+
int NUM_TESTS = 5;
102+
103+
for (int i = 0; i < NUM_TESTS; i++)
104+
{
105+
double r, theta;
106+
printf("Test %d.... ", i);
107+
double x = get_rand(-5, 5);
108+
double y = get_rand(-5, 5);
109+
printf("(%.2g, %.2g).... ", x, y);
110+
to_polar(x, y, &r, &theta);
111+
assert(fabs(r - hypot(x, y)) < 0.01);
112+
assert(fabs(theta - atan2(y, x)) < 0.01);
113+
printf("passed\n");
114+
}
115+
}
116+
117+
/** Main function */
118+
int main()
119+
{
120+
test();
121+
122+
return 0;
62123
}

misc/union_find.c

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,89 @@
1+
/**
2+
* @file union_find.c
3+
* @brief [Union
4+
* find](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) algorithm.
5+
*/
16
#include <stdio.h>
7+
#include <stdlib.h>
8+
#define MAX_SIZE 1000 /**< maximum number of elements in the set */
29

3-
int p[1000000];
4-
int find(int x)
10+
/**
11+
* @brief Find index of or value in an array
12+
*
13+
* @param [in,out] p array to search and update
14+
* @param x value to search
15+
* @return value at the index `x`
16+
*/
17+
int find(int *p, int x)
518
{
19+
if (x >= MAX_SIZE)
20+
{
21+
fprintf(stderr, "Out-of bounds value\n");
22+
exit(EXIT_FAILURE);
23+
}
24+
625
if (p[x] == x)
726
{
827
return x;
928
}
1029
else
1130
{
12-
p[x] = find(p[x]);
31+
p[x] = find(p, p[x]);
1332
return p[x];
1433
}
1534
}
16-
// Call to function join(int x, int y) to join PARAM x and y
17-
void join(int x, int y) { p[find(x)] = find(y); }
1835

36+
/**
37+
* @brief Function to join
38+
* @param [in,out] p array to join in
39+
* @param x value or index to join to
40+
* @param y value or index to join from
41+
*/
42+
void join(int *p, int x, int y) { p[find(p, x)] = find(p, y); }
43+
44+
/** Main function */
1945
int main()
2046
{
21-
// Have all array indexes that you need to use refrence themselves
47+
int union_set[MAX_SIZE];
48+
49+
// Have all array indexes that you need to use reference themselves
2250
for (int i = 0; i < 10; i++)
2351
{
24-
p[i] = i;
52+
union_set[i] = i;
2553
}
2654
// p = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
27-
join(3, 5);
55+
56+
join(union_set, 3, 5);
57+
printf("The array is now: ");
58+
for (int i = 0; i < 10; i++)
59+
{
60+
printf("%d ", union_set[i]);
61+
}
62+
printf("\n");
2863
// Now 3 and 5 are groupped together, that is find(3) = find(5)
2964
// p = {0, 1, 2, 5, 4, 5, 6, 7, 8, 9}
30-
join(3, 8);
65+
66+
join(union_set, 3, 8);
67+
printf("The array is now: ");
68+
for (int i = 0; i < 10; i++)
69+
{
70+
printf("%d ", union_set[i]);
71+
}
72+
printf("\n");
73+
3174
// Now 3, 5 and are groupped together, find(3) = find(5) = find(8)
3275
// p = {0, 1, 2, 5, 4, 8, 6, 7, 8, 9}
33-
join(0, 5);
34-
if (find(0) == find(3))
76+
join(union_set, 0, 5);
77+
if (find(union_set, 0) == find(union_set, 3))
3578
{
3679
printf("0 and 3 are groupped together\n");
3780
}
3881
printf("The array is now: ");
3982
for (int i = 0; i < 10; i++)
4083
{
41-
printf("%d ", p[i]);
84+
printf("%d ", union_set[i]);
4285
}
4386
printf("\n");
4487

4588
return 0;
46-
}
89+
}

searching/jump_search.c

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,85 @@
1+
/**
2+
* @file jump_search.c
3+
* @brief Implementation of [jump
4+
* search](https://en.wikipedia.org/wiki/Jump_search) algorithm.
5+
*/
6+
#include <assert.h>
17
#include <math.h>
28
#include <stdio.h>
3-
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
4-
int jump_search(int *arr, int x);
5-
int n;
69

7-
int main()
8-
{
9-
int arr[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610};
10-
n = sizeof(arr) / sizeof(int);
11-
int x = 55;
12-
int index = jump_search(arr, x);
13-
printf("\nNumber %d is at index %d\n", x, index);
14-
}
10+
/**
11+
* @brief Macro to return the minimum of two values
12+
*/
13+
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
1514

16-
int jump_search(int *arr, int x)
15+
/**
16+
* @brief Implement Jump-search algorithm
17+
*
18+
* @param [in] arr Array to search within
19+
* @param x value to search for
20+
* @param n length of array
21+
* @return index where the value was found
22+
* @return -1 if value not found
23+
*/
24+
int jump_search(const int *arr, int x, size_t n)
1725
{
1826
int step = floor(sqrt(n));
1927
int prev = 0;
20-
while (*(arr + (min(step, n) - 1)) < x)
28+
29+
while (arr[min(step, n) - 1] < x)
2130
{
2231
prev = step;
2332
step += floor(sqrt(n));
2433
if (prev >= n)
34+
{
2535
return -1;
36+
}
2637
}
2738

28-
while (*(arr + prev) < x)
39+
while (arr[prev] < x)
2940
{
3041
prev = prev + 1;
31-
if (prev == fmin(step, n))
42+
if (prev == min(step, n))
43+
{
3244
return -1;
45+
}
3346
}
34-
if (*(arr + prev) == x)
47+
if (arr[prev] == x)
48+
{
3549
return prev;
50+
}
3651
return -1;
3752
}
53+
54+
/**
55+
* @brief Test implementation of the function
56+
*
57+
*/
58+
void test()
59+
{
60+
int arr[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610};
61+
size_t n = sizeof(arr) / sizeof(int);
62+
63+
int x = 55;
64+
printf("Test 1.... ");
65+
int index = jump_search(arr, x, n);
66+
assert(index == 10);
67+
printf("passed\nTest 2.... ");
68+
x = 56;
69+
index = jump_search(arr, x, n);
70+
assert(index == -1);
71+
printf("passed\nTest 3.... ");
72+
x = 13;
73+
index = jump_search(arr, x, n);
74+
assert(index == 7);
75+
printf("passed\n");
76+
}
77+
78+
/**
79+
* @brief Main function
80+
*/
81+
int main()
82+
{
83+
test();
84+
return 0;
85+
}

0 commit comments

Comments
 (0)