0% found this document useful (0 votes)
12 views7 pages

Set Operations and Function Composition

Uploaded by

Kathan Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Set Operations and Function Composition

Uploaded by

Kathan Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LOKMANYA INSTITUTE OF

MANAGEMENT AND COMPUTER


APPLICATION
COLLEGE CODE: 318 (GTU)

SEMESTER– I

Subject Name:- Basic Maths


Subject Code:-

Date:- _________________

Name :- ___________________________________________

Div:-_______ Roll no:-________

Students Sign Examiner Signature


Project 1: Set Operations
• Description: Create a program to perform various set operations (union,
intersection, difference, complement) on sets represented as arrays.
• Key Concepts: Arrays, functions, set operations.

Input:

#include <stdio.h>
#include <stdlib.h>

// Function to find union of two sets


void union_sets(int set1[], int size1, int set2[], int size2) {
int *union_set = (int *)malloc((size1 + size2) * sizeof(int));
int k = 0;

// Add all elements of set1 to the union


for (int i = 0; i < size1; i++) {
union_set[k++] = set1[i];
}

// Add elements of set2 if they are not already in union_set


for (int i = 0; i < size2; i++) {
int found = 0;
for (int j = 0; j < size1; j++) {
if (set2[i] == set1[j]) {
found = 1;
break;
}
}
if (!found) {
union_set[k++] = set2[i];
}
}

2
printf("Union: ");
for (int i = 0; i < k; i++) {
printf("%d ", union_set[i]);
}
printf("\n");

free(union_set); // Free dynamically allocated memory


}
// Function to find intersection of two sets
void intersection_sets(int set1[], int size1, int set2[], int size2) {
printf("Intersection: ");
for (int i = 0; i < size1; i++) {
for (int j = 0; j < size2; j++) {
if (set1[i] == set2[j]) {
printf("%d ", set1[i]);
break;
}
}
}
printf("\n");
}

// Function to find difference of two sets (set1 - set2)


void difference_sets(int set1[], int size1, int set2[], int size2) {
printf("Difference (Set1 - Set2): ");
for (int i = 0; i < size1; i++) {
int found = 0;
for (int j = 0; j < size2; j++) {
if (set1[i] == set2[j]) {
found = 1;
break;
}
}

3
if (!found) {
printf("%d ", set1[i]);
}
}
printf("\n");
}

// Function to find complement of a set


void complement_set(int universal_set[], int u_size, int subset[], int s_size) {
printf("Complement: ");
for (int i = 0; i < u_size; i++) {
int found = 0;
for (int j = 0; j < s_size; j++) {
if (universal_set[i] == subset[j]) {
found = 1;
break;
}
}
if (!found) {
printf("%d ", universal_set[i]);
}
}
printf("\n");
}

int main() {
int universal_set[] = {1, 12, 23, 54, 21, 47, 78, 75, 98, 101};
int set1[] = {1, 12, 23, 47, 21, 98};
int set2[] = {21, 54, 78, 75, 101, 47};

int u_size = sizeof(universal_set) / sizeof(universal_set[0]);


int size1 = sizeof(set1) / sizeof(set1[0]);
int size2 = sizeof(set2) / sizeof(set2[0]);

4
printf("Set1: ");
for (int i = 0; i < size1; i++) {
printf("%d ", set1[i]);
}
printf("\nSet2: ");
for (int i = 0; i < size2; i++) {
printf("%d ", set2[i]);
}
printf("\n----------------------\n");

union_sets(set1, size1, set2, size2);


intersection_sets(set1, size1, set2, size2);
difference_sets(set1, size1, set2, size2);
complement_set(universal_set, u_size, set1, size1);

return 0;
}

Output:
Set1: 1 12 23 47 21 98
Set2: 21 54 78 75 101 47
----------------------
Union: 1 12 23 47 21 98 54 78 75 101
Intersection: 47 21
Difference (Set1 - Set2): 1 12 23 98
Complement: 54 78 75 101

5
Project 2: Function Composition and Inversion
• Description: Create a program to perform composition and inversion
of mathematical functions represented as arrays or mappings.
• Key Concepts: Arrays, functions, recursion.

Input:

#include<stdio.h>
int fact(int x)
{
if(x == 0 || x == 1)
{
return x;
}
else
{
return x * fact(x-1);
}
}

void main()
{
int n, r, permutation, combination;
printf("Enter the Permutation and Combination\n\n");
printf("Enter the total number of objects (n):");
scanf("%d",&n);
printf("Enter the number of selected objects (r):");
scanf("%d",&r);

if (n >= r)
{
if(n == r)
{

6
permutation = fact(n);
combination = 1;
}
else
{
permutation = fact(n) / fact(n-r);
combination = fact(n) / (fact(r) * fact(n-r));
}
printf("\n----------- Permutation ----------\n");
printf("Permutation (nPr) is: %d\n", permutation);
printf("\n----------- Combination ----------\n");
printf("Combination (nCr) is: %d\n", combination);
}
else
{
printf("\nThe number of selected objects (n) can't be greater than total number
of objects (r).");
}
}

Output:

Enter the Permutation and Combination

Enter the total number of objects (n):5


Enter the number of selected objects (r):3

----------- Permutation ----------


Permutation (nPr) is: 60

----------- Combination ----------


Combination (nCr) is: 10

You might also like