0

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.

  • Your createMatrix function isn't allocating your matrix correctly. Refer to this answer: https://stackoverflow.com/a/917824/4081597 – Rob C May 09 '22 at 12:54
  • Also try "%.02f" when printing – Rob C May 09 '22 at 12:54
  • okay, thanks, any thoughts on the neighbor average fraction function? – Nir Ben Ishay May 09 '22 at 13:39
  • A double for loop from `i - 1` to `i + 1` with the same with `j` inside will do the trick. The only thing you have to be wary is if the coordinate are not out of bound (and count how many valid neighbor the cell have) – Tom's May 09 '22 at 14:25

0 Answers0