Skip to main content

Posts

Showing posts from 2017

Write a C program for binary search in sorted array.

Solution:
#include <stdio.h>

int main()
{
   int c, first, last, middle, n, search, array[100];

   printf("Enter number of elements\n");
   scanf("%d",&n);

   printf("Enter %d integers\n", n);

   for (c = 0; c < n; c++)
   {
      scanf("%d",&array[c]);
   }

   printf("Enter value to find\n");
   scanf("%d", &search);

   first = 0;
   last = n - 1;
   middle = (first+last)/2;

   while (first <= last)
   {
      if (array[middle] < search)
      {
first = middle + 1;
      }
      else if (array[middle] == search)
      {
printf("%d found at location %d.\n", search, middle);
break;
      }
      else
last = middle - 1;
//       printf("%d\n",middle);
      middle = (first + last)/2;
  //     printf("%d\n",middle);
   }
   if (first > last)
      printf("Not found! %d is not present in the list.\n", search);

   return 0;
}

Write a C program for linear hashig.

Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,aa,data[10]={0},ele,j;
clrscr();
a:
printf("Enter an Element::");
scanf("%d",&ele);
j=ele%10;
c:
if(data[j]==0)
{
data[j]=ele;


}
else
{
j=j+1;
goto c;
}
printf("\n1:insert 2:display\nEnter choise::");
scanf("%d",&aa);
switch(aa)
{

case 1:goto a;

default:goto b;


}
b:
printf("\nHash table\n");
for(i=0;i<10;i++)
{
printf("%d  %d\n",i,data[i]);

}

       getch();

}

Write a c program for Merge short.

Solution:
#include <stdio.h>
#include<conio.h>
#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[11];

void merging(int low, int mid, int high) {
   int l1, l2, i;

   l1 = low;
   l2 = mid + 1;
   for(i = low; l1 <= mid && l2 <= high; i++) {
      if(a[l1] <= a[l2])
      {
b[i] = a[l1];
l1++;
      }
      else
         b[i] = a[l2++];
   }

   while(l1 <= mid)    
      b[i++] = a[l1++];

   while(l2 <= high)   
      b[i++] = a[l2++];

   for(i = low; i <= high; i++)
      a[i] = b[i];
}

void sort(int low, int high) {
   int mid;

   if(low < high) {
      mid = (low + high) / 2;
      sort(low, mid);
      sort(mid+1, high);
      merging(low, mid, high);
   } else { 
      return;
   }   
}

int main() { 
   int i;

   printf("List before sorting\n");

   for(i = 0; i <= max; i++)
      printf("%d ", a[i]);

   sort(0, max);

   printf("\nList after sorting\n");

   for(i = 0; i <= max; i++)
      printf("%d ", a[i]);

Entering elements into the stack from user and transfering into two stack with equal height.

Solution:
#include<stdio.h>
#include<conio.h>
#define maxsize 50
struct stack
{
 int stk[maxsize];
 int top;
};
typedef struct stack STACK;
STACK s1,s2;
int i,n;
int mid;
void array();
void push1();
void push2();
void traverse1();
void traverse2();
void main()
{
 s1.top=-1;
 s2.top=-1;
 clrscr();

     array();
     push1();
     push2();
     traverse1();
     traverse2();

}
void array()
{
int arr[50];
  printf("\n enter the no of array ele to be stored ::");
  scanf("%d",&n);
  mid=n/2;
  printf("\n enter the array elements ::\n");
 for(i=0;i<n;i++)
 {
    printf("\n enter arr[%d] ::\t",i);
    scanf("%d",&arr[i]);
 }

}
void push1()
{
   int arr[50];
   if(s1.top== (maxsize-1))
   {
    printf("\n stack is full ");
   }
   else
   {
    for(i=0;i<mid;i++)
   {
       s1.top=s1.top+1;
       s1.stk[s1.top]=arr[i];
   }
   }
}
void push2()
{  int arr[50];
   if(s2.top== (maxsize-1))
   {
    printf("\n stack is full ");
   }
   else
   {
    for(i=mid;i<…

C program to implement BFS(breadth-first search) and DFS(depth-first search) algorithm for directed and undirected graph.

solution:
#include<stdio.h>
#include<conio.h>
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();

void main()
{
int n,i,s,ch,j;
char c,dummy;
clrscr();
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("THE ADJACENCY MATRIX IS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}

do
{
for(i=1;i<=n;i++)
vis[i]=0;
printf("\nMENU");
printf("\n1.B.F.S");
printf("\n2.D.F.S");
printf("\nENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);

switch(ch)
{
case 1:bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? …