Posts

Showing posts from 2017

Rotate a matrix 270 degree AntiClockWise

#include<stdio.h> void printMt(int mat[][4]) {     int i, j;     for (i = 0; i < 4; i++) {         for (j = 0; j < 4; j++) {             printf("%d ", mat[i][j]);         }         printf("\n");     } } void AntiClock90(int mat[][4]) {     int i, j, t;     for (i = 0; i < 4 / 2; i++) {         for (j = i; j < 4; j++) {             t = mat[4 - i - 1][j];             mat[4 - i - 1][j] = mat[i][j];             mat[i][j] = t;         }     }     printf("90 anticlock\n");     printMt(mat); } void Anti270(int mat1[][4]) {     int i,j,t;     for(i=0; i<4/2; i++)     {         for( j=i; j<4; j++)         {             t=mat1[j][4-i-1];             mat1[j][4-i-1]=mat1[j][i];             mat1[j][i]=t;         }     }     printf("270 anticlock \n");     printMt(mat1); } int main() {     int mat[4][4]= {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};  

Binary Tree Creation Code Using C

#include<stdio.h> #include<stdlib.h> struct node { int key; struct node *left; struct node *right; }; struct node *createNode() { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->left=NULL; temp->right=NULL; return temp; } struct node *queue[100]; int front=-1,rear=-1; int isEmptyt() {     if(front==-1||front==rear+1)     {  return 0;     }     else     {     return 1; } } void Enqueue(struct node *temp) { if(front==-1) { front=0; } rear=rear+1; queue[rear]=temp; } struct node *Dequeue() { int t; if(front==-1||front==rear+1) {  return 0; } t=front; front++; return queue[t]; } void Delete() { front=-1; rear=-1; } struct node* BinaryTree(struct node *root,int key) {      struct node *temp=createNode();      temp->key=key;      if(root==NULL)      {       root=temp;       return root; }      else      {       Enqueue(root);       while(isE

EASIEST WAY FOR MERGE TWO LINKLIST

