cs1713-day4-prog6.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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: 1 August 2017
  23. Program description: Write a function uniquify that
  24. (a) takes two arguments: an array 'a' containing at most 100 non-negative integers, and 'n', the number of integer
  25. contained in the array, and
  26. (b) stores in 'a' a list of the distinct integers contained in 'a' if the integers in 'a' are sorted in increasing order, and returns the number of distinct integers in 'a';
  27. (c) returns -1 otherwise.
  28. Acknowledgements:
  29. ------------------------------------------------------------------------------------------------------------------*/
  30. #include "common.h"
  31. #include <regex.h>
  32. int match(const char *string, char *pattern)
  33. {
  34. int status;
  35. regex_t re;
  36. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
  37. return(0); /* report error */
  38. }
  39. status = regexec(&re, string, (size_t) 0, NULL, 0);
  40. regfree(&re);
  41. if (status != 0) {
  42. return(0); /* report error */
  43. }
  44. return(1);
  45. }
  46. int uniquify(int *a, int n)
  47. {
  48. int i;
  49. int no_distinct_int = 1;
  50. /*printf("\nNo. of non-negative integers: %d, starting with %d and ending with %d\n",n, a[1], a[n-1]);*/
  51. for(i=1; i<n-1; i++)
  52. {
  53. if(a[i] != a[i+1])
  54. {
  55. no_distinct_int++;
  56. /*printf(" %d %d : %d ",a[i], a[i+1], n);*/
  57. }
  58. }
  59. if(no_distinct_int > 0)
  60. return no_distinct_int;
  61. else if(no_distinct_int == 1)
  62. return 1;
  63. else
  64. return -1;
  65. }
  66. int main(int argc, char* argv[])
  67. {
  68. const int arr_size = 100; /*at most 100 elements*/
  69. int arr[arr_size], i=1;
  70. int retVal, num1, num2;
  71. char isInIncreasingOrder = 1;
  72. char* pattern = "^[+]?[0-9]+$";
  73. if((argc > 1) && (argc < arr_size+1))
  74. {
  75. /*check if args in increasing order*/
  76. while(i != (argc-1))
  77. {
  78. if(match(argv[i],pattern))
  79. {
  80. if(match(argv[i+1],pattern))
  81. {
  82. num1 = atoi(argv[i]);
  83. num2 = atoi(argv[i+1]);
  84. if(num1 > num2)
  85. {
  86. isInIncreasingOrder = 0;
  87. }
  88. else
  89. {
  90. arr[i] = num1;
  91. arr[i+1] = num2;
  92. }
  93. }
  94. else
  95. printf("\n%s is not a valid number. Will be considered as zero.\n", argv[i+1]);
  96. }
  97. else
  98. {
  99. printf("\n%s is not a valid number. Will be considered as zero.\n", argv[i]);
  100. }
  101. i++;
  102. }
  103. if(isInIncreasingOrder)
  104. {
  105. /*printf("\nFollowing numbers are in increasing order:\n");
  106. for(i=1; i<argc; i++)
  107. printf(" %d ", arr[i]);*/
  108. retVal = uniquify(arr, argc);
  109. if(retVal != -1)
  110. {
  111. printf("\nNo. of distinct integers %d\n", retVal);
  112. }
  113. else
  114. printf("\nNo distinct integers\n");
  115. }
  116. else
  117. printf("\nPositive integer arguments not in increasing order.\n");
  118. }
  119. else
  120. printf("\nUsage: ./cs1713-day3-prog1.o positiveInt1 ... positiveInt100\n");
  121. return 0;
  122. }