Created
April 25, 2011 08:43
-
-
Save ashutoshrishi/940285 to your computer and use it in GitHub Desktop.
Set operations - comp assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define ARR_S 10 // maximum array size | |
void input_arr(int arr[] , int size); | |
void print_arr(int arr[] , int size); | |
void bubble_sort(int arr[] , int size); | |
int main() { | |
int arr[10]; | |
input_arr(arr, ARR_S); | |
bubble_sort(arr, ARR_S); | |
printf("\nSorted array:\n"); | |
print_arr(arr, ARR_S); | |
return 0; | |
} | |
void input_arr(int arr[], int size) { | |
int i; | |
printf("Enter %d elements: \n", size); | |
for (i=0; i< size; i++) { | |
scanf("%d", &arr[i]); | |
} | |
} | |
void print_arr(int arr[], int size) { | |
int i; | |
printf("\nArray:\n"); | |
for (i=0; i< size; i++) { | |
printf("%d, ", arr[i]); | |
} | |
printf("\n"); | |
} | |
void bubble_sort(int arr[], int size) { | |
int i,j; | |
int temp; | |
for (i=0; i<size-1; i++) | |
for (j=0; j<size-1; j++) { | |
if (arr[j] < arr[j+1]) { | |
temp = arr[j]; | |
arr[j] = arr[j+1]; | |
arr[j+1] = temp; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main() { | |
int n,i,c,a,b; | |
a=0; | |
b=1; | |
printf("enter fibonacci series: "); | |
scanf("%d",&n); | |
printf("\n%d, %d, ",a,b); | |
for(i=3;i<=n;i++) | |
{ | |
c=a+b; | |
a=b; | |
b=c; | |
printf("%d, ",c); | |
} | |
printf("\n"); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int fibo(int n) { | |
if (n == 0) | |
return 1; | |
if (n == 1) | |
return 1; | |
return fibo(n-1) + fibo(n-2); | |
} | |
int main() { | |
int n, i; | |
printf("Enter number: "); | |
scanf("%d", &n); | |
printf("\n"); | |
for (i=1; i<=n; i++) | |
printf("%d, ", fibo(i+1)); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int factorial(int n); | |
int main() { | |
int n; | |
printf("Enter a number: "); | |
scanf("%d", &n); | |
printf("\nFactorial: %d\n", factorial(n)); | |
return 0; | |
} | |
int factorial(int n) { | |
if (n==1) | |
return n; | |
else | |
return n * factorial(n-1); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define SET_S 20 /*maximum set size*/ | |
void accept_arr(int[], int); | |
void set_union(int[], int[], int, int); /* set union */ | |
void set_intersect(int[], int[], int, int); /* set intersection */ | |
void set_diff(int[], int[], int, int); /* difference of two sets */ | |
void set_symdiff(int[], int[], int, int); /* symmetric difference of two sets */ | |
int main() { | |
int m, n; /* input size of arrays A and B */ | |
int set_A[SET_S], set_B[SET_S]; | |
printf("Enter size of set A: "); | |
scanf("%d", &m); | |
printf("\nEnter size of set B: "); | |
scanf("%d", &n); | |
/* Input arrays */ | |
accept_arr(set_A, m); | |
accept_arr(set_B, n); | |
/* Output */ | |
printf("Union of A and B:\n"); | |
set_union(set_A, set_B, m, n); | |
printf("\nIntersection of A and B:\n"); | |
set_intersect(set_A, set_B, m, n); | |
printf("\nDifference of A and B:\n"); | |
set_diff(set_A, set_B, m, n); | |
printf("\nSymmetric Difference of A and B:\n"); | |
set_symdiff(set_A, set_B, m, n); | |
return 0; | |
} | |
void accept_arr(int arr[], int size) { | |
int i; | |
printf("Enter %d elements: \n", size); | |
for (i=0; i<size; i++) { | |
scanf("%d", &arr[i]); | |
} | |
} | |
int in_set(int arr[], int size, int n); /* returns 1 (true) if n is in arr, 0 (false) if not */ | |
void set_union(int a[], int b[], int m, int n) { | |
int c[(m+n)]; /* c is the union of a and b */ | |
int i,j; | |
i = 0; | |
for (j=0; j<m; j++) | |
/* iterate over first set */ | |
if (!in_set(c, (i+1), a[j]) ) | |
c[i++] = a[j]; | |
for (j=0; j<n; j++) | |
/* iterates over second set */ | |
if (!in_set(c, (i+1), b[j])) | |
c[i++] = b[j]; | |
printf("SET C: \n"); | |
for (j = 0; j<i; j++) | |
printf("%d, ", c[j]); | |
printf("\n"); | |
} | |
/* returns 1 (true) if n is in arr, 0 (false) if not */ | |
int in_set(int arr[], int size, int n) { | |
int i; | |
for (i=0; i<size; i++) { | |
if (arr[i] == n) | |
return 1; | |
} | |
return 0; | |
} | |
void set_intersect(int a[], int b[], int m, int n) { | |
int i, j; | |
printf ("Intersection: \n"); | |
j = 0; | |
for (i=0; i<m; i++) { | |
if (in_set(b, n, a[i])) | |
printf("%d, ", a[i]); | |
} | |
printf("\n"); | |
} | |
void set_diff(int a[], int b[], int m, int n) { | |
int i; | |
printf ("Difference: \n"); | |
for (i=0; i<m; i++) { | |
if (!in_set(b, n, a[i])) | |
printf("%d, ", a[i]); | |
} | |
printf("\n"); | |
} | |
void set_symdiff(int a[], int b[], int m, int n) { | |
int i; | |
printf ("Symmetric Difference: \n"); | |
for (i=0; i<m; i++) { | |
if (!in_set(b, n, a[i])) | |
printf("%d, ", a[i]); | |
} | |
for (i=0; i<n; i++) { | |
if (!in_set(a, n, b[i])) | |
printf("%d, ", b[i]); | |
} | |
printf("\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define STR_S 50 /* maximum string size */ | |
int mstrlen(char str[]); | |
void mstrcpy(char to[], char from[]); | |
int mstrcmp(char a[], char b[]); | |
void mstrcat(char to[], char str[]); | |
void reverse(char []); | |
void getstr(char []); | |
int main() { | |
char string[STR_S]; | |
char string2[STR_S]; | |
int c, i; | |
int choice; | |
printf("Enter string: "); | |
getstr(string); | |
printf("D: string: %s", string); | |
printf("\n1.Len\n2.Cpy\n3.\n4.\n5.\n"); | |
printf("\nchoice: "); | |
scanf("%d", &choice); | |
switch(choice) { | |
case 1: | |
/* strlen: length of string */ | |
printf("\nLength of the string: %d", mstrlen(string)); | |
break; | |
case 2: | |
/* strcpy: copy strings */ | |
printf("\nEnter new string to copy: "); | |
getstr(string); | |
mstrcpy(string, string2); | |
printf("\nnew string: %s", string); | |
break; | |
case 3: | |
/* strcmp: compare two strings */ | |
printf("\nEnter new string to compare: "); | |
getstr(string2); | |
printf("\nResult: %d", mstrcmp(string, string2)); | |
break; | |
case 4: | |
printf("\nEnter string to concat: "); | |
getstr(string2); | |
mstrcat(string, string2); | |
printf("\nNew string: %s", string); | |
break; | |
case 5: | |
mstrcpy(string2, string); | |
reverse(string); | |
printf("\nReversed string: %s\n", string); | |
printf("\nString: %s\nString2: %s\n\n", string, string2); | |
printf("\n\nD: %d\n\n", mstrcmp(string2, string)); | |
if ( mstrcmp(string2, string) == 0) | |
printf("\nPALINDROME!!\n\n"); | |
break; | |
default: | |
break; | |
} | |
return 0; | |
} | |
void getstr(char str[]) { | |
/* int c, i; | |
* | |
* i = 0; | |
* while ( (c=getchar()) != '\n') | |
* str[i++] = c; | |
* str[i] = '\0'; | |
*/ | |
fgets(str, STR_S, stdin); | |
str[mstrlen(str)-2] = '\0'; | |
} | |
int mstrlen(char str[]) { | |
int i; | |
i = 0; | |
while(str[i++] != '\0') | |
; | |
return i; | |
} | |
void mstrcpy(char to[], char from[]) { | |
int i; | |
while ( from[i] != '\0') { | |
to[i] = from[i]; | |
i++; | |
} | |
to[i] = '\0'; | |
} | |
/* strcmp: compare a[] to b[]. 0 if equal. */ | |
int mstrcmp(char a[], char b[]) { | |
int i; | |
for (i=0; a[i] == b[i]; i++) | |
if (a[i] == '\0') | |
return 0; | |
return a[i] - b[i]; | |
} | |
void mstrcat(char to[], char str[]) { | |
int i, j; | |
i = 0; | |
j = 0; | |
while (to[i] != '\0') | |
i++; | |
while ( str[j] != '\0') { | |
to[i] = str[j]; | |
j++; | |
i++; | |
} | |
to[i] = '\0'; // null terminate the final string | |
} | |
void reverse(char str[]) { | |
int i, j; | |
int c; | |
for (i=0, j=mstrlen(str)-2; i < j; i++, j--) { | |
c = str[i]; | |
str[i] = str[j]; | |
str[j] = c; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment