1. ## Array Operations

Hello!
I'm having a difficult time with this bit of code
my value index is supposed to be set to the 'row' with the lowest value of "err"
but index is never set to anything higher than 0. no matter my input
'data' is 1 dimensional array taken from a file with 200 rows (numRows) and 6 columns (numCols) of data type double
Code:
```void safeTest(double *testPoint, double *data, int numRows, int numCols)
{
int r, c, i, index = 0;
double sum, err = 0;
double oper[numCols-1];
double temp = 1000000;

for(i = 0; i < numCols-1; i++)
{
oper[i] = testPoint[i];
}

for(r = 0; r < numRows; r++)
{
for(c = 0; c < numCols; c++)
{
oper[c] -= data[r*numCols+c];
oper[c] =  oper[c] * oper[c];
sum     += oper[c];
}

err = sqrt(sum);
sum = 0;

for(i = 0; i < numCols-1; i++)
{
oper[i] = testPoint[i];
}

if(temp > err)
{
temp = err;
index  = r;
}

}

if(data[index*numCols + 5] == 1)
{
printf("It is SAFE to move on\n");
}else
{
printf("It is NOT SAFE to move on\n");
}
}```
Thank you for any help!

2. Code:
```double oper[numCols-1];
...
for(c = 0; c < numCols; c++)```
You have a problem there.

Quzah.

3. Sorry I should have clarified that. That value doesn't include the last column of data so it is correct

4. Code:
```		for(c = 0; c < numCols; c++)
{
oper[c] -= data[r*numCols+c];
oper[c] =  oper[c] * oper[c];
sum     += oper[c];
}```
oper array max indice you can get safely is oper[numCols-2].
Array with dimension N, valid indices [0-N-1].

5. Originally Posted by cswimmer
Sorry I should have clarified that. That value doesn't include the last column of data so it is correct
That's not the problem I was talking about. Say numCols is 2. Then:
Code:
```double oper[2-1]; /* 2 - 1 = 1 */
...
for(c = 0; c < 2 /* loop for element 0 and element 1 */; c++)
... oops, oper[ 1 ] is out of bounds of your array```
Quzah.

6. Originally Posted by Bayint Naung
oper array max indice you can get safely is oper[numCols-2].
Array with dimension N, valid indices [0-N-1].
Thanks Bayint I was able to fix it with this:
Code:
```int safeTest(double *testPoint, double *data, int numRows, int numCols){
int r, c, i, index = 0;
double sum, eucl = 0;
double oper[numCols];
double temp = 10000000.0;

for(r = 0; r < numRows; r++){
for(c = 0; c < numCols; c++){
oper[c] = data[r*(numCols+1)+c];
sum += (oper[c] - testPoint[c])*(oper[c] - testPoint[c]);
}
eucl = sqrt(sum);
sum = 0;

if(temp > eucl){

temp = eucl;
index  = r;
}
}
if(data[index*(numCols+1) + 5] == 1.0){

printf("It is SAFE to move on\n");
}else{
printf("It is NOT SAFE to move on\n");
}```