You are on page 1of 32

COLLEGE OF TECHNOLOGY AND ENGINEERING

MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY


UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

INDEX
S.NO PROGRAM NAME DATE SIGN &
REMARK

1
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

2
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

3
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 1
AIM: Write a program to find mean mode median after sorting array
CODE:
#include <stdio.h>
#include<conio.h>
main()
{
int array[100], n, f, d, swap,i,j,sum=0,t,b[20]={0},k=0,c=1,max=0,mode;
float x=0.0,y=0.0;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
sum=sum+array[i];
}
for (f = 0 ; c < ( f - 1 ); f++)
{
for (d = 0 ; d < n - f - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
x=(float)sum/(float)n;
printf("Mean\t= %f",x);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(array[i]>array[j])
{
t=array[i];
array[i]=array[j];
array[j]=t;
}

4
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

}
}
if(n%2==0)
y=(float)(array[n/2]+array[(n-1)/2])/2;
else
y=array[(n-1)/2];
printf("\nMedian\t= %f",y);
for(i=0;i<n-1;i++)
{
mode=0;
for(j=i+1;j<n;j++){
if(array[i]==array[j]){
mode++;
}
}
if((mode>max)&&(mode!=0)){
k=0;
max=mode;
b[k]=array[i];
k++;
}
else if(mode==max){
b[k]=array[i];
k++; } }
for(i=0;i<n;i++){
if(array[i]==b[i])
c++; }
if(c==n)
printf("\nThere is no mode");
else{
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}

5
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 2
AIM: Write a program to perform specific functions on BST
CODE:
#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete();
break;

