cs1713-day2-prog2Inefficient.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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: 25 July 2017
  23. Program description: Write a program to find the highest and second highest value in an array of n integers using less than 2n − 3 comparisons.
  24. Acknowledgements:
  25. ------------------------------------*/
  26. #include <stdio.h>
  27. int main()
  28. {
  29. int a[]={3, 2, 56, 2, 8, 5, 3, 2, 0, 4, 1, 43, 21};
  30. int i=0, max, loc;
  31. int secondMax;
  32. /* typedef struct loserList{
  33. } *l;*/
  34. max = a[0];
  35. while( i != 13 )
  36. {
  37. if(a[i] >= max)
  38. {
  39. max = a[i];
  40. loc = i;
  41. }
  42. i++;
  43. }
  44. printf("\nMax element: %d\n", max);
  45. a[loc] = -1;
  46. i = 0;
  47. secondMax = a[0];
  48. while( i != 12 )
  49. {
  50. if(a[i] != -1 && a[i] >= secondMax)
  51. {
  52. secondMax = a[i];
  53. }
  54. i++;
  55. }
  56. printf("\n2nd Max element: %d\n", secondMax);
  57. return 0;
  58. }