cs1713-day4-prog7.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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: Suppose that we choose to represent sets by arrays of non-negative integers, containing at most 100 elements each. Let A and B be 2 such sets of integers. Implement the following set operations:
  24. (a) A∪B;
  25. (b) A∩B;
  26. (c) A ⊆ B (returns 1 if A is a subset of B, 0 otherwise);
  27. (d) prints the power set of A on the screen.
  28. Acknowledgements:
  29. ------------------------------------------------------------------------------------------------------------------*/
  30. #include "common.h"
  31. char isAlreadyPresent(int a, int* arr, int size)
  32. {
  33. int i;
  34. for(i=0; i<size; i++)
  35. {
  36. if(arr[i] == a)
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. int* unionOfTwoSets(int* A, int* B, int sizeA, int sizeB)
  42. {
  43. int* C = (int*) malloc(sizeof(sizeA+sizeB));
  44. int i, j, k=0;
  45. for(i=0; i<sizeA; i++)
  46. C[i]=A[i];
  47. for(j=i; j<(sizeA+sizeB); j++)
  48. {
  49. if(isAlreadyPresent(B[k], A, sizeA))
  50. {
  51. C[j]=B[k];
  52. k++;
  53. }
  54. }
  55. return C;
  56. }
  57. int* intersectionOfTwoSets(int* A, int* B, int sizeA, int sizeB)
  58. {
  59. return 0;
  60. }
  61. int* subsetOfTwoSets(int* A, int* B, int sizeA, int sizeB)
  62. {
  63. return 0;
  64. }
  65. void powerSet(int* A, int sizeA)
  66. {
  67. }
  68. int main(int argc, char* argv[])
  69. {
  70. const int SIZE=100;
  71. int A[SIZE], B[SIZE];
  72. unsigned int i, sizeA, sizeB;
  73. if(argc == 3)
  74. {
  75. sizeA = atoi(argv[1]);
  76. sizeB = atoi(argv[2]);
  77. printf("\nEnter elements of set A:\n");
  78. for(i=0; i<sizeA; i++)
  79. {
  80. printf("\nEnter element %d: ", i+1);
  81. scanf("%d",&A[i]);
  82. }
  83. printf("\nEnter elements of set B:\n");
  84. for(i=0; i<sizeB; i++)
  85. {
  86. printf("\nEnter element %d: ", i+1);
  87. scanf("%d",&B[i]);
  88. }
  89. printf("\nA∪B: ");
  90. int* unionArray = unionOfTwoSets(A, B, sizeA, sizeB);
  91. for(i=0; i<(sizeA+sizeB); i++)
  92. printf("\nA∩B: ");
  93. printf("\nA ⊆ B: ");
  94. printf("\nPower set of A: ");
  95. printf("\nPower set of B: ");
  96. }
  97. else
  98. ERR_MESG("\nUsage: ./cs1713-day4-prog7.o sizeA sizeB\n");
  99. return 0;
  100. }