cs1713-day4-prog1.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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: cs1713
  22. Date: 3 August 2017
  23. Program description: Define a structure to represent complex numbers.
  24. Using this definition, write a function that takes as input 3 real numbers,
  25. a, b, and c, and returns the two roots of the quadratic equation ax2 +bx+c=0.
  26. Acknowledgements:
  27. ---------------------------------------------------------------------------*/
  28. #include "common.h"
  29. typedef struct complexNum
  30. {
  31. float rez;
  32. float imz;
  33. }complexNum;
  34. complexNum* rootOfQuadratic(float a, float b, float c)
  35. {
  36. //printf("\nEntered values of a,b and c are %f %f and %f", a, b, c);
  37. double d;
  38. complexNum* compNum;
  39. compNum = (complexNum*)malloc(sizeof(complexNum)*2);
  40. if(a != 0)
  41. {
  42. d = (b*b)-(4*a*c);
  43. printf("\nValue of discriminant: %.2lf\n", d);
  44. if(d < 0)
  45. {
  46. /*Imaginary roots*/
  47. printf("\nComplex roots exist in conjugate pairs.\n");
  48. compNum[0].rez = ((-1)*b/(2*a));
  49. compNum[1].rez = compNum[0].rez;
  50. compNum[0].imz = d/(2*a);
  51. compNum[1].imz = compNum[0].imz;
  52. }
  53. if(d == 0)
  54. {
  55. /*Real and equal roots*/
  56. printf("\nReal and equal roots exist.\n");
  57. double s = sqrt(d);
  58. compNum[0].rez = ((-1)*b+s)/(2*a);
  59. compNum[1].rez = compNum[0].rez;
  60. compNum[0].imz = 0;
  61. compNum[1].imz = 0;
  62. }
  63. else if(d > 0)
  64. {
  65. /*Real and non-equal roots*/
  66. printf("\nReal and non equal roots exist.\n");
  67. double s = sqrt(d);
  68. compNum[0].rez = ((-1)*b+s)/(2*a);
  69. compNum[1].rez = ((-1)*b-s)/(2*a);
  70. compNum[0].imz = 0;
  71. compNum[1].imz = 0;
  72. }
  73. return compNum;
  74. }
  75. else
  76. return 0;
  77. }
  78. int main(int argc, char* argv[])
  79. {
  80. complexNum* compNum;
  81. if(argc == 4)
  82. {
  83. float a = atof(argv[1]);
  84. float b = atof(argv[2]);
  85. float c = atof(argv[3]);
  86. compNum = rootOfQuadratic(a, b, c);
  87. if(compNum != 0)
  88. printf("\nRoots of %.2fx^2 + %.2fx + %.2f is\n %.2f+i(%.2f) & %.2f-i(%.2f)\n", a, b, c, compNum[0].rez, compNum[0].imz, compNum[1].rez, compNum[1].imz);
  89. else
  90. printf("\nNot a quadratic equation\n");
  91. }
  92. else
  93. {
  94. printf("\nUsage: ./cs1713-day4-prog1.o a b c\n");
  95. return -1;
  96. }
  97. return 0;
  98. }