Skip to content

Commit 7231236

Browse files
committed
Fix exceptions in tests caused by improper bounds checking
1 parent e79b35b commit 7231236

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

archive/c/c-plus-plus/insertion-sort.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ void insertSort(std::vector<int> &v)
77
{
88
int n = v.size();
99
int i = 0, j = 0, temp = 0;
10-
for (i = 1; i < n; i++)
10+
for (int i = 1; i < n; i++)
1111
{
1212
int store = v[i];
13-
j = i - 1;
14-
while (store < v[j] && j >= 0)
13+
int j = i - 1;
14+
15+
while (j >= 0 && v[j] > store)
1516
{
1617
v[j + 1] = v[j];
1718
j--;
1819
}
1920
v[j + 1] = store;
2021
}
21-
return;
2222
}
2323

2424
int main(int argc, char *argv[])

archive/c/c-plus-plus/roman-numeral.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using namespace std;
55

6-
bool is_roman(char x)
6+
bool is_invalid_roman(char x)
77
{
88
return !(x == 'I'
99
|| x == 'V'
@@ -27,7 +27,7 @@ int main(int argc, char *argv[])
2727

2828
for (char c : s)
2929
{
30-
if (is_roman(c))
30+
if (is_invalid_roman(c))
3131
{
3232
cerr << "Error: invalid string of roman numerals" << endl;
3333
exit(0);
@@ -47,7 +47,7 @@ int main(int argc, char *argv[])
4747

4848
for (int i = s.size() - 1; i >= 0; i--)
4949
{
50-
if (value[s[i]] > value[s[i - 1]] && i > 0)
50+
if (i > 0 && value[s[i]] > value[s[i - 1]])
5151
{
5252
num += value[s[i]] - value[s[i - 1]];
5353
i--;

0 commit comments

Comments
 (0)