2016-07-02 7 views
0

私の質問は、だから私は動的に行要素とfloatの配列を割り当て、割り当てが失敗した場合はNULLを返すようにするとしています動的割り当てとアレイ

float *rowavg(float *matrix,int rows,int cols) 

第2の機能を機密レベル変更されます。私はこれを正しくしたと思いますよね?私がやろうとしている他の部分は、新しい配列のi番目の要素を前の配列のi番目の行の値の平均値に設定します。この部分は私が起きているところです。私は以前の配列を正しく呼びましたか(私はノーだと思います)?

#include <stdio.h> 
#include <stdlib.h> 

float *readMatrix(int rows, int cols); 
float *rowavg(float *matrix, int rows, int cols); 
float *colavg(float *matrix, int rows, int cols); 

#define MAX_DIM 10 

int main(void) 
{ 
    int done = 0; 
    int rows, cols; 
    float *dataMatrix; 
    float *rowAveVector; 
    float *colAveVector; 
    float overallAve; 

    while (!done) 
    { 
    do 
    { 
     printf("Enter row dimension (must be between 1 and %d): ", MAX_DIM); 
     scanf("%d", &rows); 

    } while(rows <= 0 || rows > MAX_DIM); 
    do 
    { 
     printf("Enter column dimension (must be between 1 and %d): ", MAX_DIM); 
     scanf("%d", &cols); 
    } while(cols <= 0 || cols > MAX_DIM); 

    dataMatrix = readMatrix(rows, cols); 
    if (dataMatrix == NULL) 
    { 
     printf ("Program terminated due to dynamic memory allocation failure\n"); 
     return (0); 
    } 

    rowAveVector = rowAverage(dataMatrix, rows, cols); 
    colAveVector = colAverage(dataMatrix, rows, cols); 
    if(rowAveVector == NULL || colAveVector == NULL) 
    { 
     printf("malloc failed. Terminating program\n"); 
     return (0); 
    } 
} 
float *readMatrix(int rows, int cols) 
//Dynamically allocate an array to hold a matrix of floats. 
//The dimension of the matrix is numRows x numCols. 
//If the malloc fails, return NULL. 
//Otherwise, prompt the user to enter numRows*numCols values 
//for the matrix in by-row order. 
//Store the values entered by the user into the array and 
//return a pointer to the array. 

{ 

    int i=0; 
    int j=0; 
    int elements=0; 
    float *m=malloc(rows*cols*sizeof(float)); 
    if (m==NULL) 
    { 
     printf("error\n"); 
     return NULL; 
    } 
    printf("Enter values for the matrix: "); 
    for (i=0;i<rows;i++) 
    { 
     for (j=0;j<cols;j++) 
     { 
      elements = i*cols+j; 
      scanf("%f", &m[elements]); 
     } 
    } 
    return m; 
} 


float *rowavg(float *matrix, int rows, int cols) 
{ 

    int i=0; 
    float mean=0; 
    float *mat=malloc(rows*sizeof(float)); 
    if(mat==NULL) 
    { 
     printf("error\n"); 
     return NULL; 
    } 
    for (i=0;i<rows;i++) 
    { 
     readMatrix(rows,cols); 
     mean= 
     mat[i]=mean; 
    } 
} 
+0

デバッグヘルプの検索で問題が発生した場合は、その旨を説明してください。 – sjsam

+0

そして、関数から割り当てられた配列へのポインタをどこで返しますか?あなたは '平均'をどこで計算しますか? 'readMatrix'が返すデータで何かすることになっていますか? 'matrix'引数は何ですか?私にはCの基本的な理解が不足しているようで、[初心者向けの良い本が必要です](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)。 –

+0

@JoachimPileborg readMatrixは、ユーザーが行列に入力した値を保持します。行列の次元は、ユーザーに依存します。次に、2番目の関数は前の行列の各行を取り、各行の平均を求めます。各行の平均値は新しい配列に格納されます。 – nm10563

答えて

0

まず、rowavgでは必要ありませんreadMatrix(rows,cols);のコール。

そして、あなたはrowavgがあなたの行列の各行の平均値を返したい場合は、以下を試してみてください。

float *rowavg(float *matrix, int rows, int cols) 
{ 
    // check matrix befor use 
    if (matrix == NULL) 
    { 
     return NULL; 
    } 
    int i=0; 
    int j=0; 
    double mean; // variable name from original code in the question 
    float *avgarr = (float *)malloc(rows*sizeof(float)); 
    if(avgarr == NULL) 
    { 
     return NULL; 
    } 
    for (i=0;i<rows;i++) 
    { 
     mean = 0.0; 
     for (j=0;j<cols;j++) 
     { 
      mean += matrix[i*cols+j]; 
     } 
     avgarr[i] = (float)(mean/cols); 
    } 
    return avgarr; 
} 

そしてなぜならmainにあなたはすでにあなたがprintf("error\n");を使うべきではありません

rowAveVector = rowAverage(dataMatrix, rows, cols); 
    if(rowAveVector == NULL) 
    { 
     printf("malloc failed. Terminating program\n"); 
     return (0); // here consider return value different from 0 
    } 

を持っていますrowavgにあります。

割り当てられたメモリがもう不要になった後、freeを使用して考えてみましょう。

+0

そして、各列の平均値を求めたいのなら、(平均/列)を(平均/行)に切り替えるだけでしょうか? – nm10563

+0

1)メモリを 'malloc(cols * sizeof(float));'; 2) 'for(i = 0; i VolAnd

+0

大丈夫もう一つのこと。行列の要素の平均が行列によって指し示されているのを見たいと思ったら。私はちょうど(行*列)で、平均を分けるだろうか? – nm10563

関連する問題