#include<stdio.h> #include<math.h> void retta(double x1,double y1,double x2,double…
- User
#include<stdio.h> #include<math.h>
void retta(double x1,double y1,double x2,double y2,double *m,double *q) {
if(x1==x2) { *m=INFINITY; *q=x1; } else { *m=(y2-y1)/(x2-x1); *q=-(*m)*x1+y1; }
}
int main() {
double x1,y1,x2,y2; double m,q;
printf("inserisci le coordinate di un punto:\n"); scanf("%lf%lf",&x1,&y1);
printf("inserisci le coordinate di un secondo punto:\n"); scanf("%lf%lf",&x2,&y2);
retta(x1,y1,x2,y2,&m,&q);
printf("l'equazione della retta per i due punti dati e':\n");
if(m==INFINITY) { printf("x=%g\n",q); } else if(m==0) { printf("y=%g\n",q); } else { printf("y=%gx+%g\n",m,q); } } perche esce cosi inserisci le coordinate di un punto: 1 3 inserisci le coordinate di un secondo punto: 1 5 l'equazione della retta per i due punti dati e': x=1 PS C:\Users\massi\Desktop\PROGRAMMI C VS CODE DEF\ESERCIZI NELLE DISPENSE E\E9\Esercizio 3\output> cd 'c:\Users\massi\Desktop\PROGRAMMI C VS CODE DEF\ESERCIZI NELLE DISPENSE E\E9\Esercizio 3\output' PS C:\Users\massi\Desktop\PROGRAMMI C VS CODE DEF\ESERCIZI NELLE DISPENSE E\E9\Esercizio 3\output> & .'Esercizio 3.exe' inserisci le coordinate di un punto: 2 3 inserisci le coordinate di un secondo punto: 5 11 l'equazione della retta per i due punti dati e': y=2.66667x+-2.33333 con quei 2 sefgni
- Assistant
The output you're seeing for the equation of the line is expected based on the inputs you provided and the logic of your code. Let's break it down:
-
Vertical Line Handling:
When the x-coordinates of both points are the same (x1 == x2), your code correctly identifies this as a vertical line and assignsINFINITYto the slopem. The output correctly reflects this with the equationx=q, whereqis actually just the x-coordinate of the two points (e.g., if you input two points with the same x-coordinate1, the line equation will bex=1). -
: In the other scenario you presented, you provided the points (2, 3) and (5, 11). The slope ( m = \frac{y2 - y1}{x2 - x1} = \frac{11 - 3}{5 - 2} = \frac{8}{3} \approx 2.66667 ), which is correctly computed by the program. The intercept ( q ) is calculated as ( y1 - m \cdot x1 = 3 - \left(\frac{8}{3}\right) \cdot 2 = -\frac{7}{3} \approx -2.33333 ).
-
- User
italiano
- Assistant
Il comportamento del tuo programma è corretto rispetto ai dati forniti e alla logica implementata. Analizziamo i dettagli:
-
Gestione della Linea Verticale:
Quando le coordinate x di entrambi i punti sono uguali (x1 == x2), il tuo codice identifica correttamente questa situazione come una linea verticale e assegnaINFINITYalla pendenzam. L'output riflette correttamente questa condizione con l'equazionex=q, doveqè semplicemente la coordinata x dei due punti (ad esempio, se si inseriscono due punti con x-coordinate uguali a1, l'equazione saràx=1).
-