Vorrei scoprire gentilmente il motivo del perché, il valore dell'elemento <coefs[0]>, nel file <mathematic.c> ad un certo punto diventa zero (al momento del calcolo di <root[0]>).
I coefficienti vengono correttamente passati alla funzione e coefs[1] e coefs[2] mantengono il loro valore nell'espressione di roots[0].
Perché <coefs[0]> diventa zero? :-[
Obiettivo dell'esercizio: passare i coefficienti di una equazione di secondo grado ad una funzione che memorizzerà in un array di tre elementi <root[0], root[1], root[2]> (i valori <discriminante,radice1,radice2>), restituendo i risultati.
Grazie per il supporto.
Emanuele.
===================================
Makefile
===================================
Codice: Seleziona tutto
# author : E. M. Latorre
# start encode : 06.10.2020
# last encode : 06.16.2020
# Variables.
CFLAGS=-ansi -Wpedantic
TARGET=equation.x
COMPILER=gcc
OBJECTS=equation.o lib/mathematic.o
all: ${TARGET}
equation.x: equation.o lib/mathematic.o
${COMPILER} ${CFLAGS} -o ${TARGET} ${OBJECTS} -lm;
equation.o: equation.c
${COMPILER} ${CFLAGS} -c equation.c
lib/mathematic.o: lib/mathematic.c
${COMPILER} ${CFLAGS} -c lib/mathematic.c -o lib/mathematic.o
.PHONY: clean
clean:
-rm ${TARGET} *.o *~ */*.o;
clear;
ls -l --color;
equation.c
===================================
Codice: Seleziona tutto
/*
* author : E. M. Latorre
* start encode : 06.15.2020
* last encode : 06.16.2020
* name : equation.c
* target : Compute the roots of a second degree equation.
*/
/* List of headers.*/
#include<stdio.h>
#include<stdlib.h>
int main (void)
{
/* Variables. */
double coef[2],root[2];
/* Functions. */
extern double degree2 (double coef[2],double root[2]);
/* Set initials conditions values. */
coef[0]=coef[1]=coef[2]=0.;
root[0]=root[1]=root[2]=0.;
/* Input coefficients. */
printf("Input coefficients of equation ax^2+bx+c:\n");
printf("a: ");
scanf("%lf",&coef[0],"\n");
if (coef[0] == 0)
{
printf("Attention! [a] coefficient must be different from 0.");
exit(0);
}
printf("b: ");
scanf("%lf",&coef[1],"\n");
printf("c: ");
scanf("%lf",&coef[2],"\n");
/* Call [degree2] function and calculate the roots of a second degree equation. */
degree2(coef,root);
/* Print roots.*/
if (root[0] > 0.)
{
printf("Discriminat positive: 2 distinct real solutions.\n");
printf("Roots: %lf e %lf. \n",root[1],root[2]);
}
else if (root[0] == 0.)
{
printf("Discriminat null: 2 equal real solutions.\n");
printf("Roots: %lf. \n",root[1]);
}
else if (root[0] < 0.)
{
printf("Discriminant negative: no solution in R number set.\n");
}
exit(0);
}
==================
mathematic.c (file esterno)
==================
Codice: Seleziona tutto
/* ===================================================================================== */
/*
* author : E. M. Latorre
* start encode : 06.15.2020
* last encode : 06.15.2020
* name : degree2
* target : Compute the solutions of a second degree equation.
*/
double degree2 (double coefs[],double roots[])
{
/* Test section: values from [main].*/
printf("Coefficients: a=%lf\tb=%lf\tc=%lf.\n",coefs[0],coefs[1],coefs[2]);
/* Variables. */
/* Set default values. */
roots[0]=roots[1]=roots[2]=0.;
/* Test section: values from [default].*/
printf("Array [root]: Delta=%lf\tRoot 1=%lf\tRoot 2=%lf.\n",roots[0],roots[1],roots[2]);
/* Calculate delta.*/
roots[0]=pow(coefs[1],2)-(4.*(coefs[0]*coefs[2]));
/* Find roots.*/
if (roots[0] > 0.)
{
roots[1]=(-coefs[1]-sqrt(roots[0]))/(2.*coefs[0]);
roots[2]=(-coefs[1]+sqrt(roots[0]))/(2.*coefs[0]);
}
else if (roots[0] == 0.)
{
roots[1]=-coefs[1]/(2.*coefs[0]);
}
else if (roots[0] < 0.)
{
}
}