cs1713-day2-prog1b.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 whether the multisets corresponding to A and B are equal.
  24. Acknowledgements:
  25. ------------------------------------*/
  26. #include <stdio.h>
  27. #include <malloc.h>
  28. int max(int arr[], int len)
  29. {
  30. int i, max;
  31. max = arr[0];
  32. for(i=0; i<len; i++)
  33. {
  34. if(arr[i] >= len)
  35. max = arr[i];
  36. }
  37. return max;
  38. }
  39. int main()
  40. {
  41. int A[]={1, 2, 2};
  42. int B[]={1, 2, 2};
  43. int mulSet=0;
  44. int i, j, k;
  45. int maxA, maxB;
  46. maxA = max(A, 5);
  47. maxB = max(B, 3);
  48. if(maxA != maxB)
  49. {
  50. printf("\nNo. of multisets of A and B are not equal.\n");
  51. }
  52. else
  53. {
  54. int* countA = (int*) malloc(sizeof(int)*maxA);
  55. int* countB = (int*) malloc(sizeof(int)*maxB);
  56. for(i=0; i<maxA; i++)
  57. {
  58. countA[i] = 0;
  59. countB[i] = 0;
  60. }
  61. //calc set A elements frequency
  62. for(i=0; i<5; i++)
  63. {
  64. countA[A[i]-1]++;
  65. }
  66. //calc set B elements frequency
  67. for(j=0; j<3; j++)
  68. {
  69. countB[B[j]-1]++;
  70. }
  71. for(k=0; k<maxA; k++)
  72. {
  73. if(countA[k] == countB[k])
  74. {
  75. mulSet++;
  76. }
  77. else
  78. {
  79. printf("\nNo. of multisets of A and B are not equal.\n");
  80. break;
  81. }
  82. }
  83. free(countA);
  84. free(countB);
  85. }
  86. return 0;
  87. }