我的功能
int numSteepSegments(const int heights[], int arrSize) {
int mile = 0, steep_count = 0;
while (mile <= arrSize) {
int height = heights[mile];
int height_next = heights[mile 1];
if ((height 1000) < height_next) {
steep_count;
} else if ((height - 1000) > height_next) {
steep_count;
}
cout << heights[mile] << endl;
cout << steep_count << endl;
mile;
}
return steep_count;
}
吐出的陡峭計數是預期的兩倍。
使用陣列:
1200, 1650, 3450, 2800, 2900, 1650, 1140, 1650, 1200和arrSize = 9,我錯過了什么?
是cout:
1200
0
1650
1
3450
1
2800
1
2900
2
1650
2
1140
2
1650
2
1200
3
1746942513
4
最后一個值是多少?它顯然不在陣列中,而且我看不到它屬于我正在處理的數字附近的任何地方。我在條件句中沒有看到什么導致錯誤的增量steep_count?
uj5u.com熱心網友回復:
C/C 陣列是從零開始的。arrSize包含元素的陣列的索引范圍從0到arrSize-1。
您的回圈索引mile范圍從0到arrSize(含),所以heights[mile]從陣列的末尾走開。此外,heights[mile 1]即使您的索引限制為arrSize-1.
嘗試:
- 改變你的回圈范圍從
0到arrSize-2(含),或 - 將回圈更改為范圍 from
1并arrSize-1用于mile-1第一個索引。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/432106.html
上一篇:兩個影像之間的視頻變形,FFMPEG/Minterpolate
下一篇:C 模板顯式右值型別
