I'm trying to write a program that calculates attributes of an "image". The "image" is a 2D array of 0's and 1's, 0's being white space, 1's being black pixels.
I need to calculate tallness, which is just pixelHeight/pixelWidth, but I can't get the syntax of the pointers correct. I'd like to call on pointers from the previous two functions in my third (tallness) function.
#include <stdio.h>
#include <math.h>
#define MAX_X 20
#define MAX_Y 20
int countBlackPixels(int image[MAX_X][MAX_Y]);
int findYPixel(int image[MAX_X][MAX_Y]);
int findXPixel(int image[MAX_X][MAX_Y]);
double findTallness(int image[MAX_X][MAX_Y]);
//function prototypes here
void processImage(int image[MAX_X][MAX_Y]) {
int nBlackPixels;
nBlackPixels = countBlackPixels(image);
printf("Pixel-count: %d\n", nBlackPixels);
int *pixelHeight;
*pixelHeight = findYPixel(image);
printf("Height: %d\n", *pixelHeight);
int *pixelWidth;
*pixelWidth = findXPixel(image);
printf("Width: %d\n", *pixelWidth);
double tallness;
tallness = findTallness(tallness);
printf("Tallness: %.6lf\n", tallness);
}
//Count Black Pixels
int countBlackPixels(int image[MAX_X][MAX_Y]) {
int x, y, blackPixelCount;
blackPixelCount = 0;
x = 0;
while (x < MAX_X) {
y = 0;
while (y < MAX_X) {
if (image[x][y] == 1) {
blackPixelCount = blackPixelCount + 1;
//printf("black pixel at x=%d y=%d\n", x, y); // example debug printf
}
y = y + 1;
}
x = x + 1;
}
return blackPixelCount;
}
//Height
//Loops through from bottom-left to the right, and then up
//through the Y axis, find the last occurance of Y,
//adds one to overcome array's zero count, and
//returns this value as the pixel height
int * findYPixel(int image[MAX_X][MAX_Y]) {
int x, y, *pixelHeight;
y = 0;
*pixelHeight = -1;
while (y < MAX_X) {
x = 0;
while (x < MAX_X) {
if (image[x][y] == 1) {
*pixelHeight = y+1;
}
x = x + 1;
}
y = y + 1;
}
return pixelHeight;
}
//Width
//Loops through from bottom-left to the top, and then across
//through the X axis, find the last occurance of X, adds 1
//to deal with array 0 count, and
//returns this value as the pixel width
int * findXPixel(int image[MAX_X][MAX_Y]) {
int x, y, *pixelWidth;
x = 0;
while (x < MAX_X) {
y = 0;
while (y < MAX_Y) {
if (image[x][y] == 1) {
*pixelWidth = x+1;
}
y = y + 1;
}
x = x + 1;
}
return pixelWidth;
}
//Tallness function
double findTallness(int *pixelWidth, int *pixelHeight) {
double tallness;
tallness = *pixelWidth / *pixelHeight;
printf("tall=%.6lf\n", tallness);
return tallness;
}
Can anyone see where it's going wrong?
Please check your function declarations. They are not compatible with function definitions. I've modified the code so that it compiles as follows
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX_X 20
#define MAX_Y 20
int countBlackPixels(int image[MAX_X][MAX_Y]);
int* findYPixel(int image[MAX_X][MAX_Y]);
int* findXPixel(int image[MAX_X][MAX_Y]);
double findTallness(int *,int *);
//function prototypes here
void processImage(int image[MAX_X][MAX_Y]) {
int nBlackPixels;
nBlackPixels = countBlackPixels(image);
printf("Pixel-count: %d\n", nBlackPixels);
int *pixelHeight;
pixelHeight = findYPixel(image);
printf("Height: %d\n", *pixelHeight);
int *pixelWidth;
pixelWidth = findXPixel(image);
printf("Width: %d\n", *pixelWidth);
double tallness;
tallness = findTallness(pixelWidth, pixelHeight);
printf("Tallness: %.6lf\n", tallness);
}
//Count Black Pixels
int countBlackPixels(int image[MAX_X][MAX_Y]) {
int x, y, blackPixelCount;
blackPixelCount = 0;
x = 0;
while (x < MAX_X) {
y = 0;
while (y < MAX_X) {
if (image[x][y] == 1) {
blackPixelCount = blackPixelCount + 1;
//printf("black pixel at x=%d y=%d\n", x, y); // example debug printf
}
y = y + 1;
}
x = x + 1;
}
return blackPixelCount;
}
//Height
//Loops through from bottom-left to the right, and then up
//through the Y axis, find the last occurance of Y,
//adds one to overcome array's zero count, and
//returns this value as the pixel height
int * findYPixel(int image[MAX_X][MAX_Y]) {
int x, y, *pixelHeight;
pixelHeight = malloc(sizeof(int));
y = 0;
*pixelHeight = -1;
while (y < MAX_X) {
x = 0;
while (x < MAX_X) {
if (image[x][y] == 1) {
*pixelHeight = y+1;
}
x = x + 1;
}
y = y + 1;
}
return pixelHeight;
}
//Width
//Loops through from bottom-left to the top, and then across
//through the X axis, find the last occurance of X, adds 1
//to deal with array 0 count, and
//returns this value as the pixel width
int * findXPixel(int image[MAX_X][MAX_Y]) {
int x, y, *pixelWidth;
pixelWidth = malloc(sizeof(int));
x = 0;
while (x < MAX_X) {
y = 0;
while (y < MAX_Y) {
if (image[x][y] == 1) {
*pixelWidth = x+1;
}
y = y + 1;
}
x = x + 1;
}
return pixelWidth;
}
//Tallness function
double findTallness(int *pixelWidth, int *pixelHeight) {
double tallness;
tallness = (1.0 * *pixelWidth) / *pixelHeight;
printf("tall=%.6lf\n", tallness);
return tallness;
}
You are not allocating memory to store integers in findXPixel
and findYPixel
. To allocate memory, initialize the variables as follows:
pixelHeight = malloc(sizeof(int));
Similarly, pixelWidth = malloc(sizeof(int));
In addition, in processImage
, you do not need to dereference the pointers *pixelHeight
and *pixelWidth
. Use them as
pixelHeight = findYPixel(image);
pixelWidth = findXPixel(image);
Also, replace tallness = *pixelWidth / *pixelHeight;
with
tallness = (*pixelWidth * 1.0)/ *pixelHeight;
You need to cast int
to double
when performing such calculation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.