Friday, June 28, 2013

C Program for finding Sqrt upto 3 decimal point

A c program for finding Sqrt upto 3 decimal point........Paper and Pencil way.
#include<stdio.h>
#include<stdlib.h>
void Sqrt(int n)
{
double R=0,d=0;
int i=0,p,temp=0,count=0;
 if(n<=0){printf("Not possible ");return ;}
 
 for(i=0;i<=n/2+1;i++)
 {   if(d==0)
      p=i*i;
      else
        p=(d*10+i)*i;
      if(p>n)
  {  
    R=(double)R*10+temp;
    n=n-(d*10+temp)*temp;
    d=(d*10+temp)+temp;
    if(n<=d&&count<3)
    {
     n=n*100;
     count++;
    } 
    else
     break;
      i=0;
  }
     temp=i; 
     }
printf("final result=%g",(float)R/1000);
}
int main(void)
{int i=0,data;
printf("Enter the data for Sqrt=");
scanf("%d",&data);
Sqrt(data);
return 0;
}
There is a better way by Newton Raphson Method....one may google it. It's my own work that's why i am posting. i don't think it's bad to know the basic.

Tuesday, June 25, 2013

A program for reversing each k-nodes of blocks in the program.....Tested and working

//A program for reversing each k-nodes of blocks in the program.....Tested and working
input:- 1 2 3 4 5 6 7 8 9 10 11
for k=3
output:- 3 2 1 6 5 4 9 8 7 10 11

#include<stdio.h>
#include<stdlib.h>
typedef struct Linkedlist
{
int data;
struct Linkedlist *next;
} List;

void InsertNode(List **Head,int data)
{
 List *temp,*ptr;
 temp=(List*)malloc(sizeof(List));
 temp->data=data;
 temp->next=NULL;
 if(!*Head)
  {
  *Head=temp;
  return ;
  }
  ptr=*Head;
  while(ptr->next)
  ptr=ptr->next;
  ptr->next=temp;
}
void Traverse(List *Head)
{
if(!Head)return;
printf("%d ",Head->data);
Traverse(Head->next);
}
void Kreverse(List **Head,int k)
{
List *prev,*First,*Last,*ptr,*temp;int i=1;
    First=ptr=*Head;
i=k-1;
if(k==1||!*Head)return ;
while(1)
{
  while(ptr->next&&i)
  {
  ptr=ptr->next;
  i--;
  }

  if(i)
  return;
  if(*Head==First)
  *Head=ptr;
  else
  {
   First->next=ptr;
        First=Last;
  }
  prev=Last=ptr->next;
  ptr=First;

  i=k-1;
  while(i)
  {
  temp=ptr;
  ptr=ptr->next;
  temp->next=prev;
  prev=temp;
  i--;
  }
  ptr->next=prev;
  ptr=Last;
if(!ptr)return;
i=k-1;
}
}
int main(void)
{
List *Head=NULL;
int i=0,data,n;
printf("Enter the no. of input you want to enter\n");
scanf("%d",&n);
printf("\nEnter the data\n");
while(i<n)
{
scanf("%d",&data);
InsertNode(&Head,data);
i++;
fflush(stdin);
}
printf("List is\n");
Traverse(Head);
printf("\nEnter the value for k reverse\n");
int k;
scanf("%d",&k);
printf("\nReversed list is\n");
Kreverse(&Head,k);
Traverse(Head);
return 0;
}

