Posts

Program to find the shortest path

Program to find the shortest path #include " " #include " " #define INF 9999 void main( ) { int arr[4][4] ; int cost[4][4] = { 7, 5, 0, 0, 7, 0, 0, 2, 0, 3, 0, 0, 4, 0, 1, 0 } ; int i, j, k, n = 4 ; clrscr( ) ; for ( i = 0 ; i { for ( j = 0; j { if ( cost[i][j] == 0 ) arr[i][j] = INF ; else arr[i][j] = cost[i][j] ; } } printf ( "Adjacency matrix of cost of edges:\n" ) ; for ( i = 0 ; i { for ( j = 0; j printf ( "%d\t", arr[i][j] ) ; printf ( "\n" ) ; } for ( k = 0 ; k { for ( i = 0 ; i { for ( j = 0 ; j { if ( arr[i][j] > arr[i][k] + arr[k][j] ) arr[i][j] = arr[i][k] + arr[k][j]; ...

minimum cost of a spanning tree.

minimum cost of a spanning tree. #include "stdio.h" #include "conio.h" #include "alloc.h" struct lledge { int v1, v2 ; float cost ; struct lledge *next ; } ; int stree[5] ; int count[5] ; int mincost ; struct lledge * kminstree ( struct lledge *, int ) ; int getrval ( int ) ; void combine ( int, int ) ; void del ( struct lledge * ) ; void main( ) { struct lledge *temp, *root ; int i ; clrscr( ) ; root = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ; root -> v1 = 4 ; root -> v2 = 3 ; root -> cost = 1 ; temp = root -> next = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ; temp -> v1 = 4 ; temp -> v2 = 2 ; temp -> cost = 2 ; temp -> next = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ; temp = temp -> next ; temp -> v1 = 3 ; temp -> v2 = 2 ; temp -> cost = 3 ; temp -> next = ( struct lledge * ) malloc ( sizeof ...

Breadth first search algorithm.

Breadth first search algorithm. #include "stdio.h" #include "conio.h" #include "alloc.h" #define TRUE 1 #define FALSE 0 #define MAX 8 struct node { int data ; struct node *next ; } ; int visited[MAX] ; int q[8] ; int front, rear ; void bfs ( int, struct node ** ) ; struct node * getnode_write ( int ) ; void addqueue ( int ) ; int deletequeue( ) ; int isempty( ) ; void del ( struct node * ) ; void main( ) { struct node *arr[MAX] ; struct node *v1, *v2, *v3, *v4 ; int i ; clrscr( ) ; v1 = getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = getnode_write ( 3 ) ; v2 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = getnode_write ( 4 ) ; v2 -> next = v3 = getnode_write ( 5 ) ; v3 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = getnode_write ( 6 ) ; v2 -> next = v3 = getnode_write ( 7 ) ; v3 -> next = NUL...

depth first search algorithm.

depth first search algorithm. #include "stdio.h" #include "conio.h" #include "alloc.h" #define TRUE 1 #define FALSE 0 #define MAX 8 struct node { int data ; struct node *next ; } ; int visited[MAX] ; void dfs ( int, struct node ** ) ; struct node * getnode_write ( int ) ; void del ( struct node * ) ; void main( ) { struct node *arr[MAX] ; struct node *v1, *v2, *v3, *v4 ; int i ; clrscr( ) ; v1 = getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = getnode_write ( 3 ) ; v2 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = getnode_write ( 4 ) ; v2 -> next = v3 = getnode_write ( 5 ) ; v3 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = getnode_write ( 6 ) ; v2 -> next = v3 = getnode_write ( 7 ) ; v3 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[3] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next...

Program that creates random numbers in a given file

Program that creates random numbers in a given file. #include "stdio.h" #include "conio.h" #include "stdlib.h" void main( ) { FILE *fp ; char str [ 67 ] ; int i, noofr, j ; clrscr( ) ; printf ( "Enter file name: " ) ; scanf ( "%s", str ) ; printf ( "Enter number of records: " ) ; scanf ( "%d", &noofr ) ; fp = fopen ( str, "wb" ) ; if ( fp == NULL ) { printf ( "Unable to create file." ) ; getch( ) ; exit ( 0 ) ; } randomize( ) ; for ( i = 0 ; i { j = random ( 1000 ) ; fwrite ( &j, sizeof ( int ), 1, fp ) ; printf ( "%d\t", j ) ; } fclose ( fp ) ; printf ( "\nFile is created. \nPress any key to continue." ) ; getch( ) ; }

External Sorting.

External Sorting. #include "stdio.h" #include "conio.h" #include "stdlib.h" void shownums ( char * ) ; void split ( char * ) ; void sort ( char * ) ; void main( ) { char str[67] ; clrscr( ) ; printf ( "Enter file name: " ) ; scanf ( "%s", str ) ; printf ( "Numbers before sorting:\n" ) ; shownums ( str ) ; split ( str ) ; sort ( str ) ; printf ( "\nNumbers after sorting:\n" ) ; shownums ( str ) ; getch( ) ; } /* Displays the contents of file */ void shownums ( char *p ) { FILE *fp ; int i ; fp = fopen ( p, "rb" ) ; if ( fp == NULL ) { printf ( "Unable to open file." ) ; getch( ) ; exit ( 0 ) ; } while ( fread ( &i, sizeof ( int ), 1, fp ) != 0 ) printf ( "%d \t", i ) ; fclose ( fp ) ; } /* Splits the original file into two files */ void split ( char *p ) { FILE *fs, *ft ; ...

Merge Sort.

Merge Sort. #include #include void main( ) { int a[5] = { 11, 2, 9, 13, 57 } ; int b[5] = { 25, 17, 1, 90, 3 } ; int c[10] ; int i, j, k, temp ; clrscr( ) ; printf ( "Merge sort.\n" ) ; printf ( "\nFirst array:\n" ) ; for ( i = 0 ; i printf ( "%d\t", a[i] ) ; printf ( "\n\nSecond array:\n" ) ; for ( i = 0 ; i printf ( "%d\t", b[i] ) ; for ( i = 0 ; i { for ( j = i + 1 ; j { if ( a[i] > a[j] ) { temp = a[i] ; a[i] = a[j] ; a[j] = temp ; } if ( b[i] > b[j] ) { temp = b[i] ; b[i] = b[j] ; b[j] = temp ; } } } for ( i = j = k = 0 ; i { if ( a[j] c[i++] = a[j++] ; else c[i++] = b[k++] ; if ( j == 5 || k == 5 ) brea...