6
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
void create()
{
int data;
printf("Enter data of node to be inserted : ");
scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
temp->value = data;
temp->l = temp->r = NULL;
}
void search(struct btnode *t)
{
if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */
search(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */
search(t->l);
else if ((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}
void inorder(struct btnode *t)

7
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
printf("%d -> ", t->value);
if (t->r != NULL)
inorder(t->r);
}
void delete()
{
int data;
if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the data to be deleted : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
printf("%d -> ", t->value);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)

8
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->value);
}
void search1(struct btnode *t, int data)
{
if ((data>t->value))
{
t1 = t;
search1(t->r, data);
}
else if ((data < t->value))
{
t1 = t;
search1(t->l, data);
}
else if ((data==t->value))
{
delete1(t);
}
}
void delete1(struct btnode *t)
{
int k;
if ((t->l == NULL) && (t->r == NULL))
{
if (t1->l == t)
{
t1->l = NULL;
}
else
{
t1->r = NULL;
}
t = NULL;
free(t);
return;
}
else if ((t->r == NULL))
{
if (t1 == t)
{
root = t->l;
t1 = root;

9
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

else if (t1->l == t)
{
t1->l = t->l;
}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
else if (t->l == NULL)
{
if (t1 == t)
{
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
}
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{
k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value = k;
}
}
int smallest(struct btnode *t)
{

10
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value);
}
int largest(struct btnode *t)
{
if (t->r != NULL)
{
t2 = t;
return(largest(t->r));
}
else
return(t->value);
}

11
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 3
AIM: Write a program for Apriori algorithm implementation
CODE:
import java.io.*;
import java.util.*;
public class Apriori extends Observable {
public static void main(String[] args) throws Exception {
Apriori ap = new Apriori(args);
}
private List<int[]> itemsets ;
private String transaFile;
private int numItems;
private int numTransactions;
private double minSup;
private boolean usedAsLibrary = false;
public Apriori(String[] args, Observer ob) throws Exception
{
usedAsLibrary = true;
configure(args);
this.addObserver(ob);
go();
}
public Apriori(String[] args) throws Exception
{
configure(args);
go();
}
private void go() throws Exception {
long start = System.currentTimeMillis();
createItemsetsOfSize1();
int itemsetNumber=1; //the current itemset being looked at
int nbFrequentSets=0;
while (itemsets.size()>0)
{
calculateFrequentItemsets();
if(itemsets.size()!=0)
{
nbFrequentSets+=itemsets.size();
log("Found "+itemsets.size()+" frequent itemsets of size " + itemsetNumber + " (with support
"+(minSup*100)+"%)");;
createNewItemsetsFromPreviousOnes();
}
itemsetNumber++;
}

12
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

long end = System.currentTimeMillis();


log("Execution time is: "+((double)(end-start)/1000) + " seconds.");
log("Found "+nbFrequentSets+ " frequents sets for support "+(minSup*100)+"% (absolute
"+Math.round(numTransactions*minSup)+")");
log("Done");
}
private void foundFrequentItemSet(int[] itemset, int support) {
if (usedAsLibrary) {
this.setChanged();
notifyObservers(itemset);
}
else {System.out.println(Arrays.toString(itemset) + " ("+ ((support / (double) numTransactions))+"
"+support+")");}
}
private void log(String message) {
if (!usedAsLibrary) {
System.err.println(message);
}
}
private void configure(String[] args) throws Exception
{
if (args.length!=0) transaFile = args[0];
else transaFile = "chess.dat"; // default
if (args.length>=2) minSup=(Double.valueOf(args[1]).doubleValue());
else minSup = .22;// by default
if (minSup>1 || minSup<0) throw new Exception("minSup: bad value");
numItems = 0;
numTransactions=0;
BufferedReader data_in = new BufferedReader(new FileReader(transaFile));
while (data_in.ready()) {
String line=data_in.readLine();
if (line.matches("\\s*")) continue; // be friendly with empty lines
numTransactions++;
StringTokenizer t = new StringTokenizer(line," ");
while (t.hasMoreTokens()) {
int x = Integer.parseInt(t.nextToken());
if (x+1>numItems) numItems=x+1;
}
}
outputConfig();
}
private void outputConfig() {
//output config info to the user
log("Input configuration: "+numItems+" items, "+numTransactions+" transactions, ");
log("minsup = "+minSup+"%");
}
private void createItemsetsOfSize1() {

13
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

itemsets = new ArrayList<int[]>();


for(int i=0; i<numItems; i++)
{
int[] cand = {i};
itemsets.add(cand);
}
}
private void createNewItemsetsFromPreviousOnes()
{
int currentSizeOfItemsets = itemsets.get(0).length;
log("Creating itemsets of size "+(currentSizeOfItemsets+1)+" based on "+itemsets.size()+" itemsets of
size "+currentSizeOfItemsets);
HashMap<String, int[]> tempCandidates = new HashMap<String, int[]>(); //temporary candidates
for(int i=0; i<itemsets.size(); i++)
{
for(int j=i+1; j<itemsets.size(); j++)
{
int[] X = itemsets.get(i);
int[] Y = itemsets.get(j);
assert (X.length==Y.length);
int [] newCand = new int[currentSizeOfItemsets+1];
for(int s=0; s<newCand.length-1; s++) {
newCand[s] = X[s];
}
int ndifferent = 0;
for(int s1=0; s1<Y.length; s1++)
{
boolean found = false;
for(int s2=0; s2<X.length; s2++) {
if (X[s2]==Y[s1]) {
found = true;
break;
}
}
if (!found){ // Y[s1] is not in X
ndifferent++;
newCand[newCand.length -1] = Y[s1];
}
}
assert(ndifferent>0);
if (ndifferent==1) {
Arrays.sort(newCand);
tempCandidates.put(Arrays.toString(newCand),newCand);
}
}
}

14
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

itemsets = new ArrayList<int[]>(tempCandidates.values());


log("Created "+itemsets.size()+" unique itemsets of size "+(currentSizeOfItemsets+1));

}
private void line2booleanArray(String line, boolean[] trans) {
Arrays.fill(trans, false);
StringTokenizer stFile = new StringTokenizer(line, " "); //read a line from the file to the tokenizer
while (stFile.hasMoreTokens())
{
int parsedVal = Integer.parseInt(stFile.nextToken());
trans[parsedVal]=true; //if it is not a 0, assign the value to true
}
}
private void calculateFrequentItemsets() throws Exception
{
log("Passing through the data to compute the frequency of " + itemsets.size()+ " itemsets of size
"+itemsets.get(0).length);
List<int[]> frequentCandidates = new ArrayList<int[]>(); //the frequent candidates for the current itemset
boolean match; //whether the transaction has all the items in an itemset
int count[] = new int[itemsets.size()]; //the number of successful matches, initialized by zeros
BufferedReader data_in = new BufferedReader(new InputStreamReader(new
FileInputStream(transaFile)));
boolean[] trans = new boolean[numItems];
for (int i = 0; i < numTransactions; i++) {
String line = data_in.readLine();
line2booleanArray(line, trans);
for (int c = 0; c < itemsets.size(); c++) {
match = true; // reset match to false
int[] cand = itemsets.get(c);
for (int xx : cand) {
if (trans[xx] == false) {
match = false;
break;
}
}
if (match) {
count[c]++;
}
}
}
data_in.close();
for (int i = 0; i < itemsets.size(); i++) {
if ((count[i] / (double) (numTransactions)) >= minSup) {
foundFrequentItemSet(itemsets.get(i),count[i]);
frequentCandidates.add(itemsets.get(i));
}
}

15
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

itemsets = frequentCandidates;
}
}

16
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 4
AIM:Write a program to count the number of Cuboids.
CODE:
#include<stdio.h>
#include<conio.h>

int main()
{ int n,p,l[10],i,flag=0;
clrscr();
printf("Enter no. of dimensions: ");
scanf("%d",&n);
for (i=1;i<=n;i++)
{ printf("Enter level for dimension %d",i);
scanf("%d",&l[i]);
}
for(i=1;i<=n;i++)
{ if(l[i]!=0)
flag = 1;
}
if(flag==0)
{ for(i=1,p=1;i<=n;i++)
{
p=p*2;
}
}
else
{ for(i=1,p=1;i<=n;i++)
p=p*(l[i]+1);
}
printf("No. of cuboids are: %d",p);
getch();
}

17
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 5
AIM:Write a program to implement Data Smoothening techniques using following
methods on n elements. Also partition the Data into even depth bucket based on user
choice. Bin by boundary Bin by Mean Bin by Median

CODE:
#include<stdio.h>
#include<conio.h>
void main()
{ int j,n,a,b[30],b1[30],b2[30],b3[30],k,i;
clrscr();
printf("Enter no. of elements\n");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++)
{ printf("Enter value for element %d ",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{ for(j=0;j<n-i;j++)
{ if(b[j]>b[j+1]) { a=b[j];
b[j]=b[j+1];
b[j+1]=a;
} } }
printf("Sorted no. are \n");
for(i=0;i<n;i++)
printf("%d\n",b[i]);
printf("\nEnter size of bin");
scanf("%d",&k);
for(i=0;i<n/k;i++)
{ for(j=1;j<k;j++)
{ if((b[j+(i)*k]-b[(i)*k])<(b[(i)*k+k-1]-b[(i)*k+j])) b1[(i)*k+j]=b[(i)*k];
else b1[(i)*k+j]=b[(i)*k+k-1];
}
b1[(i)*k]=b[(i)*k];
b1[(i+1)*k]=b[(i)*k+k-1]; }
printf("\nBy bin boundary no.s are\n");
for(i=0;i<n/k;i++)
{ for(j=0;j<k;j++)

18
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

printf("%d",b1[(i)*k+j]);
printf("\t"); }
printf("\n");
for(i=0;i<n/k;i++)
{ for(j=0,a=0;j<k;j++)
a=a+b[(i)*k +j];
a=a/5;
for(j=0;j<k;j++)
b2[(i)*k+j]=a; }
printf("\nBy mean method no are \n");
for(i=0;i<n/k;i++)
{ for(j=0;j<k;j++)
printf("%d",b2[(i)*k+j]);
printf("\t"); }
for(i=0,a=0;i<n/k;i++)
{ if(k%2==0) a=((b[(i)*k+k/2])+(b[(i)*k+k/2+1]))/2;
Else a=b[(i)*k+(k+1)/2];
for(j=0;j<k;j++)
b3[(i)*k+j]=a; }
printf("\nBy median method no are \n");
for(i=0;i<n/k;i++)
{ for(j=0;j<k;j++)
printf("%d",b3[(i)*k+j]);
printf("\t"); }
getch(); }

19
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 6
AIM:Write a program to implement the data transformation techniques on given array.
CODE:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i,j;
float s,n,d,a[100],b[100],min,max,nmin,nmax,m;
clrscr();
printf("Enter no.of elements : ");
scanf("%f",&n);
printf("Enter elements \n");
for(i=0;i<n;i++)
{
printf("Enter Value for Element %d:",i);
scanf("%f",&a[i]);
}
printf("Enter Normalisation limits: \n Min limit:");
scanf("%f" ,&nmin);
printf("Max limit:" );
scanf("%f",&nmax);
for(i=0,min = a[0],max=a[0];i<n;i++)
{
if(a[i] <min)
min =a[i];
if(a[i] >max)
max = a[i];
}

for(i=0;i<n;i++)
b[i]=(a[i] - min) * (nmax-nmin) /(max-min) + nmin;

for(i=0;i<n;i++)
printf("%f \n",b[i]);
printf("\n by Z-score method :\n");
for(i=0,m=0;i<n;i++)
m=m+a[i];
m=m/n;

for(i=0;i<n;i++)
{ for(j=0,d=0;j<n;j++)
d=d+(a[j]-m) * (a[j] -m);
s=sqrt(d/n);

20
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

b[i]=(a[i]-m)/s;
}

for(i=0;i<n;i++)
printf("%f \n ",b[i]);
getch();
}

21
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 7
AIM:Write a program to check that two attributes are correlated using Pearson product coefficient.
CODE:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i,j;
float a[100],b[100],n,am,bm,sa,sb,r;
clrscr();
printf("enter no. of elements: ");
scanf("%f" ,&n);
printf("enter elements for a \n");
for(i=1;i<=n;i++)
{
printf("Elements %d: ",i);
scanf("%f",&a[i]);
}

printf("Enter elements for b \n");


for(i=1;i<=n;i++)
{
printf("Elements %d: ",i);
scanf("%f",&b[i]);
}
for(i=1,am=0,bm=0;i<=n;i++)
{
am=am + a[i];
bm=bm + b[i];
}
am=am/n;
bm=bm/n;
for(i=1,sa=0,sb=0;i<=n;i++)
{
sa=sa+(a[i]-am)*(a[i]-am);
sb=sb+(b[i]-bm)*(b[i]-bm);
}
sa=sa/n;
sa=sqrt(sa);
sb=sb/n;
sb=sqrt(sb);
for(i=1,r=0;i<=n;i++)
r=r+(a[i]-am)*(b[i]-bm);
r=r/(n*sa*sb);

22
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

if(r>0)
printf("\n a & b are positively correlated \n");
else if(r==0)
printf("a & b are independent");
else
printf("\n a & b are negatively independent");
printf("\n %f",r);
getch();
}

23
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 8
AIM:Write a program to calculate dissimilarity for given patients using binary asymmetric variable
using structure.
CODE:
#include<stdio.h>
#include<conio.h>

int i,j;
struct patients
{
char fever,cough,test1,test2,test3,test4;
}
jack={'p','n','p','n','n','n'},
mary={'p','n','p','n','p','n'},
jim={'p','p','n','n','n','n'};

int fun(struct patients,struct patients,char,char);

void main()
{
int q,r,s,t;
float d;
clrscr();
q=fun(jack,mary,'p','p');
r=fun(jack,mary,'p','n');
s=fun(jack,mary,'n','p');
t=fun(jack,mary,'n','n');
d=(( float)(r+s))/(q+r+s);
printf("Contigency matrix for jack-mary \n");
printf("%d \t %d \n %d\t %d\n",q,r,s,t);
printf("Dissimilarity for jack mary is %f\n",d);

q=fun(mary,jim ,'p','p');
r=fun(mary,jim,'p','n');
s=fun(mary,jim,'n','p');
t=fun(mary,jim,'n','n');
d=(( float)(r+s))/(q+r+s);
printf("Contigency matrix for mary-jim \n");
printf("%d \t %d \n %d\t %d\n",q,r,s,t);
printf("Dissimilarity for jim mary is %f\n",d);

q=fun(jack,jim,'p','p');
r=fun(jack,jim,'p','n');
s=fun(jack,jim,'n','p');
t=fun(jack,jim,'n','n');

24
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

d=(( float)(r+s))/(q+r+s);
printf("Contigency matrix for jack-jim \n");
printf("%d \t %d \n %d\t %d\n",q,r,s,t);
printf("Dissimilarity for jack jim is %f\n",d);

getch();
}
int fun(struct patients p1,struct patients p2,char a,char b)
{
int i = 0;
if(p1.fever==a && p2.fever==b)
i++;
if(p1.cough==a && p2.cough==b)
i++;
if(p1.test1==a && p2.test1==b)
i++;
if(p1.test2==a && p2.test2==b)
i++;
if(p1.test3==a && p2.test3==b)
i++;
if(p1.test4==a && p2.test4==b)
i++;
return i;
}

25
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 9
AIM:Write a program to implement the Concept Hierarchy.
CODE:
#include<stdio.h>
#include<conio.h>
int roundmin(int);
int roundmax(int);

void main()
{
int a,b,i,j,k,f,level1[10],level2[10];
int level3[10], min=-351,max=4700,rmin=-159,rmax=1838;
clrscr();
level1[0]=roundmin(rmin);
level1[1]=roundmax(rmax);
level2[2]=12345;
a=roundmin(min);
b=roundmax(max);
if(a<level1[0])
{
level1[2]=level1[0];
level2[3]=level1[1];
level1[0]=a;
level1[1]=level1[0];
level1[4]=12345;
}
else if(b>level1[1])
{
level1[2]=level1[1];
level1[3]=b;
level1[4]=12345;
}
i=0;
while(level1[i]!=12345)
{
printf("\t%d<->%d ",level1[i],level1[i+1]);
i=i+2;
}

//level 2

for(i=0,j=0;level1[i]!=12345;i+=2)
{
a=(level1[i+1]-level1[i])/1000;
if(a==3||a==6||a==9)

26
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

{
f=(level1[i+1]-level1[i])/3;
level2[j++]=level1[i];
level2[j++]=level1[i]+f;
level2[j++]=level2[j-1];
level2[j++]=level1[i]+2*f;
level2[j++]=level2[j-1];
level2[j++]=level1[i+1];
}
else if(a==2||a==4||a==8)
{
f=(level1[i+1]-level1[i])/4;
level2[j++]=level1[i];
level2[j++]=level1[i]+f;
level2[j++]=level2[j-1];
level2[j++]=level1[i]+2*f;
level2[j++]=level2[j-1];
level2[j++]=level1[i]+3*f;
level2[j++]=level2[j-1];
level2[j++]=level1[i+1];
}
else if(a==7)
{
f=(level1[i+1]-level1[i])/7;
level2[j++]=level1[i];
level2[j++]=level1[i]+2*f;
level2[j++]=level2[j-1];
level2[j++]=level1[i]+5*f;
level2[j++]=level2[j-1];
level2[j++]=level1[i+1];
}
else if(a==1||a==5)
{
f=(level1[i+1]-level1[i])/5;
level2[j++]=level1[i];
level2[j++]=level1[i]+f;
level2[j++]=level2[j-1];
level2[j++]=level1[i]+2*f;
level2[j++]=level2[j-1];
level2[j++]=level1[i]+3*f;
level2[j++]=level2[j-1];
level2[j++]=level1[i]+4*f;
level2[j++]=level2[j-1];
level2[j++]=level1[i+1];
}
}
printf("\n");

27
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

i=0;
while(i<j)
{
printf("%d<->%d ",level2[i],level2[i+1]);
i=i+2;
}
printf("\n");
//level 3

for(i=0,k=0;i<j;i+=2)
{
a=((level2[i+1]-level2[i])/500);
if(a==3||a==6||a==9)
{
f=(level2[i+1]-level2[i])/3;
level3[k++]=level2[i];
level3[k++]=level3[k-1];
level3[k++]=level2[i]+f;
level3[k++]=level3[k-1];
level3[k++]=level2[i]+2*f;
level3[k++]=level2[i+1];
}
else if(a==2||a==4||a==8)
{
f=(level2[i+1]-level2[i])/4;
level3[k++]=level2[i];
level3[k++]=level2[i]+f;
level3[k++]=level3[k-1];
level3[k++]=level2[i]+2*f;
level3[k++]=level3[k-1];
level3[k++]=level2[i]+3*f;
level3[k++]=level3[k-1];
level3[k++]=level2[i+1];
}
else if(a==1||a==5)
{
f=(level2[i+1]-level2[i])/5;
level3[k++]=level2[i];
level3[k++]=level2[i]+f;
level3[k++]=level3[k-1];
level3[k++]=level2[i]+2*f;
level3[k++]=level3[k-1];
level3[k++]=level2[i]+3*f;
level3[k++]=level3[k-1];
level3[k++]=level2[i]+4*f;
level3[k++]=level3[k-1];
level3[k++]=level2[i+1];

28
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

}
else if(a==7)
{
f=(level2[i+1]-level2[i])/7;
level3[k++]=level2[i];
level3[k++]=level2[i]+2*f;
level3[k++]=level3[k-1];
level3[k++]=level2[i]+5*f;
level3[k++]=level3[k-1];
level3[k++]=level2[i+1];
}
}
printf("\n");
i=0;
while(i<k)
{
printf("%d<->%d ",level3[i],level3[i+1]);
i=i+2;
}
getch();
}

int roundmin(int min)


{
if(min%1000!=0)
return((min/1000)-1)*1000;
else
return min;
}

int roundmax(int max)


{
if(max%1000!=0)
return ((max/1000)+1)*1000;
else
return max;
}

29
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

DATE: / /
PROGRAM 10
AIM:Write a program to find the information gain and hence find the test attribute.
CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float gain(float,float);
void main()
{
float i,x,y,x1=0,x2=0,x3=0,y1=0,y2=0,y3=0,a,b,c,g,eage;
char ch;
FILE *f;
clrscr();
f=fopen("gs_dmw7.txt","r");
if(f==NULL)
printf("file can't open");
else{
x=0;y=0;
while(1)
{ ch=fgetc(f);
if(ch=='y')
if((ch=fgetc(f))=='e')
if((ch=fgetc(f))=='s')
x=x+1;
if(ch=='n'){
if((ch=fgetc(f))=='o')
y=y+1;
}
if(ch==EOF)
break;
}
i=gain(x,y);
printf("gain=%f",gain(x,y));
f= fopen("gs_dmw7.txt","r");
while((ch=fgetc(f))!=EOF){
if(ch=='y'){
if((ch=fgetc(f))=='o')
if((ch=fgetc(f))=='u')
if((ch=fgetc(f))=='s')
if((ch=fgetc(f))=='h')
while((ch=fgetc(f))!='\n')
{
if(ch=='y')
{
if((ch=fgetc(f))=='e')

30
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

if((ch=fgetc(f))=='s')
x1=x1+1;
}
else if(ch=='n')
{
if((ch=fgetc(f))=='o')
y1=y1+1;
}
if(ch==EOF)
break;
}
}
else if(ch=='m')
{
if((ch=fgetc(f))=='i')
if((ch=fgetc(f))=='d')
if((ch=fgetc(f))=='d')
if((ch=fgetc(f))=='l')
if((ch=fgetc(f))=='e')
if((ch=fgetc(f))=='a')
if((ch=fgetc(f))=='g')
if((ch=fgetc(f))=='e')
while((ch=fgetc(f))=='\n')
{
if(ch=='y')
{
if((ch=fgetc(f))=='e')
if((ch=fgetc(f))=='s')
x2=x2+1;
}
else if(ch=='n')
{
if((ch=fgetc(f))=='a')
y2=y2+1;
}
if(ch==EOF)
break;
}
}
else if(ch=='s')
if((ch=fgetc(f))=='e')
if((ch=fgetc(f))=='n')
if((ch=fgetc(f))=='i')
if((ch=fgetc(f))=='o')
if((ch=fgetc(f))=='r')
while((ch=fgetc(f))!='\n')
{

31
COLLEGE OF TECHNOLOGY AND ENGINEERING
MAHARANA PRATAP UNIVERSITY OF AGRICULTURE &TECHNOLOGY
UDAIPUR
NAME – SONU MEGHWAL CLASS – B.Tech 4TH YEAR (I.T.) SEM – 1ST

if(ch=='y')
{
if((ch=fgetc(f))=='e')
if((ch=fgetc(f))=='s')
x3=x3+1;
}}
else if(ch=='n')
{
if((ch=fgetc(f))=='o')
y3=y3+1;
}
if(ch==EOF)
break;
}

a=gain(x1,y1);
b=gain(x2,y2);
c=gain(x3,y3);
printf("\n a=%f \n b=%f \n c=%f",a,b,c);

eage=(x1+y1)*a/(x+y)+(x2+y2)*b/(x+y)+(x3+y3)*c/(x+y);
printf("\n eage=%f",eage);
g=(i-eage);
printf("\n net gain=%f",g);
}

getch();
}
float gain(float a,float b){
float c;
c=-(a/(a+b))*(log10(a/(a+b))/log10(2))-(b/(a+b))*(log10(b/(a+b))/log10(2));
return c;
}
OUTPUT
gain=0.940286
a=.970951
b=0.00000
c=0.970951
e(age)=0.693536
net gain=0.246750
a=1.000000
b=0.918000
c=0.811000
e(income)=0.911
net gain=0.029743
age is selected as splitting attribute.

32

You might also like