C在动态矩阵中找到单元格的平均值
我需要编写接收矩阵的功能,该矩阵应用于数组 静态2D和尺寸。该功能必须分配一个新的二维数组-A矩阵B 它具有分数类型,并且包含与矩阵A相同的大小。
typedef struct fraction
{
int num, numerator, denominator;
} fraction;
这是我到目前为止写的关于实现的解释的
/// <summary>
/// This code required one extra important function.
/// </summary>
/// <params>You decide</params>
/// <returns>You decide</returns>
// your code:
// --------------------------- //
/// <summary>
/// This function allocate a dynamic matrix from type fraction.
/// </summary>
/// <param>int rows - the number of rows in the matrix</param>
/// <param>int cols - the number of colums in the matrix</param>
/// <returns>allocated empty matrix B from type fraction</returns>
fraction** createMatrix(int rows, int cols)
{
int i;
fraction** mat = (fraction**)calloc(rows, sizeof(fraction));
for (i = 0; i < rows; i++)
{
*mat = (fraction*)calloc(cols, sizeof(fraction));
}
return mat;
}
// --------------------------- //
/// <summary>
/// The function receives a static matrix
/// and for each cell in the matrix calculates
/// the average of its neighbors.
/// </summary>
/// <param>int A[][COLS] - the static matrix</param>
/// <param>int rows - the number of rows in the matrix</param>
/// <param>int cols - the number of colums in the matrix</param>
/// <returns>matrix B from type fraction</returns>
fraction** matrixAverageNeighbor(int A[][COLS], int rows, int cols)
{
// your code:
int i, j;
fraction** B = createMatrix(rows, cols);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
B[i][j] = neighborFractionAverage(A, i, j, rows, cols);
}
}
return B;
}
// --------------------------- //
/// <summary>
/// The function receives a static matrix, and a cell value,
/// and calculates the average of its neighbors.
/// </summary>
/// <param>int A[][COLS] - the static matrix</param>
/// <param>int i - the cell row number in matrix</param>
/// <param>int j - the cell colum number in the matrix</param>
/// <param>int rows - the number of rows in the matrix</param>
/// <param>int cols - the number of colums in the matrix</param>
/// <returns>value from type fraction</returns>
fraction neighborFractionAverage(int A[][COLS], int i, int j, int rows, int cols)
{
// ???
}
// --------------------------- //
/// <summary>
/// The function receives a dynamic matrix from type fraction,
/// and print the matrix as double varibles.
/// </summary>
/// <param>fraction** B - the dynamic matrix</param>
/// <param>int rows - the number of rows in the matrix</param>
/// <param>int cols - the number of colums in the matrix</param>
/// <returns>None</returns>
void printMatrix(fraction** B, int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%2f ", B[i][j]);
}
printf("\n");
}
printf("\n");
}
// --------------------------- //
/// <summary>
/// The function receives a dynamic matrix from type fraction,
/// and free all allocated memory.
/// </summary>
/// <param>fraction** B - the dynamic matrix</param>
/// <param>int rows - the number of rows in the matrix</param>
/// <returns>None</returns>
void freeMatrix(fraction** B, int rows)
{
int i;
for (i = 0; i < rows; i++)
free(B[i]);
}
内容 重点。例如 - 输入:
5 12 6 8
4 7 0 9
13 20 8 2
18 0 2 6
应该输出:
7.66 4.40 7.20 5.00
11.40 8.50 9.00 4.80
9.80 6.50 5.75 5.00
11.00 12.20 7.20 4.00
我真的在这里迷路了。我已经想到了很多。我认为使用一个额外的功能,该功能将计算一个单元格的邻居,但设法失败了。
I need to write functions which receives a matrix applied as an array
static 2D and sizes. The function must assign a new two-dimensional array - a matrix B
which has a Fraction type and contains the same sizes as the matrix A.
typedef struct fraction
{
int num, numerator, denominator;
} fraction;
This is what I have written so far with explanations about the implementations
/// <summary>
/// This code required one extra important function.
/// </summary>
/// <params>You decide</params>
/// <returns>You decide</returns>
// your code:
// --------------------------- //
/// <summary>
/// This function allocate a dynamic matrix from type fraction.
/// </summary>
/// <param>int rows - the number of rows in the matrix</param>
/// <param>int cols - the number of colums in the matrix</param>
/// <returns>allocated empty matrix B from type fraction</returns>
fraction** createMatrix(int rows, int cols)
{
int i;
fraction** mat = (fraction**)calloc(rows, sizeof(fraction));
for (i = 0; i < rows; i++)
{
*mat = (fraction*)calloc(cols, sizeof(fraction));
}
return mat;
}
// --------------------------- //
/// <summary>
/// The function receives a static matrix
/// and for each cell in the matrix calculates
/// the average of its neighbors.
/// </summary>
/// <param>int A[][COLS] - the static matrix</param>
/// <param>int rows - the number of rows in the matrix</param>
/// <param>int cols - the number of colums in the matrix</param>
/// <returns>matrix B from type fraction</returns>
fraction** matrixAverageNeighbor(int A[][COLS], int rows, int cols)
{
// your code:
int i, j;
fraction** B = createMatrix(rows, cols);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
B[i][j] = neighborFractionAverage(A, i, j, rows, cols);
}
}
return B;
}
// --------------------------- //
/// <summary>
/// The function receives a static matrix, and a cell value,
/// and calculates the average of its neighbors.
/// </summary>
/// <param>int A[][COLS] - the static matrix</param>
/// <param>int i - the cell row number in matrix</param>
/// <param>int j - the cell colum number in the matrix</param>
/// <param>int rows - the number of rows in the matrix</param>
/// <param>int cols - the number of colums in the matrix</param>
/// <returns>value from type fraction</returns>
fraction neighborFractionAverage(int A[][COLS], int i, int j, int rows, int cols)
{
// ???
}
// --------------------------- //
/// <summary>
/// The function receives a dynamic matrix from type fraction,
/// and print the matrix as double varibles.
/// </summary>
/// <param>fraction** B - the dynamic matrix</param>
/// <param>int rows - the number of rows in the matrix</param>
/// <param>int cols - the number of colums in the matrix</param>
/// <returns>None</returns>
void printMatrix(fraction** B, int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%2f ", B[i][j]);
}
printf("\n");
}
printf("\n");
}
// --------------------------- //
/// <summary>
/// The function receives a dynamic matrix from type fraction,
/// and free all allocated memory.
/// </summary>
/// <param>fraction** B - the dynamic matrix</param>
/// <param>int rows - the number of rows in the matrix</param>
/// <returns>None</returns>
void freeMatrix(fraction** B, int rows)
{
int i;
for (i = 0; i < rows; i++)
free(B[i]);
}
The function for printing matrix B should be written according to a double type representation, with 2 digits after
the point. For example - input:
5 12 6 8
4 7 0 9
13 20 8 2
18 0 2 6
should output:
7.66 4.40 7.20 5.00
11.40 8.50 9.00 4.80
9.80 6.50 5.75 5.00
11.00 12.20 7.20 4.00
I am really lost here. I have thought alot and tried. I thought using an extra function that will calculate how many neighbors a cell has, but managed to fail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论