cs1713-day0-prog2.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (C) 2020, 2019, 2018, 2017 Girish M
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. /*-----------------------------------
  20. Name: Girish M
  21. Roll number: MTech CS 13
  22. Date: 20/07/2017
  23. Program description: Calculate area of triangle using coordinates
  24. Acknowledgements: Hero's formula
  25. ------------------------------------*/
  26. #include <stdio.h>
  27. #include <math.h>
  28. float areaTriangle(float []);
  29. const unsigned length = 6;
  30. int main(void)
  31. {
  32. float cdn[length];
  33. unsigned i;
  34. printf("\nEnter 3 coordinates of triangle (x1,y1), (x2,y2) and (x3,y3)\n");
  35. for(i=0; i<length; i++)
  36. {
  37. scanf("%f", &cdn[i]);
  38. }
  39. printf("\nThe area of triangle is: %.3f\n", areaTriangle(cdn));
  40. return 0;
  41. }
  42. /*Ar. triangle = 1/2 * (x1(y2-y3)-y1(x2-x3)+(x2*y3-y2*x3))*/
  43. float areaTriangle(float cdn[])
  44. {
  45. return fabs((0.5*(cdn[0]*(cdn[3]-cdn[5])-(cdn[1]*(cdn[2]-cdn[4]))+(cdn[2]*cdn[5]-cdn[3]*cdn[4]))));
  46. }