Skip to main content

Posts

Showing posts from February, 2014

Prim’s Algorithm For Minimum Spanning Tree In C Programming

#include #include #define size 20 #define infi 9999 void prim(int L[][size],int node) { int T[size],i,j,k; int min_dist,v1,v2,total=0; for(i=1;i<= node;i++) T[i]=0; printf("\n--------------------------------"); printf("\n The minimum spanning tree is:"); printf("\n--------------------------------"); T[1]=1; for(k=2;k<= node;k++) { min_dist=infi; for(i=1;i<= node;i++) { for(j=1;j<= node;j++) { if(L[i][j]&&((T[i]&&!T[j])||(!T[i]&&T[j]))) { if(L[i][j] < min_dist) { min_dist=L[i][j]; v1=i; v2=j; } } } } printf("\n Edge (%d %d)and weight= %d",v1,v2,min_dist); T[v1]=T[v2]=1; total=total+min_dist; } printf("\n -----------------------------------"); printf("\n total path Length is= %d",total); printf("\n ------------------------------------"); } void main() { int L[size][size],node; int v1,v2,length,i,

Matrix Multiplication in C Programming

#include int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter the number of rows and columns of first matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for (c=0;c < m;c++) for (d=0;d < n;d++) scanf("%d",&first[c][d]); printf("Enter the number of rows and columns of second matrix\n"); scanf("%d%d", &p, &q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.\n"); else { printf("Enter the elements of second matrix\n"); for(c=0;c < p;c++) for (d=0;d < q;d++) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) { for ( k = 0 ; k < p ; k++ )

Bubble Sort in C Programming

#include< stdio.h> #include<conio.h> void display(int*,int); void bubble_sort(int*,int); void main() { int i,*a,n; clrscr(); printf("\n Enter number of element you want:"); scanf("%d",&n); for(i=0;i < n;i++) { printf("\n Enter element[%d]:",i); scanf("%d",&a[i]); } printf("\n------------------------------------------"); printf("\n Elements before sorting:"); display(a,n); printf("\n------------------------------------------"); bubble_sort(a,n); printf("\n------------------------------------------"); printf("\n Elements after sorting:"); display(a,n); printf("\n------------------------------------------"); getch(); } void bubble_sort(int *a,int n) { int pass,i,temp; for(pass=0;pass<= n-1;pass++) { for(i=0;i< n-pass-1;i++) { if(a[i]>=a[i+1]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } printf("\