From 1b9f5bbaf8f7c6bb36eb0d6493e8a6940a96ebc5 Mon Sep 17 00:00:00 2001 From: Frederik Beimgraben Date: Sat, 11 Jan 2025 16:00:44 +0100 Subject: [PATCH] fix: Make already existent functions compliant with the task. --- MMS24_25.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 11 deletions(-) diff --git a/MMS24_25.c b/MMS24_25.c index 2a048a4..4c339b4 100644 --- a/MMS24_25.c +++ b/MMS24_25.c @@ -1,34 +1,104 @@ -// -// Created by minhd on 03.12.2024. -// +/* =========================STYLE-GUIDELINES=========================== + * All comments use MD-Syntax; + * Formulas are written in KaTeX/TeX Syntax. + * We adhere by the `kernel.org` style-guidelines. + * See: https://www.kernel.org/doc/html/v4.10/process/coding-style.html + * =============================AUTHORS================================ + * Created by Frederik Beimgraben, Minh Dan Cam and Lea Hornberger in + * the winter term of 2024/25 at the faculty of computer science at + * Reutlingen University. + * After grading the full source code will be available at: + * https://git.beimgraben.net/MMS-2024-25/MMS-Loesung-2024-25.git + */ #include "MMS24_25.h" #include "math.h" #include #include +#include "MMS24-25.h" +#include + +// NULL Pointer +#ifndef NULL +#define NULL 0 +#endif + +double interpolateLine( + double dX1, + double dY1, + double dX2, + double dY2, + double dXq + ) { + double y = ( + (dY2 - dY1) / (dY2 - dX1) + ) * dXq - ( + (dY2 - dY1) / (dX2 - dX1) + ) * dX1 + dY1; + + #ifdef _DEBUG_ + // DEBUG: print the parameters and result + printf("interpolateLine(%lf, %lf, %lf, %lf, %lf) -> %lf\n", + dX1,dY1,dX2,dY2,dXq,y); + #endif -double interpolateLine(double dX1, double dY1,double dX2, double dY2, double dXq) { - double y = ((dY2 - dY1) / (dY2 - dX1)) * dXq + dY1 - ((dY2 - dY1) / (dX2 - dX1)) * dX1; return y; } -void rescaleDoubleArray(double *pdIn, int iLen, double dMin, double dFactor, double *pdOut) { - +void rescaleDoubleArray( + double *pdIn, + int iLen, + double dMin, + double dFactor, + double *pdOut + ) { + /* Rescale an array of double values, so that the following rules apply: + * Given $ |pdIn| = |pdOut| = iLen $ it should be, that: + * $ min(pdIn) > dMin $, as well as + * \[ \forall i, j \in (0,\dots,iLen-1): + * (pdOut_i - pdOut_j) = (pdIn_i - pdIn_j) * dFactor \], + * which naturally results from: + * $ pdOut_i = pdIn_i * dFactor + dMin $ + */ + for (int i = 0; i < iLen; i++) { + pdOut[i] = pdIn[i] * dFactor + dMin; + } } -double *generateSineValues(int iNumValues, int iNumSamplesPerPeriod, double dAmp) { - double* sineArray = (double*)malloc(iNumValues * sizeof(double)); +double *generateSineValues( + int iNumValues, + int iNumSamplesPerPeriod, + double dAmp + ) { + /* Generate an array of `iNumValues` double value, so that the values + * represent the Y-coordinates of a sine curve of which one full period + * occupies `iNumSamplesPerPeriod` values + */ + + // Initialize the output array + double* sineArray = NULL; + sineArray = (double*) malloc(sizeof(double) * iNumValues); + + if (sineArray == NULL) { + printf("Failed to allocate memory for SineArray\n"); + exit(-1); + } + for (int i=0; i