/* Link list Node struct Node {     int data;     Node* next; }; */ Node* SortedMerge(Node* head1,   Node* head2) {     struct  Node *head,*last;     if(head1==NULL)     {         return head2;     }     if(head2==NULL)     {         return head1;     }     if(head1&&head2)     {         if(head1->data<head2->data)         {             last=head1;             head1=last->next;         }         else         {             last=head2;             head2=last->next;         }     }     head=last;     while(head1&&head2)     {         if(head1->data<head2->data)         {             last->next=head1;             last=head1;             head1=last->next;         }         else         {             last->next=head2;             last=head2;             head2=last->next;         }     }     if(head1)     {         last->next=head1;     }     if(head2)     {         last->next=head2

BINARY SEARCH TREE CREATION THROUGH C

#include<stdio.h> #include<stdlib.h> struct tree {   int data;   struct  tree *left;   struct   tree *right; }; int pre(struct tree *root) {    if(root!=NULL)    {     printf("%d  ",root->data);     pre(root->left);     pre(root->right);    } } struct tree *Insert_node(struct tree *root) {     struct tree *temp,*ptr,*prev=NULL; temp=(struct tree *)malloc(sizeof(tree)); int n; printf("enter data"); scanf("%d",&n); temp->data=n; temp->left=NULL; temp->right=NULL; if(root==NULL) {   root=temp; } else {      ptr=root; while(ptr!=NULL) { prev=ptr; if(ptr->data>temp->data) { ptr=ptr->left; }        else if(ptr->data<temp->data) { ptr=ptr->right; } else if(ptr->data==temp->data) {  printf("duplticate key");  break; }  }   if(temp->data<prev->data) { prev->left=tem

Largest Sum Contiguous Subarray. Kadane's Algorithm

// C++ program to print largest contiguous array sum #include<iostream> #include<climits> using namespace std; int maxSubArraySum(int a[], int size) { int max_so_far = INT_MIN, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } /*Driver program to test maxSubArraySum*/ int main() { int a[] = {-2, -3, -4, -6, -7, -9, -90, -30}; int n = sizeof(a)/sizeof(a[0]); int max_sum = maxSubArraySum(a, n); cout << "Maximum contiguous sum is " << max_sum; return 0; }

Static Keyword in C

In C, static variables can only be initialized using constant literals. For example, following program fails in compilation. See this for more details. #include<stdio.h> int initializer( void ) {      return 50; }     int main() {      static int i = initializer();      printf ( " value of i = %d" , i);      getchar ();      return 0; } Output In function 'main': 9:5: error: initializer element is not constant static int i = initializer(); ^

void pointer in C

void pointer in C A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type. int a = 10; char b = 'x' ;   void *p = &a;  // void pointer holds address of int 'a' p = &b; // void pointer holds address of char 'b' Advantages of void pointers: 1) malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type (just because of void *) int main(void) {      // Note that malloc() returns void * which can be      // typecasted to any type like int *, char *, ..      int *x = malloc(sizeof(int) * n); } Note that the above program compiles in C, but doesn’t compile in C++. In C++, we must explicitly typecast return value of malloc to (int *). 2) void pointers in C are used to implement generic functions in C. For example compare function whic

Check current time using C program

#include<stdio.h> #include<time.h> int main() {      struct tm *local,*gm;      time_t t;      t=time(NULL);      local=localtime(&t);      printf("time-->%d:%d:%d",local->tm_hour,local->tm_min,local->tm_sec); }

find even odd using & operator

int main() { int i =0; for(i=1;i<10;i++) { (i& 1)? printf("Odd"): printf("Even");   printf("\n"); } return 0; }

program to convert binary to decimal using c

#include<stdio.h> int main() {     int n,rem,d,j=1,dec=0;     printf("Enter the number in binary");     scanf("%d",&n);    while(n>0)    {        rem=n%10;        d=rem*j;        dec=dec+d;       j*=2;       n=n/10;    }    printf("decimal is=%d",dec); }

c program for find leap year using c

#include<stdio.h> int main() {   int year;   printf("enter year");   scanf("%d",&year);   if(year%100!=0)   {           if(year%4==0||year%400==0)      {         printf("leap year");      }      else      {       printf("not leap year");      }       } }

BYTE STUFFING PROGRAM USING C

For Hadoop Tutorial Click Here #include<stdio.h> #include<string.h> void main(){   char frame[50][50],str[50][50]; char flag[10]; strcpy(flag,"flag"); char esc[10]; strcpy(esc,"esc"); int i,j,k=0,n; strcpy(frame[k++],"flag"); printf("Enter no.of String :\t"); scanf("%d",&n); printf("Enter String \n"); for(i=0;i<=n;i++)   { gets(str[i]);   } printf("You entered :\n"); for(i=0;i<=n;i++)     { puts(str[i]);   } printf("\n"); for(i=1;i<=n;i++)   {     if(strcmp(str[i],flag)!=0 && strcmp(str[i],esc)!=0)             {                    strcpy(frame[k++],str[i]);     }     else {           strcpy(frame[k++],"esc");           strcpy(frame[k++],str[i]);       }   } strcpy(frame[k++],"flag"); //frame[k++]='\0'; printf("------------------------------\n"); printf("Byte stuffing at sender s

Hamming code program using c

#include<stdio.h> #include<math.h> int main() {  int data[20],r,n,i=1,l,t,sum,p,c,j;    printf("\n enter howw many bit in data");    scanf("%d",&n);      for(i=0;i<n;i++)    {     l=n+i+1;     t=pow(2,i);     if(l<=t)     {     break;     }    }    r=i;    printf("\nr= %d \n",r);    for(i=1;i<n+r;i++)    {    data[i]=0;    }    for(i=0;i<r;i++)    {      t=pow(2,i); data[t] =-1;    }    printf("\n enter data bit");    for(i=1;i<=n+r;i++)    {   if(data[i]==-1) {  data[i]=0; } else { scanf("%d",&data[i]); }    }    for(i=n+r;i>=1;i--)    {     printf("\t %d",data[i]);    }    sum=0;    c=0;    for(i=0;i<r;i++)    {     p=pow(2,i);     printf("\n"); for(j=p;j<=n+r;j++)     {      c++;         sum=(sum==data[j]?0:1);   if(c==p) {

CHECKSUM USING C

#include<stdio.h> static int c; int Add(int a,int b) { if(a==0&&b==0&&c==1) {     c=0;     return 1; } if(a==0&&b==1&&c==1) { c=1; return 0; } if(a==0&&b==0&&c==0) { c=0; return 0; } if(a==1&&b==1&&c==1) { c=1; return 1; } if(a==1&&b==0&&c==0)   {   c=0;   return 1;   } if(a==1&&b==0&&c==1) { c=1; return 0; } if(a==0&&b==1&&c==0) {  c=0; return 1; } if(a==1&&b==1&&c==0) {   c=1;return 0; } } int main() { int d[32]={1,1,1,0,0,1,1,1,1,1,0,1,1,1,0,1,0,0,1,1,1,0,0,1,1,0,1,0,1,0,0,1},n=32,t; int d1=8,i,j; int carry[8]={0,0,0,0,0,0,0,0}; int checksum[8]={0}; for(i=0;i<n;i=i+d1) {   for(j=7;j>=0;j--) { checksum[j]=Add(d[i+j],checksum[j]); } while(c!=0) {     for(j=7;j>=0;j--) { checksum[j]=Add(carry[j],c

Cyclic redundancy check program using c

#include<stdio.h> void main() {  int Data[100],n,Div[20],d,i,j,t[20],t1,k;  printf("\n enter how many bit in data and div");  scanf("%d %d",&n,&d);  int p[20];  printf("\n enter data bit");  for(i=0;i<n;i++)  {   scanf("%d",&Data[i]);   }   printf("i==%d",i);  for(j=i;j<n+d-1;j++)  {   Data[j]=0;   }   printf("\n enter div bit");  for(i=0;i<d;i++)  {   scanf("%d",&Div[i]); p[i]=0;  }  for(i=0;i<n+d-1;i++)  {   printf("\t %d",Data[i]);   }  t1=n+d-1;  i=0;     while(i<t1)    {                 j=0;                                         while(p[j]!=1&&j<d) { j++; } k=0; for(k;j<d;k++) {  t[k]=p[j]; j++; } while(k<d) { t[k]=Data[i]; i++; k

circle and line using java

Image
import java.util.*; import javax.swing.JFrame; import java.lang.*; import java.awt.*; import java.util.Scanner; class Demo1 extends Canvas {   int x, y;  public void paint(Graphics g)   {    Circle(g,50,500,200);    Circle(g,8,445,200);    Circle(g,8,555,200);    Circle(g,8,470,180);    Circle(g,8,530,180);    Smile(g,40,500,200);    line(g,500,170,500,220);    line(g,500,250,500,600);    line(g,600,400,500,300);    line(g,400,400,500,300);    line(g,600,600,500,500);    line(g,500,500,400,600);     }  public void line(Graphics g,int x0,int y0,int x1,int y1)  {         int dx,dy;         float Xinc,Yinc,x,y,steps;         dy=y1-y0;         dx=x1-x0;         g.fillOval(x0,y0,4,4);         if(dy>dx)         {             steps=Math.abs(dy);         }         else         {             steps=Math.abs(dx);         }         x=x0;         y=y0;         Yinc=dy/steps;         Xinc=dx/steps;         while(steps!=0)         {             ste