Hi, first step: compile with a high level of warnings. I used cpp.sh (the gear icon top right of the code), I turned on all 3 warning options
In function 'int indexOfMin(double*, int)':
14:9: warning: variable 'minIndex' set but not used [-Wunused-but-set-variable]
18:1: warning: no return statement in function returning non-void [-Wreturn-type] |
Warnings are your friend, they tell you about potential problems in your code. Code may compile without many warning options, but will crash - as you have discovered. When I am coding I use as many warning options as I can, and am not finished until I have no warnings at all.
Line 10 assumes that there are 10 elements in the array. Specify the size of the array like this:
1 2
|
// in main()
const unsigned int SIZE = 10;
|
even though the compiler can count how many items are in the array, the program becomes fragile if this size is not a
const
variable and used throughout the code:
double v[SIZE] = {10.0, 9.3, 4.4, 9.1, 3.4, 11.0,43.0, 0.0, 1.0, 0.0};
Edit: Don't mix unsigned and signed types, because SIZE is unsigned we nee to changed the for loops as well:
15 16 17
|
for (unsigned int i=startIndex;i<SIZE;i++){
if (v[i]<minValue) {minValue=v[i]; minIndex=i;}
}
|
You should also pass this SIZE variable to all functions that need it.
Try to avoid using
printf
.
std::cout
is safer, although more verbose.
If you want to have a variable number of elements in a container use
std::vector
Good Luck !!