Posts

Showing posts from January 30, 2011

PROLOG ASSIGNMENT 3 (SAU).(MIN_COST)

path_C(A, Z, Graph, Path, Cost) :-           path1_C(A, [Z], 0, Graph, Path, Cost). path1_C(A, [A|Path1], Cost1, Graph, [A|Path1], Cost1). path1_C(A, [Y|Path1], Cost1, graph(Nodes, Edges), Path, Cost) :-           (member(e(X,Y,CostXY), Edges); member(e(Y,X,CostXY), Edges)),            not (member(X, Path1)),           Cost2 is Cost1 + CostXY,           path1_C(A, [X,Y|Path1], Cost2, graph(Nodes, Edges), Path, Cost). shortest_path(A, Z, Graph, Path, Mini_Cost) :-          path_C(A, Z, Graph, Path, Mini_Cost),          no_shorter_path(A, Z, Graph, Path1, Cost1, Mini_Cost). no_shorter_path(A, Z, Graph, Path, Cost, Mini_Cost) :-        ...

PROLOG ASSIGNMENT 3 (SAU).

path(A, Z, Graph, Path) :-           path1(A,[Z],Graph,Path). path1(A, [A|Path1], Graph, [A|Path1]). path1(A, [Y|Path1], graph(Nodes, Edges), Path) :-                 (member(e(X,Y), Edges); member(e(Y,X), Edges)),                 not (member(X, Path1)),                        path1(A, [X,Y|Path1], graph(Nodes, Edges), Path). member(X, [X|L]). member(X, [Y|L]) :-     member(X, L). eq_set(P, N) :-     subset(P,N),     subset(N,P). subset([],[]). subset( [X|Set],Subset)  :-            % X not in subset   subset(Set,Subset). subset( [X|Set],[X|Subset])  :-      % X included in subset   subset(...

MATRIX MULTIPLICATION

Q. Write a program that contains three functions, the first two will be encoder and decoder function used to decode and encode a matrix of integers.The encoder will simply swap the values of elements A ij and A ji. The decoder will unswapp the values,So for example A 12 will be swapped with A 21during encoding and back during decoding.Write the third function to multiply three matrices. Now a data file will be in the following formate <'E or D'><N rows><M column><A 11A 12A 13....AN rowsM columns> The first character of the file will be either E or D indicating whether the data is encoded or decoded. The following integers indicate the number of rows and columns,followed by matrix data. Write a program to read two encoded files (Say M1.dat M2.dat) and multiply them to generate a third encoded file (say M3.dat) in the exact same formatedescribed above.use command line argument. So to run it with the above three files I should type ./a.out M1.dat M2.dat ...

Histogram

#include<stdio.h> int main() { int a[]={1,2,1,3,5,4,3,2,1,2,3,1,4,5,3,2,1,6,5,2,2,1,1}; histogram(a,23); return 0; } int histogram(int a[],int n){ int i,j,k=0,l,m=0,count[25]={0},max=0; for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(a[i]==a[j]) count[i]++; //counting the occurance of each element in the array } int flag=0; for(j=0;j<i;j++) if(a[i]==a[j]) flag=1; // removing redundant element //====================displaying histogram1===================*/ if(!flag) { a[m]=a[i]; count[m]=count[i]; max=max<count[m]?count[m]:max; m++; printf("%d %d ",a[i],count[i]); for(k=0;k<count[i];k++) printf("*"); printf("\n"); } } printf("=================displaying histogram2==================\n"); for(i=0;i<m;i++) { for(l=0;l<max-count[i];l++) printf("\t"); for(j=0;j<count[i];j++) printf("*\t"); printf(...