Untitled
unknown
plain_text
2 years ago
5.7 kB
10
Indexable
// Course: Advanced C programming
// exercise 2, question 1
// file name: ex2_q1.c
// --------------------------- //
//
// Assigned by:
// Student1_Full_Name #ID
// Student2_Full_Name #ID
//
// --------------------------- //
// --------------------------------------- //
// Include and definition package section:
// --------------------------------------- //
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define scanf_s scanf
#define ROWS 4
#define COLS 4
// --------------------------------------- //
// Types declration section:
// --------------------------------------- //
typedef struct fraction
{
int num, numerator, denominator;
} fraction;
// --------------------------------------- //
// Functions declration section:
// --------------------------------------- //
fraction** createMatrix(int rows, int cols);
fraction** matrixAverageNeighbor(int A[][COLS], int rows, int cols);
fraction neighborFractionAverage(int A[][COLS], int i, int j, int rows, int cols);
void printMatrix(fraction** B, int rows, int cols);
void freeMatrix(fraction** B, int rows);
fraction doubleToFraction(double num);
// --------------------------------------- //
// Main section:
// --------------------------------------- //
int main()
{
fraction** B;
int A[ROWS][COLS] = {
{5, 12, 6, 8},
{4, 7, 0, 9},
{13, 20, 8, 2},
{18, 0, 2, 6}
};
// Start Program:
printf("Start Program\n");
// call functions:
B = matrixAverageNeighbor(A, ROWS, COLS);
// write output:
printf("Output:\n");
printMatrix(B, ROWS, COLS);
// free matrix:
freeMatrix(B, ROWS);
return 0;
}
// --------------------------- //
/// <summary>
/// This code required one extra important function.
/// Think hard about what it should be.
/// </summary>
/// <params>You decide</params>
/// <returns>You decide</returns>
fraction doubleToFraction(double num)
{
fraction result;
int currnum = (int)num, gcd;
double denominator = num - currnum;
int a = 10, b = (denominator * 10), temp;
while (b != 0)
{
temp = b;
b = a % b;
a = temp;
}
gcd = a;
result.num = currnum;
result.numerator = (denominator * 10) / gcd;
result.denominator = 10 / gcd;
return result;
}
//}
/// <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) //3 4
{
int i;
fraction** mat = NULL;
mat = (fraction**)calloc(rows, sizeof(fraction*));
for (i = 0; i < rows; i++)
{
mat[i] = (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)
{
fraction** B = createMatrix(rows, cols);
int i, j;
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)
{
int sum = 0, count = 0, r, c;
double resultdouble;
fraction result;
for (r = i - 1; r <= i + 1; r++) {
for (c = j - 1; c <= j + 1; c++) {
if (r >= 0 && r < rows && c >= 0 && c < cols && !(r == i && c == j)) {
sum += A[r][c];
count++;
}
}
}
resultdouble = (double)sum / count;
result = doubleToFraction(resultdouble);
return result;
}
// --------------------------- //
/// <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++)
{
(double)B[i][j].numerator / B[i][j].denominator;
printf("%d %d/%-3d ", B[i][j].num,B[i][j].numerator,B[i][j].denominator);
}
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]);
}
free(B);
}
Editor is loading...