私の質問は、だから私は動的に行要素と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;
}
}
デバッグヘルプの検索で問題が発生した場合は、その旨を説明してください。 – sjsam
そして、関数から割り当てられた配列へのポインタをどこで返しますか?あなたは '平均'をどこで計算しますか? 'readMatrix'が返すデータで何かすることになっていますか? 'matrix'引数は何ですか?私にはCの基本的な理解が不足しているようで、[初心者向けの良い本が必要です](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)。 –
@JoachimPileborg readMatrixは、ユーザーが行列に入力した値を保持します。行列の次元は、ユーザーに依存します。次に、2番目の関数は前の行列の各行を取り、各行の平均を求めます。各行の平均値は新しい配列に格納されます。 – nm10563