Calculate Height of a 2D Array of 0's and 1's in C -


i'm trying write program finds height of pixels of "image". "image" 2d array of 0's , 1's, 0's being white space, 1's being black pixels.

the nested loop goes through array left right, first through x-axis, y-axis.

i'm trying find highest value of y-value. plan store y values on 0 in new array, , find highest y-value. however, below code has stored y=5 array reason.

i'm thinking order wrong maybe?

#include <stdio.h>  #define max_x       20 #define max_y       20  void printimage(int image[max_x][max_y]); int countblackpixels(int image[max_x][max_y]); int findypixel(int image[max_x][max_y]);  //function prototypes  void processimage(int image[max_x][max_y]) {      int ypixel;     ypixel = findypixel(image);     printf("height: %d\n", ypixel);  }  //height //calculates height of image   int findypixel(int image[max_x][max_y]) {     int x, y, ypixel, i;     int new_array[max_y-1];     x = 0;     ypixel=-1;     while (x < max_x) {         y = max_y-1;         while (y > 0) {             if (image[x][y] == 1) {                 ypixel=y;                 }             y = y - 1;         }         x = x + 1;     }     return ypixel; } 

does see i'm going wrong?

this loop assigns all values in array current y value:

for(i=0; < max_y; i++) {     new_array[i] = y;  //store y values in new array } 

that's why end 1 value @ end.

you don't need store array of y values. find first non-zero entry in 2d-array, can return current y value answer. (this assumes start searching @ top of image , work way down.)

even if loop bottom up, still don't need array. update ypixel = y; each time encounter non-zero pixel. @ end of loop, ypixel y-value highest non-blank pixel. should start ypixel = -1 or something, helps identify case when whole image blank.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -