fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. #define MAX_X 20
  6. #define MAX_Y 20
  7.  
  8. int countBlackPixels(int image[MAX_X][MAX_Y]);
  9. int* findYPixel(int image[MAX_X][MAX_Y]);
  10. int* findXPixel(int image[MAX_X][MAX_Y]);
  11. double findTallness(int *,int *);
  12.  
  13. //function prototypes here
  14.  
  15. void processImage(int image[MAX_X][MAX_Y]) {
  16.  
  17. int nBlackPixels;
  18. nBlackPixels = countBlackPixels(image);
  19. printf("Pixel-count: %d\n", nBlackPixels);
  20.  
  21. int *pixelHeight;
  22. pixelHeight = findYPixel(image);
  23. printf("Height: %d\n", *pixelHeight);
  24.  
  25. int *pixelWidth;
  26. pixelWidth = findXPixel(image);
  27. printf("Width: %d\n", *pixelWidth);
  28.  
  29. double tallness;
  30. tallness = findTallness(pixelWidth, pixelHeight);
  31. printf("Tallness: %.6lf\n", tallness);
  32. }
  33.  
  34. //Count Black Pixels
  35. int countBlackPixels(int image[MAX_X][MAX_Y]) {
  36. int x, y, blackPixelCount;
  37. blackPixelCount = 0;
  38. x = 0;
  39. while (x < MAX_X) {
  40. y = 0;
  41. while (y < MAX_X) {
  42. if (image[x][y] == 1) {
  43. blackPixelCount = blackPixelCount + 1;
  44. //printf("black pixel at x=%d y=%d\n", x, y); // example debug printf
  45. }
  46. y = y + 1;
  47. }
  48. x = x + 1;
  49. }
  50. return blackPixelCount;
  51. }
  52.  
  53. //Height
  54. //Loops through from bottom-left to the right, and then up
  55. //through the Y axis, find the last occurance of Y,
  56. //adds one to overcome array's zero count, and
  57. //returns this value as the pixel height
  58.  
  59. int * findYPixel(int image[MAX_X][MAX_Y]) {
  60. int x, y, *pixelHeight;
  61. pixelHeight = malloc(sizeof(int));
  62. y = 0;
  63. *pixelHeight = -1;
  64. while (y < MAX_X) {
  65. x = 0;
  66. while (x < MAX_X) {
  67. if (image[x][y] == 1) {
  68. *pixelHeight = y+1;
  69. }
  70. x = x + 1;
  71. }
  72. y = y + 1;
  73. }
  74. return pixelHeight;
  75. }
  76.  
  77. //Width
  78. //Loops through from bottom-left to the top, and then across
  79. //through the X axis, find the last occurance of X, adds 1
  80. //to deal with array 0 count, and
  81. //returns this value as the pixel width
  82.  
  83. int * findXPixel(int image[MAX_X][MAX_Y]) {
  84. int x, y, *pixelWidth;
  85. pixelWidth = malloc(sizeof(int));
  86. x = 0;
  87. while (x < MAX_X) {
  88. y = 0;
  89. while (y < MAX_Y) {
  90. if (image[x][y] == 1) {
  91. *pixelWidth = x+1;
  92. }
  93. y = y + 1;
  94. }
  95. x = x + 1;
  96. }
  97. return pixelWidth;
  98. }
  99.  
  100.  
  101. //Tallness function
  102. double findTallness(int *pixelWidth, int *pixelHeight) {
  103. double tallness;
  104. tallness = (1.0 * *pixelWidth) / *pixelHeight;
  105. printf("tall=%.6lf\n", tallness);
  106.  
  107. return tallness;
  108. }
  109.  
  110. int main(){
  111.  
  112. return 0;
  113. }
Success #stdin #stdout 0s 2108KB
stdin
Standard input is empty
stdout
Standard output is empty