Posts

Showing posts from May, 2015

C program for finding Optimal Binary Search Tree

Image
//Program for OBST#include<stdio.h>float e[6][6],w[6][6],t;int root[6][6];float p[6],q[6];void obst(int i, int j){ int value = root[i][j]; if(i==value) { printf("left child of k%d is d%d\n",value,value-1); } else { printf("left child of k%d is k%d\n",value,root[i][value-1]); obst(i,value-1); } if(j==value) { printf(" right child of k%d is d%d\n",value,value); } else { printf(" right child of k%d is k%d\n",value,root[value+1][j]); obst(value+1,j); }}int main(){p[1]=0.15;p[2]=0.10;p[3]=0.05;p[4]=0.10;p[5]=0.2;q[0]=0.05;q[1]=0.10;q[2]=0.05;q[3]=0.05;q[4]=0.05;q[5]=0.1;int l,i,n=5,r,j;for(i=1;i<=(n+1);i++){ e[i][i-1]=q[i-1]; w[i][i-1]=q[i-1];}for(l=1;l<=n;l++){ for(i=1;i<=(n-l+1);i++) { j=i+l-1; e[i][j]=10000; w[i][j]=w[i][j-1]+p[j]+q[j]; for(r=i;r<=j;r++) { t=e[i][r-1]+e[r+1][j]+w[i][j]; if(t<e[i][j]) { e[i][j]=t; root[i][j]=r; } } }}printf("w :\n");for(i=1;i<=(n+1);i++){ for(j=i-1;j&

Calculating running program time - C Programming Tutorial

Image
In this c programming tutorial we are going to see how we can calculate actual running time for any c program or algorithm implementation. Below is the code calculates running time

Least Common Sequence program in C

Image
#include<stdio.h>#include<string.h>void main(){ int i,j,m,n; char x[10],y[10],x1[10],y1[10],b[10][10]={ },ans[10]; int c[10][10]; int flag=0,wflag=1,k; int temp_i,temp_j; printf("\nEnter X sequence"); scanf("%s",x1); printf("\nEnter Y sequence"); scanf("%s",y1); m=strlen(x1); n=strlen(y1); x[0]='\0'; y[0]='\0'; for(i=1;i<=m;i++) { c[i][0]=0; x[i]=x1[i-1]; } x[i]=x1[i-1]; for(i=0;i<=n;i++) { c[0][i]=0; y[i+1]=y1[i]; } for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { if(x[i]==y[j]) { c[i][j]=c[i-1][j-1]+1; b[i][j]='\\'; } else if((c[i-1][j])>=(c[i][j-1])) { c[i][j]=c[i-1][j]; b[i][j]='^'; } else { c[i][j]=c[i][j-1]; b[i][j]='<'; } } } printf("\n\ty"); for(j=1;j<=n;j++) { printf("\t%c",y[j]); } printf("\nx"); for(j=0;j<=n;j++) { printf("\t%d",c[j][0]); } printf("\n"); for(i=1;i<=m;i++) { pri

C program for Matrix chain multiplication

Image
#include<stdio.h>#define MAX 10void tree(int,int);int s[MAX][MAX];void main(){int m[MAX][MAX],p[MAX];int i,j,s1,k,l,q;printf("enter the size of P:");scanf("%d",&s1);printf("enter the p values:\n");for(i=0;i<=s1;i++)scanf("%d",&p[i]);for(i=0;i<=s1;i++)printf("%d",p[i]);for(i=1;i<=s1;i++){for(j=1;j<=s1;j++){m[i][j]=0;}}for(l=2;l<=s1;l++){for(i=1;i<=s1-l+1;i++){j=i+l-1;m[i][j]=100000000;for(k=i;k<=j-1;k++){q=m[i][k]+m[k+1][j]+(p[i-1]*p[k]*p[j]);if(q<m[i][j]){m[i][j]=q;s[i][j]=k;}}}}printf("\nMatrix M :\n");for(l=2;l<=s1;l++) { for(i=1;i<=s1-l+1;i++) { j=i+l-1; printf(" %d ",m[i][j]); }printf("\n\n");}printf("\nMatrix S :\n ");for(l=2;l<=s1;l++) { for(i=1;i<=s1-l+1;i++) { j=i+l-1; printf(" %d ",s[i][j]); }printf("\n");}tree(i-1,j);}void tree(int x,int y){if(x==y)printf("A%d",x);else{printf("(");tr

Comparing two locations for accuracy in Android

If you are developing a android application which uses android Location Service and you need to find best estimated location by comparing two locations, where one location is the currently obtained location and other is from the location fix then this post contains answer to your question. The below mentioned code contains a method isBetterLocation which will compare two locations and will return true if current location is more accurate than location fix . You might expect that the most recent location fix is the most accurate. However, because the accuracy of a location fix varies, the most recent fix is not always the best. isBetterLocation method follows following criteria for choosing location fixes, you vary this criteria depending on the use-cases of the application and field testing. Check if the location retrieved is significantly newer than the previous estimate. Check if the accuracy claimed by the location is better or worse than the previous estimate. Check which provider