10 Programs
10 Programs
c
/*This program implemenmts a binary tree using the struct data type to create a node. */
#include <stdio.h>
#include <stdlib.h>
/* Search for element in the binary tree, and return 1 if true, 0 if false */
int search(struct Node* tn, int d)
{
if(tn == NULL)
return 0;
else if(tn->data == d)
/* Element found */
return 1;
else if(tn->data < d) /* Search left subtree */
return search(tn->right, d);
else
main() {
int ch, d, running = 1;
struct Node* tn = NULL;
while(running) {
/*Print menu of choices and accept choice */
printf("\n1: Insert new data\n");
printf("2: Search data\n");
printf("3: Print elements in order\n");
printf("4: Exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
/* Execute options */
switch(ch)
{
case 1: /* Insert data into binary tree */
printf("Enter new data:");
scanf("%d", &d);
tn = insert(tn, d);
break;
case 2: /* Search for a data item in the binary tree */
printf("Enter data to search:");
scanf("%d", &d);
if(search(tn, d))
printf("\nData found\n");
else
printf("\nData not found\n");
break;
case 3: /* Traverse the binary tree in order and print the elements' data values */
inorder(tn);
printf("\n");
break;
case 4: /* Assign continuation flag to 0*/
running = 0;
}
}
}
2.combination
#include <stdio.h>
#include <stdlib.h>
int n, r, count;
void calccomb(int pos, int beg, int* arr);
void showcomb(int* arr);
main()
{
int *arr;
printf("Enter n:");
scanf("%d", &n);
printf("Enter r:");
scanf("%d", &r);
arr = (int*)malloc(r * sizeof(int));
calccomb(0, 1, arr);
printf("Number of combinations: %d\n", count);
}
void calccomb(int pos, int beg, int* arr)
{
int i;
if(pos == r){
showcomb(arr);
return;
}
for(i = beg; i <= n-r+pos+1; i++)
{
arr[pos] = i;
calccomb(pos+1, i+1, arr);
}
}
void showcomb(int* arr)
{
++count;
int i;
for(i = 0; i < r; i++)
printf("%d ", arr[i]);
printf("\n");
}
3.file copy
#include <stdio.h>
main()
{
FILE *fs,*fd;
char ch, fname1[80], fname2[80];
printf("Enter file to copy from with extension(within 80 characters)");
gets(fname1);
printf("Enter file to copy to with extension(within 80 characters)");
gets(fname2);
fs = fopen(fname1,"rb");
fd = fopen(fname2,"wb");
if(fs == NULL)
perror("Error in opening source file.");
else
{
do
{
ch = getc(fs);
fputc(ch, fd);
}
while (!feof(fs));
fclose(fs);
fclose(fd);
}
}
4.heap sort
/*
* This program contains methods to implement and use the heap
* data structure.
*/
#include <stdio.h>
{}
/*
* This function transforms the subtree rooted at index to a max heap.
* It assumes the left and right subtrees of index are already max heaps
*/
void max_heapify(int a[], int size, int index)
{
if(index >= size) return;
int left, right, max;
index++;
left = index << 1;
right = (index << 1) + 1;
max = index;
if(left <= size && a[left - 1] > a[index - 1])
max = left;
if(right <= size && a[right - 1] > a[max - 1])
max = right;
if(max != index)
{
a[max - 1] = a[max - 1] ^ a[index - 1];
a[index - 1] = a[max - 1] ^ a[index - 1];
a[max ^ 1] = a[max - 1] ^ a[index - 1];
max_heapify(a, size, max - 1);
}
}
/*
* This function builds an unsorted array into a ax heap using the
* max_heapify procedure.
*/
void build_max_heap(int a[], int size)
{
int i;
for(i = size / 2; i >= 1; i--)
max_heapify(a, size, i - 1);
}
/*
* This function takes an unsorted array, contructs a heap from it
* using the build_max_heap() procedure,
* and sorts the array by heap sort method.
*/
void heap_sort(int a[], int size)
{
int length = size, i;
build_max_heap(a, size);
for(i = length; i >= 2; i--)
{
a[1] = a[1] ^ a[i];
a[i] = a[1] ^ a[i];
a[1] = a[1] ^ a[i];
size--;
max_heapify(a, 1, size);
}
}
5.mat.c
#include <stdio.h>
main()
{
FILE *fp;
int a[100][100], b[100][100], c[100][100];
int i, j, k, m, n, p;
printf("Enter the no. of rows and columns of matrix 1 :\n");
scanf("%d%d", &m, &n);
printf("Enter the no. of columns of matrix 2 :\n");
scanf("%d", &p);
fp=fopen("mat.txt", "r");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
fscanf(fp, "%d", &a[i][j]);
for(i=0; i<n; i++)
for(j=0; j<p; j++)
fscanf(fp, "%d", &b[i][j]);
for(i=0; i<m; i++)
for(j=0; j<p; j++)
for(k=0; k<n; k++)
c[i][j] = c[i][j] + (a[i][k])*(b[k][j]);
fclose(fp);
fp = fopen("mat.txt", "a");
fprintf(fp, "\nThe multiplied matrix is :\n");
for(i=0; i<m; i++)
{
for(j=0; j<p; j++)
item /= 10;
}
break;
case 'c':
c = va_arg(vlist, int);
putchar((char)c);
break;
case 's':
s = va_arg(vlist, char*);
while(*s) putchar(*(s++));
break;
case 'u':
uint = va_arg(vlist, unsigned int);
while(uint > 0) {
putchar(uint % 10 + 48);
uint /= 10;
}
break;
}
str++;
}
else putchar(ch);
}
va_end(vlist);
}
main()
{
myprintf("This is a test: %d %d %d %d %d\n", 11, 12, 13, 14, 15);
7.permutation
#include <stdio.h>
#include <stdlib.h>
showperm(arr+1, n - 1);
swap(arr, arr + i);
}
}
main()
{
int *arr, n, i;
cnt = 0;
printf("Enter n: ");
scanf("%d", &n);
n1 = n;
arr1 = arr = (int*)malloc(n * sizeof(int));
/*Assign the identity permutation to arr*/
for(i = 0; i < n; i++)
*(arr+i) = i + 1;
showperm(arr, n);
printf("Number of permutations: %d\n", cnt);
}
8.prng random no
#include <stdio.h>
int s[256], key[16], k[256], c1 = 0, c2 = 0;
int count[256];
void ksa();
int prng();
main()
{
printf("Enter 16 integers for key:");
for(int i = 0; i < 16; i++)
scanf("%d", &(a[i]));
for(int i = 0; i < 1000000; i++)
count[prng()]++;
printf("Counts of numbers:");
for(int i = 0; i < 256; i++)
printf("%d ", count[i]);
printf("\n");
}
void ksa()
{
int i, j;
/* Store identity permutaion in s */
for(i = 0; i < 256; i++)
s[i] = i;
j = 0;
for(i = 0; i < 256; i++){
j = (j + s[i] + k[i]) % 256;
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
int prng()
{
int temp;
c1 = (c1 + 1)%256;
c2 = (c2 + s[c1])%256;
temp = s[i];
s[i] = s[j];
s[j] = temp;
return s[(s[i] + s[j])%256];
}
9.queqe array
#define QSZ 20
#include <stdio.h>
struct Queue
{
int arr[QSZ];
int front, rear;
};
void init(struct Queue *q)
{
q->rear = q->front = -1;
}
int isEmpty(struct Queue *q)
{
return (q->front == -1);
}
int isFull(struct Queue *q)
{
return (q->front == QSZ - 1);
}
void push(struct Queue *q, int data)
{
int i;
if(isFull(q) == 1)
10.rc4
/*************** Start *****************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LOOP1 256*256
#define LOOP2 2
/*Function not used anywhere*/
unsigned int myrand(){
unsigned int j;
j=(unsigned int) (256.0*rand()/(RAND_MAX+1.0));
return j;
}
main(){
char str[80];
int s[256], i, j, temp, t, z, k[256], key[16], ml = 0, cnt = 0, r;
/* Use system as seed for library defined random function */
srand(time(NULL));
/* Run KSA and PRGA for LOOP1 random keys and check LOOP2TH pseudo random byte each time */
while(ml < LOOP1){
11.stack array
#define STKSZ 20
#include <stdio.h>
struct Stack
{
int arr[STKSZ];
int top;
};
typedef struct Stack stack;
void init(stack *s)
{
s->top = -1;
}
int isFull( stack *s)
{
if( s->top == STKSZ - 1)
return 1;
else
return 0;
}
int isEmpty ( stack *s)
{
if( s->top == -1)
return 1;
else
return 0;
}
void push(stack *s, int data)
{
if(isFull(s) == 1)
printf("Warning: Stack is full.");
else
{
(s->top)++;
s->arr[s->top] = data;
}
}
int pop(stack *s)
{
int temp;
if(isEmpty(s) == 1)
printf("Warning: Stack is empty.");
else
{
temp = s->arr[s->top];
(s->top)--;
return temp;
}
}
int top(stack *s)
{
if(isEmpty(s) ==1)
printf("warning: stack is empty.");
else
{
return s->arr[s->top];
}
}
void display(stack *s)
{
int i;
for(i = 0; i <= s->top; i++)
{
printf("%d ", s->arr[i]);
}
}