2016-05-29 5 views
0

"画像"の属性を計算するプログラムを作成しようとしています。 「画像」は0と1の2次元配列であり、0は空白、1は黒画素である。Cの3番目の関数で2つの異なる関数からポインタを使用する方法

私は高さを計算する必要があります。ピクセルの高さはpixelHeight/pixelWidthですが、ポインタの構文が正しいとは限りません。私は3番目の関数(tallness)で前の2つの関数のポインタを呼びたいと思います。

#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; 
    } 

どこが間違っているのか誰にでも見られますか?

+1

どのポインタですか?コンパイラから何のエラーが出ていますか、何を試していますか? –

答えて

1

機能の宣言を確認してください。関数定義と互換性がありません。

#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; 
    } 

あなたがfindXPixelfindYPixelで整数を格納するためのメモリを割り当てていませんが、次のようにコンパイルするように、私は、コードを変更しました。次のようにメモリを割り当てるには、変数の初期化:processImage

同様

pixelHeight = malloc(sizeof(int));

、また pixelWidth = malloc(sizeof(int));

、 を使用すると、ポインタ*pixelHeight*pixelWidth間接参照する必要はありません。 は、あなたがこのような計算を実行するときdoubleintをキャストする必要が

tallness = (*pixelWidth * 1.0)/ *pixelHeight; 

tallness = *pixelWidth/*pixelHeight;を置き換え、また

pixelHeight = findYPixel(image); 
pixelWidth = findXPixel(image); 

としてそれらを使用してください。

Demo

+0

ありがとうございました! * pixelWidth * 1.0が何をしているのか尋ねることができますか? それはintからdoubleに変わるものですか? –

+1

@BMarino: '* pixelWidth * 1.0'は、' double'定数で乗算される整数である '* pixelWidth'をもたらします。これは、組み合わされた値の' int'から 'double'への変換を引き起こします。 'double'を' int'で割った値はdoubleのままです。 簡略化するために、同じ結果を達成したより冗長なバージョンの 'tallness =((double)* pixelWidth)/ * pixelHeight'に置き換えることもできます。 –

関連する問題