Amazon Coding preparation test.Run Length Encoding and Decoding known as RLECompress


  1. Let us try Run Length Encoding and Decoding Today. If you have consecutive letters, then you can replace them with a macro that denotes the value. Macro starts with a Hash (#), followed by the letter that is repeated, followed by two digits representing repetition count. If RLE does not result in a compression, leave the text as is. Input string can be any letter, or number, or the symbols “<space> ~`:;'"!@#$%^&*()_-+=[]{}\|<>,.?/”
  2. String RLECompress(String input)
  3. Input: A, Output: A
  4. Input: AAAAAAABC, output #A07BC
    Note:-There are cases which do not looks obvious has been included take care of it.
    // Here is my program....... your comments will be welcome.
    #include <stdio.h>
    #include <string.h>
    void RLECompress(char Str[])
    {   char Result[1000];
        int i=1,temp=Str[0],count=0,n,j=0;
        n=strlen(Str);
        do
        {   if(Str[i]>=32&&Str[i]<=126||Str[i]=='\0'); 
           else if(temp!='\0'){printf("invalid string");return ;}
           if(Str[i]==temp&&count<100) count++;
            else
            {
                if(count!=0)
                {Result[j++]='#';
                     Result[j++]=temp;
                     Result[j++]=(count+1)/10+48;
                     Result[j++]=(count+1)%10+48;
                  }
                 else if(temp=='#')
                 {
                 Result[j++]='#';
                 Result[j++]=temp;
                 Result[j++]='0';
                 Result[j++]=count+49;
                 }
                 else 
                 {Result[j++]=temp;}
            count=0;
            temp=Str[i];
            }
            i++;
            
        }while(i<=n);
    Result[j]='\0';

    int m=strlen(Result);
    if(n>=m)
    printf("%s",Result);
    else
    printf("%s",Str);
    }

    int main(void)
    {
    char Str[]="AAAAAAAAAAAA~`:;'!!!!\"!!@#$%^&*()_-+=[]{}\|<>,.?/CDD";
    RLECompress(Str);
    return 0;
    }

Sunday, June 23, 2013

Working program for Palindrome finding using Singly Linkedlist


// Working program for Palindrome using Singly LinkedList
#include <stdio.h>
#include <stdlib.h>
typedef struct Linkedlist
{
    int data;
    struct Linkedlist *next;
}List;

void Insertnode(List **head,int data)
{
List *ptr=*head,*temp;
temp=(List*)malloc(sizeof(List));
if(!temp)return;
temp->data=data;
temp->next=NULL;
if(!ptr)
*head=temp;
else
{
 while(ptr->next)
  ptr=ptr->next;
 ptr->next=temp;
 }
}

List *Partition(List **Head)
{
    if(!*Head) return;
    List *temp,*sptr=*Head,*head=*Head;
    while(head)
    {
    head=head->next;
    if(!head){temp->next=NULL; return sptr;}
 
    head=head->next;
    temp=sptr;
    sptr=sptr->next;
 
    }
temp->next=NULL;
return sptr;
}
List *Reverse(List *head)
{
List *temp,*prev=NULL,*ptr=head;
while(ptr->next)
{   temp=ptr;
    ptr=ptr->next;
    temp->next=prev;
    prev=temp;
}
ptr->next=prev;
return ptr;
}

void Matching(List *head,List *tail)
{
    if(!head||!tail)
    return;
    if(head->data==tail->data||head->data+32==tail->data)
    {head=head->next;
     tail=tail->next;
    }
    else
    {printf("\nNot a palindrome");return ;}
 
    while(head->data==tail->data)
   {
       head=head->next;
       if(head==NULL) {printf("\nPalindrome\n");return;}
       tail=tail->next;
   
   }
   printf("\nNot a palindrome\n");
}

void Palindrome(List *head)
{  List *tail=NULL;
    if(!head)return ;
    tail=Partition(&head);
    tail=Reverse(tail);
    Matching(head,tail);
}


main()
{
 List *Head=NULL;
 int i=0,data;

 while(i<7)                  // You may change if no. of character increases as per.
 {
 scanf("%c",&data);
 Insertnode(&Head,data);
 i++;
 }
Palindrome(Head);
}

Saturday, June 22, 2013

Palindrome checking through XOR Linked List


An efficient way to write a program for checking whether it is palindrome or not......Here i am using XOR linked list for this purpose.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>    // For supporting uintptr_t
typedef struct Linkedlist    // structure creation
{
int data;
struct Linkedlist *next;
}xorll;

xorll *Insertnode(xorll *prev,xorll *cur,int data)  // Node will be created here
{ xorll *new;
 new=(xorll*)malloc(sizeof(xorll));
 new->data=data;
 new->next=prev;
 if(!cur);     // if first node is null
 else if(!prev)  // if it is second node
 {
  cur->next=(uintptr_t)prev^(uintptr_t)new;
  new->next=cur;
 }
 else    // for general cases (more than two nodes)
{
  prev->next=(uintptr_t)prev->next^(uintptr_t)new;
}
return new;
}

void Traverse(xorll *ptr)  // For traversing the List .....Added for clear picture
{
xorll *prev=NULL,*temp;
if(!ptr) {printf("Nothing to print there\n");return;}  // if first node is null
do                                      // for general cases
{   temp=ptr;
    printf("%c ",ptr->data);             // I have assumed character string as input
    ptr=(uintptr_t)prev^(uintptr_t)ptr->next;
     prev=temp;
  }while(ptr!=NULL);  // if it is last node get out from here you are done
 
}

void Palindrome(xorll *headptr,xorll *tailptr)  // The actual function for checking palindrome
{
  xorll *headprev=NULL,*tailprev=NULL,*temph,*tempt;
  if(!headptr) {printf("Empty list\n");return;}   // if any of the means no List exists
//Now we have to check for lower and upper case letters
  while(headptr->data==tailptr->data||headptr->data+32==tailptr->data||headptr->data==tailptr->data+32)
  {
      temph=headptr;
      tempt=tailptr;
    if(headptr==tailptr)
      {
          printf("palindrome\n");
          return;
      }
      headptr=((uintptr_t)headprev^(uintptr_t)headptr->next);
      if(headptr==tailptr)
      {
          printf("Palindrome\n");
          return;
      }
   
      tailptr=(uintptr_t)tailprev^(uintptr_t)tailptr->next;
      headprev=temph;
      tailprev=tempt;
  }
printf("Not a palindrom\n");
}

int main(int argc, char ** argv)
{
xorll *Head,*Tail;
Head=Tail=NULL;
int i=0,data;
while(i<5)    // Link creation, for 5 characters you can make it large
{
scanf("%c",&data);
if(i==0)          // for first node
Head=Tail=Insertnode(Head,Tail,data);
else if(i==1)    // for second node
Tail=Insertnode(NULL,Tail,data);
else                // for general case
Tail=Insertnode(Tail,Tail,data);
i++;
}
printf("\nFrom front of the list\n");
Traverse(Head);
printf("\nFrom Back of the list\n");
Traverse(Tail);
printf("\n\n");
Palindrome(Head,Tail);
return 0;
}