0

I was writing a code in C to find whether a given triangle is equilateral,isosceles or scalene. I have written it as :

#include<stdio.h>
#include<math.h>
int main()
{
    float x1,x2,x3,y1,y2,y3;
    float d1,d2,d3;
    printf("Enter the co-ordinates of 1st vertex: \n");
    scanf("%f%f", &x1,&y1);
    printf("Enter the co-ordinates of 2nd vertex: \n");
    scanf("%f%f", &x2,&y2);
    printf("Enter the co-ordinates of 3rd vertex: \n");
    scanf("%f%f", &x3,&y3);
    d1= sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    d2= sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
    d3= sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3));
    if((d1==d2) && (d2==d3))
        printf("Given triangle is equilateral");
    else if((d1!=d2) && (d2!=d3) && (d1!=d3)) 
        printf("Given triangle is scalene");
    else
        printf("Given triangle is isosceles but not equilateral");   

}
Now, my problem is how to take input which is an irrational number as input at runtime ? For example, if I have to take √3 as input then how to take it, Is there any way to take square-root at runtime ?
Please help.

ankit
  • 277
  • 1
  • 4
  • 25
  • 2
    So you want the user to be able to input the exact value of irrational square roots? – eesiraed Jul 28 '19 at 05:23
  • 3
    Note that you have direct floating point comparisons which [may cause problems](https://stackoverflow.com/q/588004/9254539). – eesiraed Jul 28 '19 at 05:25
  • 1
    @AlexanderZhang, No, I want to know, Is it possible to write input something like sqrt(3). We can take value of irrational number till 2 decimal places but it will not give correct output. right ? – ankit Jul 28 '19 at 05:35
  • 3
    That's what I meant. You would have to write code to detect and parse such inputs, but first you have to fix your floating point error issues – eesiraed Jul 28 '19 at 05:37
  • 1
    @n.m. yes, It is homework problem. I wanted to know whether my code is giving correct output or not. So I wanted to take some sample input like (3,0),(-3,0),(0,3√3) but I didn't know how to take it as input. – ankit Jul 28 '19 at 05:38
  • 2
    If your assignment does not explicitly require your program to accept exact irrational numbers or formulas, do not try to implement this. Such a feat would be well beyond the abilities of a beginner programmer. Let the user input an approximation like 5.19615242271. – n. m. could be an AI Jul 28 '19 at 05:46
  • And in these geometry homework problems you want all the precision you can habmve so consider using doubles – Antti Haapala -- Слава Україні Jul 28 '19 at 06:26

2 Answers2

1

A solution can be write a code to ask the user Whether he wants to input an irrational number and then process the input inside the program.

  1. Print if the nth vertex contains a irrational number
    1. Get its position
    2. Take integer input and process it as irrational entity

sqrt(integer)

inside the program.

Minato
  • 38
  • 6
  • Suppose , I have asked the user whether he wants to put number as rational or irrational then how will he put this irrational number at runtime ? – ankit Jul 28 '19 at 05:57
  • 2
    @ankit let the user input an integer value of what’s inside the square root, you take the integer value and assign it as a sqrt(integer) inside the program. Example if I want to enter an irrational number sqrt(3) then you take it as `int a` and assign it as `int b = sqrt(a)` inside the program itself. – Minato Jul 28 '19 at 06:24
  • @ankit pleasure helping you – Minato Jul 28 '19 at 06:43
0

Let the user input an integer value of what’s inside the square root. You take the integer value and assign it as sqrt( integer ) inside the program.

Example ::

If I want to input an irrational number sqrt(3) Then you take it as int a an assign it as int b = sqrt( a ) inside the program itself.

Minato
  • 38
  • 6