Thread: how can l make this shorter?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    4

    how can l make this shorter?

    so this is what l basically have to do 27 times:
    Code:
    // counts black pixels in row 1
    
    int
    row1Pixels(int digit_image[DIGIT_HEIGHT][DIGIT_WIDTH]) {
        int column, black_pixels;
        black_pixels = 0;
        for (column = 0; column < DIGIT_WIDTH; column++) {
        	if (digit_image[1][column] == 1) {
            	black_pixels = black_pixels + 1;
            }
        } 
        return black_pixels;
    }
    
    // counts black pixels in row 2
    
    int
    row2Pixels(int digit_image[DIGIT_HEIGHT][DIGIT_WIDTH]) {
        int column, black_pixels;
        black_pixels = 0;
        for (column = 0; column < DIGIT_WIDTH; column++) {
        	if (digit_image[2][column] == 1) {
            	black_pixels = black_pixels + 1;
            }
        } 
        return black_pixels;
    }
    can anybody show me a function which does the exact same thing but does it in just 1 function?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Since the only variation (apart from function name) is the first index when accessing elements of digital_image, pass that index as a second argument (of type int) to the function.

    If you don't know how to pass more than one argument to a function, look it up. All basic textbooks and tutorials related to functions describe how.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Or pass the desired row-of-DIGIT_WIDTH-columns directly as the function's only argument.
    Last edited by migf1; 05-28-2013 at 09:25 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh my!

    Code:
    // counts black pixels in row 1
     
    int
    row1Pixels(int digit_image[DIGIT_HEIGHT][DIGIT_WIDTH]) {
        int column, black_pixels;
        black_pixels = 0;
        for (column = 0; column < DIGIT_WIDTH; column++) {
            if (digit_image[1][column] == 1) {
                black_pixels = black_pixels + 1;
            }
        }
        return black_pixels;
    }
    
    // counts black pixels in all rows. This function is called 27 times, 
    // but that's not a bad thing.
     
    int countBlackPixels(int digit_image[DIGIT_HEIGHT][DIGIT_WIDTH], int row) {
        int column, black_pixels;
        black_pixels = 0;
        for (column = 0; column < DIGIT_WIDTH; column++) {
            if (digit_image[row][column] == 1) {
                black_pixels = black_pixels + 1;
            }
        }
        return black_pixels;
    }
    The calling function will increment the value of the variable row (which would be the value of the customary variable 'i', in the following for loop:
    Code:
    int blackPixelSum=0;
    int i;
    for(i=0;i<DIGIT_HEIGHT;i++) {
       blackPixelSum+=countBlackPixels(digit_image[DIGIT_HEIGHT][DIGIT_WIDTH], i);
    }
    So 'i' is i in the calling function, but in the countBlackPixels() function, 'i' becomes 'row'.

    This isn't the only way to do this, but it's one good way to do it.
    Last edited by Adak; 05-28-2013 at 11:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shorter code
    By czarny020 in forum C Programming
    Replies: 10
    Last Post: 02-14-2011, 12:32 PM
  2. How can I improve this code or make it shorter?
    By abh!shek in forum C++ Programming
    Replies: 27
    Last Post: 01-13-2008, 01:07 PM
  3. Shorter notation?
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 06-14-2003, 11:35 AM
  4. Any way to make shorter???
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-14-2002, 04:02 AM
  5. is there a better/shorter way to do a menu
    By sizzle_chest in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2001, 04:09 PM