Posts

binary search

#include<stdio.h> int binarysearch(int arr[],int target,int mid,int size,int *pass) {  int l=0;  int h=size-1;  while(l<=h)  {   int mid=(l+h)/2;      if(arr[mid]==target)   {    return 1;   }   else if(arr[mid]<target)   {    l=mid+1;    *pass=(*pass)+1;   }   else   {    h=mid-1;    *pass=(*pass)+1;   }  }  return -1; } int main() { int mid;  int pass=1;  int arr[]={10,20,30,40,50,60,70};  int size=sizeof(arr)/sizeof(arr[0]);  int target;  printf("enter the number to find\n");  scanf("%d",&target);  int ans=binarysearch(arr,target,mid,size,&pass);  if(ans==-1)  {   printf("data not found");     }  else   {   printf("\n data found at %d pass",pass);  } }

Min max

#include <stdio.h> void minmax(int a[], int i, int j, int *min, int *max, int *c) {     if (i == j)   {         *min = *max = a[i];     }   else if (i == j - 1   {         if (a[i] < a[j])    {             *min = a[i];             *max = a[j];         } else    {             *min = a[j];             *max = a[i];         }         (*c)++;     }   else   {         int mid = (i + j) / 2;         int min1, max1;         minmax(a, i, mid, min, max, c);         min1 = *min;         max1 = *max;         minmax(a, mid + 1, j, min, max, c);       ...

mergesort

#include <stdio.h> void merge(int a[], int low, int high, int mid, int *c) {     int i, j, k, b[10];     i = low;     j = mid + 1;     k = low;     while (i <= mid && j <= high)     {         (*c)++;         if (a[i] < a[j])         {             b[k] = a[i];             i++;         }         else         {             b[k] = a[j];             j++;         }         k++;     }     while (i <= mid)     {         b[k] = a[i];         i++;         k++;     }     while (j <= high)     {       ...

class net id ---------PK

#include<stdio.h> #include<math.h> int main() { char cl;   int b1,b2,b3,b4;   int d1,d2,d3,d4;   int bd1,bd2,bd3,bd4;   int s;   printf("\nEnter 1st byte : ");   scanf("%d",&b1);     printf("\nEnter 2nd byte : ");   scanf("%d",&b2);   printf("\nEnter 3rd byte : ");   scanf("%d",&b3);   printf("\nEnter 4th byte : ");   scanf("%d",&b4);   printf("\nEnter No. of subnets : ");   scanf("%d",&s);   if(b1>255 || b1<0 || b2>255 || b2<0 || b3>255 || b3<0 || b4>255 || b4<0)   {   printf("\nInvalid IP"); }   printf("IP address is : %d.%d.%d.%d",b1,b2,b3,b4);   if(b1>0 && b1<=127)   {   cl='A';   printf("\nClass A"); d1=255; d2=0; d3=0; d4=0; } else if(b1>127 && b1<=191)   {   cl='B';   printf("\nClass B"); d1=255; d2=255; d3=0; d4=0; } else ...

CRC receiver

// Include headers #include<stdio.h> #include<string.h> // length of the generator polynomial #define N strlen(gen_poly) // data to be transmitted and received char data[28]; // CRC value char check_value[28]; // generator polynomial char gen_poly[10]; // variables  int data_length,i,j; // function that performs XOR operation void XOR(){     // if both bits are the same, the output is 0     // if the bits are different the output is 1     for(j = 1;j < N; j++)     check_value[j] = (( check_value[j] == gen_poly[j])?'0':'1');      } // Function to check for errors on the receiver side void receiver(){ // get the received data     printf("Enter the received data: ");     scanf("%s", data);     printf("\n-----------------------------\n");     printf("Data received: %s", data); // Cyclic Redundancy Check     crc(); // Check if the remainder is zero to find the error ...

CRC sender

#include<stdio.h>     int arr[8],crc[12],gen[5]={1,0,0,1,1},temp[12];     int i,j,k,l;     void to_binary(int a)     {   for(i=6;i>=0;i--)   {      arr[i]=a%2;      a=a/2;   }   printf("\nThe binary equivalent of given character is :");   for(i=0;i<7;i++)   printf("%d",arr[i]);     }     void to_crc()     {  printf("\n\nFrame after adding 4 zeros at the end for Dataword: ");  for(i=0;i<11;i++)  {      if(i>6)      crc[i]=0;     else   crc[i]=arr[i];  }  for(i=0;i<11;i++)  {      printf("%d",crc[i]);      temp[i]=crc[i];  }    //EX-ORING  printf("\n\nFrame Generated after applying CRC Tech. : ");  k=0;  for(i=0;i<7;i++)  {      if(temp[i]==1)      {   for(j=i;j<...

switch configuration algo

Image