cs1713-day2-prog2.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include <stdio.h>
  20. #include <regex.h>
  21. #include <stdlib.h>
  22. #include <malloc.h>
  23. int match(const char *string, char *pattern)
  24. {
  25. int status;
  26. regex_t re;
  27. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
  28. return(0); /* report error */
  29. }
  30. status = regexec(&re, string, (size_t) 0, NULL, 0);
  31. regfree(&re);
  32. if (status != 0) {
  33. return(0); /* report error */
  34. }
  35. return(1);
  36. }
  37. typedef struct
  38. {
  39. int value;
  40. int loser[200];
  41. int numLoser;
  42. }loserList;
  43. int main(int argc, char* argv[])
  44. {
  45. int i = 1, j = 0, k=0;
  46. char* pattern = "^[+]?[0-9]+$";
  47. if(argc > 1)
  48. {
  49. loserList* l;
  50. l = (loserList*) malloc(sizeof(loserList)*(argc-1));
  51. while(argv[i] != NULL)
  52. {
  53. if(match(argv[i],pattern))
  54. {
  55. l[k].value = atoi(argv[i]);
  56. //l[k].loser = (int*)malloc(sizeof(int)*(argc-1));
  57. l[k].numLoser = 0;
  58. }
  59. else
  60. {
  61. printf("\n%s is not a valid number. Will be considered as zero.\n", argv[i]);
  62. }
  63. i++;
  64. k++;
  65. }
  66. /*printf("\nEntered numbers are:\n");
  67. k=0;
  68. while(j != (argc-1))
  69. {
  70. printf("%d ", l[j].value);
  71. j++;
  72. }*/
  73. int indexOfMax=0, m;
  74. int max = l[indexOfMax].value;
  75. k=0;
  76. while(j != (argc-1))
  77. {
  78. if(l[j].value >= max)
  79. {
  80. max = l[j].value;
  81. indexOfMax = j;
  82. k=0;
  83. }
  84. else
  85. {
  86. if(indexOfMax <= j)
  87. {
  88. //check for all other elements
  89. for(m=0;m<(argc-1); m++)
  90. {
  91. if((l[indexOfMax].value > l[m].value) && ((m < indexOfMax)))
  92. {
  93. l[indexOfMax].numLoser++;
  94. l[indexOfMax].loser[k] = l[m].value;
  95. k++;
  96. }
  97. }
  98. }
  99. }
  100. j++;
  101. }
  102. printf("\nMax element is %d\n", l[indexOfMax].value);
  103. printf("\nIndex of max: %d\n", indexOfMax);
  104. //second max element
  105. // traverse the losers list of max element
  106. printf("\nSize of losers list %d\n",l[indexOfMax].numLoser);
  107. printf("\nLosers list of max element\n");
  108. for(i=0; i<k ; i++)
  109. {
  110. printf(" %d ", l[indexOfMax].loser[i]);
  111. }
  112. printf("\n");
  113. }
  114. else
  115. printf("\nUsage: ./cs1713-day2-prog2.o ele1 ele2 ...\n");
  116. return 0;
  117. }