Thread: Array Operations

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    double oper[numCols-1];
    ...
    for(c = 0; c < numCols; c++)
    You have a problem there.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Sorry I should have clarified that. That value doesn't include the last column of data so it is correct

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cswimmer View Post
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Quote Originally Posted by Bayint Naung View Post
    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;   
    	char advance[100] = "no";
    
        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");
    	 }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

Tags for this Thread