Thread: where's error ?!

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    29

    where's error ?!

    I can run the program but it doesn't goes well at the end ..
    it didn't show where is the brightest point at ..

    Code:
    #include<stdio.h>
    
    
    #define x_dim 5
    #define y_dim 5
    
    
    int main( void )
    {
    	int aiRawdata[ x_dim ][y_dim];
    	int aiMaxCoords[ x_dim * y_dim][2];
    	int icurrentmax;
    	int inumbrightest;
    	int index1, index2, iloop;
    
    
    	icurrentmax = -1;
    	inumbrightest = 0;
    	for ( index1=0; index1 < x_dim; ++index1 ) {
    		for (index2 =0; index2 < y_dim; ++index2) {
    
    
    			printf("Enter the brightness data coordinate [%d][%d] :" , index1, index2 );
    			scanf("%d", &aiRawdata [index1][index2] );
    
    
    			if (aiRawdata [index1][index2] == icurrentmax)
    			{
    				aiMaxCoords[inumbrightest][0] = index1;
    				aiMaxCoords[inumbrightest][1] = index2;
    				inumbrightest++;
    			}
    
    
    			else if (aiRawdata [index1][index2] > icurrentmax);
    			{
    				icurrentmax = aiRawdata[index1][index2];
    				inumbrightest = -1;
    				aiMaxCoords[0][0] = index1;
    				aiMaxCoords[0][1] = index2;
    			}
    			}
    		}
    
    
    		printf("there were %d pixels with max brightest %d as follows:\n", inumbrightest, icurrentmax);
    
    
    		for (iloop =0; iloop < inumbrightest; ++iloop) {
    		printf("Pixel at [%d][%d]\n", aiMaxCoords[iloop][0], aiMaxCoords [iloop][1]);
    	}
    
    
    			return (0);
    		}
    can someone tell me the error ?

    thank you !

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Program design error.

    You need to find the max value.
    Then, find all the locations with that max value.

    Trying to do both at the same time (inside the same loops) is not easy and may be impossible.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    29
    so there's no way in solving it ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When you find a new maximum, I believe that you should set inumbrightest to 1, not -1.

    But yeah, using two separate loops would be easier to get right.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2012, 03:33 AM
  2. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM