Sunday, 21 June 2015

Code Test 1

Predict the output of this Program :-

int** performOps(int **A, int m, int n, int *len1, int *len2) {
int i, j;
*len1 = m;
*len2 = n;
int **B = (int **)malloc((*len1) * sizeof(int *));
for (i = 0; i < *len1; i++) {
B[i] = (int *)malloc((*len2) * sizeof(int));
}
 
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
B[i][n - 1 - j] = A[i][j];
}
}
return B;
}
Assume m=3, n=4, and A : [[1,2,3,4], [5,6,7,8], [9,10, 11, 12]] 
What would be the output of the following call :

int len1, len2; int **B = performOps(A, m, n, &len1, &len2); int i, j; for (i = 0; i < len1; i++) { for (j = 0; j < len2; j++) { printf("%d ", B[i][j]); } }

Saturday, 20 June 2015

Finding the largest palindrome in a string

This cpp code finds the starting location of the largest palindrome in a string and also finds that string.

// finding the largest palindrome in a string
#include<iostream>
#include<cstring>
#include<conio.h>

using namespace std;
int main()
{
    char string[]="hxkahssjaisjaiomadamjaisjais"; // taking string to check
    int start=0;                                                         // start marker
    int end=0;                                                          // end marker
    int i;                                                      
    int left;                                                              // left of the starting position i.e i
    int right;                                                           // right of the starting position i.e i
    int c;
    int maxsize=0;                                                 /* getting the max size of the
    int loc=0;                                                              palindrome string */
    for(i=0;i<strlen(string)-1;i++)                        // staring from the index i and looping
    {
        left=i;
        right=i;
        c=0;
        while(left>0)
        {
        if(string[left--]!=string[right++])               // checking the condition of failure
        {
            break;
        }
        c++;                                                           // incrementing when true
    }
        if(c>maxsize)                                           // checking for the new maxsize
    {
        maxsize=c;                                              // new maxsize
        loc=i;                                                       // new location
    }
    }
    start=loc-maxsize;                                       // start of the palindrome    
    end=loc+maxsize;                                       // end of the palindrome
    cout<<"The length of the maximum sized palindrome is"<<maxsize<<endl;
    cout<<"The location of the maximum sized palindrome is"<<(start+2)<<endl;
    for(int j=start+1;j<end;j++)
    {
        cout<<string[j];
    }
    getch();
    return 0;
}

Monday, 25 May 2015

Anagram strings

Anagram Strings:-- Two strings are said to be anagram if they consist of same set of characters with same frequency.
example :-- lab 
                   bla
These two strings are anagram

                   lab
                   bll
These are not anagrams


C code to find whether the strings are anagram or not

#include<stdio.h>
int anagram(char [],char []);
int main()
{
    char a[100];
    char b[100];
    int f=0;
    printf("Enter the first string\n");
    gets(a);
    printf("Enter the second string\n");
    gets(b);
    f=anagram(a,b);
    if(f==1)
    {
        printf("The entered strings are anagram\n");
    }
    else
    {
        printf("The entered strings are not anagram\n");
    }
    return 0;
}

int anagram(char a[],char b[])
{
    int a1[26]={0};
    int a2[26]={0};
    int i=0;
    while(a[i]!='\0')
    {
        a1[a[i]-'a']++;
        i++;
    }
    i=0;
    while(b[i]!='\0')
    {
        a2[a[i]-'a']++;
        i++;
    }
    int temp=0;
    for(temp=0;temp<26;temp++)
    {
        if(a1[temp]!=a2[temp])
        {
            return 0;
        }
    else
    {
        return 1;
    }
    }
}

Tuesday, 6 January 2015

merge new

In the below code snippet written in C++ node is defined to create nodes of Linked-List . The function insert1 is written to create an integer linked-list and while creating the list we have used counters in the main() (function) to identify the number of nodes in each linked-list . Similarly, we have created another function insert2 to create another linked-list of character data-type. Accordingly we have to merge the two lists alternatively . This is achieved by the function merge_alternate_new . Taking one element from integer list and other element from character list. 

 First of all we have checked the number of nodes of both the Linked-Lists and this function works for the lists having equal number of nodes . 

The two display functions are created two display the two initial lists and then display the final list. 

Input:--    1->2->3->4                                                                                                                              Input:--    a->s->d->f                                                                                                                            Output:-- 1->a->2->s->3->d->4->f


#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct node
{
    int di;
    char dc;
    node *link;
}*head1=NULL , *head2=NULL, *ptr2 , *ptr1, *nptr,*prev,*fptr2,*fptr1;

void insert1()
{
    int item;
    cout<<"enter Integer value :"<<endl;
    cin>>item;
    if(head1==NULL)
    {
    nptr=new node;
    nptr->di=item;
    nptr->link=NULL;
    head1=nptr;
    }
    else
    {
        ptr1=head1;
        nptr=new node;
      
        while(ptr1!=NULL)
        {  
            if(ptr1->link==NULL)
            {
                nptr->di=item;
                ptr1->link=nptr;
                nptr->link=NULL;
            }  
            ptr1=ptr1->link;
        }
    }
}


void insert2()
{
    char item;
    cout<<"Enter Character Value :"<<endl;
    cin>>item;
    if(head2==NULL)
    {
    nptr=new node;
    nptr->dc=item;
    nptr->link=NULL;
    head2=nptr;
    }
    else
    {
        ptr2=head2;
        nptr=new node;
      
        while(ptr2!=NULL)
        {  
            if(ptr2->link==NULL)
            {
                nptr->dc=item;
                ptr2->link=nptr;
                nptr->link=NULL;
            }  
            ptr2=ptr2->link;
        }
    }
}

void display2()
{
    ptr2=head2;
    cout<<endl<<"entered character list is :\n"<<endl;
    while(ptr2!=NULL)
    {
        cout<<ptr2->dc<<"-->";
        ptr2=ptr2->link;
    }
    cout<<"NULL"<<endl;
}


void display1()
{
    ptr1=head1;
    cout<<endl<<"entered Integer list is :\n"<<endl;
    while(ptr1!=NULL)
    {
        cout<<ptr1->di<<"-->";
        ptr1=ptr1->link;
    }
    cout<<"NULL"<<endl;
}

void merge_alternate_new(int c1, int c2) /* merging alternate elements of two linked list: */
{
    cout<<"After merging both lists:\n";
    ptr2=head2;
    ptr1=head1;
  
    if(c1==c2)
    {
        while(ptr1!=NULL)
        {
            cout<<ptr1->di<<"-->";
            cout<<ptr2->dc<<"-->";
            ptr1=ptr1->link;  
            ptr2=ptr2->link;
        }
        cout<<"NULL"<<endl;
    }
    else
    cout<<"\nBoth list should contain same number of elements \n";
  
}


int main()
{
    int choice,count=0,count1=0;
    while(1)
    {
    cout<<"ENTER YOUR CHOICE";
    cout<<" \n1. INSERTION IN FiRST LIST ";
    cout<<"\n2. INSERTION IN SECOND LIST ";
    cout<<"\n3. FOR DISPLAY LIST1 ";
    cout<<"\n4. DISPLAY LIST2 ";
    cout<<"\n5. MERGE BOTH LIST ";
    cout<<"\n6. EXIT\n "<<endl;
    cin>>choice;
    switch(choice)
    {
        case 1:
            insert1();
            count++;
            break;
        case 2:
            insert2();
            count1++;
            break;
        case 3:
            display1();
            break;
        case 4:
            display2();
            break;
        case 5:
            merge_alternate_new(count,count1);
            break;
        case 6:
            exit(0);
            break;
        default: cout<<"\n enter a valid choice ";
    }
    }
    getch();
    return 0;
}

 Output:

Output

Sunday, 4 January 2015

Creating a sorted list from two Linked-list

Hello readers , this code is written in c++ and it takes two linked-list as inputs and displays a linked-list after appending one linked-list at the end of the other and sorting it . In nutshell this code sorts the elements of two lists after appending them .  I have used create1 function to enter the elements in the first linked-list and create2 function to enter the elements in the second linked-list .  display1 function is used to display the elements of the first linked-list and display2 function is used to display the elements of second linked-list . Let's see the sort_logic function in the function we have first appended second list at the end of the first list and sorted the larger linked-list formed . For doing ptr1 is made to point to head1 and ptr2 is made to point to head2. We reach at the end of the first linked-list , repeatedly pointing to the next node until a NULL is encountered . On reaching the node having link part as NULL , it is made to point to the head2 i.e the starting part of the second linked-list , display1() is called to display the appended list . Then we took a temporary pointer of node type and made it to point at head1 . We see that now we have two while loops performing the conditional swapping .

The outer loop selects a node and compares the data part of the node with the data parts of all other nodes . The inner loop consists of the if clause and the else clause , in if clause a check is done . If the data part of t pointer is greater than the data part of ptr1 the swapping is done else the ptr1 is made to point to the next node .

Input:--      7->5->3->1                                                                                                                           Input:--      8->6->4->2                                                                                  Output:-    7->5->3->1->8->6->4->2  (After appending)                                                      Output:--   1->2->3->4->5->6->7->8  (After Sorting)

Code Snippet:--             

#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
    int data;
    node *link;
}*head1=NULL,*head2=NULL,*ptr1,*ptr2,*t;
void create1()
{
    int item;
    cout<<"enter the item you want to insert"<<endl;
    cin>>item;
    if(head1==NULL)
    {
        ptr1=new node;
        ptr1->data=item;
        ptr1->link=NULL;
        head1=ptr1;
    }
    else
    {
        ptr1=new node;
        ptr1->data=item;
        ptr1->link=head1;
        head1=ptr1;
    }
}

void create2()
{
    int item;
    cout<<"enter the item you want to insert"<<endl;
    cin>>item;
    if(head2==NULL)
    {
        ptr2=new node;
        ptr2->data=item;
        ptr2->link=NULL;
        head2=ptr2;
    }
    else
    {
        ptr2=new node;
        ptr2->data=item;
        ptr2->link=head2;
        head2=ptr2;
    }
}

void display1()
{
    ptr1=head1;
    while(ptr1!=NULL)
    {
        cout<<ptr1->data<<"\t";
        ptr1=ptr1->link;
    }
}

void display2()
{
    ptr2=head2;
    while(ptr2!=NULL)
    {
        cout<<ptr2->data<<"\t";
        ptr2=ptr2->link;
    }
}

void sort_logic()
{
    ptr1=head1;
    ptr2=head2;
    while(ptr1->link!=NULL)
    {
        ptr1=ptr1->link;
    }
    ptr1->link=ptr2;
    cout<<"Two list after appending"<<endl;
    display1();
    int temp=0;
    t=head1;
    while(t!=NULL)
    {
        ptr1=t->link;
        while(ptr1!=NULL)
        {
            if(t->data>ptr1->data)
            {
                temp=t->data;
                t->data=ptr1->data;
                ptr1->data=temp;
                ptr1=ptr1->link;
            }
            else
            ptr1=ptr1->link;
        }
        t=t->link;
    }
    cout<<endl;
    cout<<"list after sorting"<<endl;
    display1();
}

int main()
{
    int option;
    while(option!=6)
    {
        cout<<endl;
        cout<<"1. enter in the first list"<<endl;
        cout<<"2. enter in the second list"<<endl;
        cout<<"3. display the first list"<<endl;
        cout<<"4. display the second list"<<endl;
        cout<<"5. display the sorted list"<<endl;
        cout<<"6. to exit the program"<<endl;
        cout<<endl;
        cin>>option;
        switch(option)
        {
            case 1: create1();
            break;
            case 2: create2();
            break;
            case 3: display1();
            break;
            case 4: display2();
            break;
            case 5: sort_logic();
            break;
        }
    }
    getch();
    return 0;
}

OUTPUT                                 

 

Saturday, 3 January 2015

Sum of digits(updated)

Here is the simple code(in C++) to find the SUM OF DIGITS of the number, until the sum is not in a single digit.

Example:
 
 number = 56

 56==>  5+6=11
 11==>   1+1=2

and 2 is a single digit number.



Best example you can try in this code (Dev-C++) is : 2147483647  and observe the answer.



#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
    int n,nc,rem,sum=0;
    int sum1=10;
    cout<<"enter the number :";
    cin>>n;
    nc=n;

    while(sum1>9)
    {
        sum=0;
        rem=0;
    while(nc>0)
    {
        rem=nc%10;
        sum=sum+rem;
        nc=nc/10;
        sum1=sum;
    }
    nc=sum1;
    cout<<"\n the sum of digits of "<<n<<" is ="<<sum1;
    n=nc;
    }
    getch();
    return 0;
   
}

Output:

Output

Friday, 2 January 2015

Merge_alternate (two linked list)

 In the below code snippet written in C++ node is defined to create nodes of Linked-List . The function insert1 is written to create the first linked-list and while creating the list we have used counters in the main() (function) to identify the number of nodes in each linked-list . Similarly, we have created another function insert2 to create another linked-list . Accordingly we have to merge the two lists alternatively . This is achieved by the function merge_alternate . Taking one element from list1 and other element from list2 we create third list such that it consists of the elements/nodes of both the initial Linked-Lists . First of all we have checked the number of nodes of both the Linked-Lists and this function works for the lists having equal number of nodes .  The linking using pointers is done as shown in the diagram below . The two display functions are created two display the two initial lists and then display the final list . 

Input:--    1->2->3->4                                                                                                                              Input:--    5->6->7->8                                                                                                                             Output:-- 1->5->2->6->3->7->4->8

Code snippet:--

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct node
{
    int data;
    node *link;
}*head1=NULL , *head2=NULL, *ptr2 , *ptr1, *nptr,*prev,*fptr2,*fptr1;

void insert1()
{
    int item;
    cout<<"ENTER THE ITEM :"<<endl;
    cin>>item;
    if(head1==NULL)
    {
    nptr=new node;
    nptr->data=item;
    nptr->link=NULL;
    head1=nptr;
    }
    else
    {
        ptr1=head1;
        nptr=new node;
       
        while(ptr1!=NULL)
        {   
            if(ptr1->link==NULL)
            {
                nptr->data=item;
                ptr1->link=nptr;
                nptr->link=NULL;
            }   
            ptr1=ptr1->link;
        }
    }
}


void insert2()
{
    int item;
    cout<<"ENTER THE ITEM :"<<endl;
    cin>>item;
    if(head2==NULL)
    {
    nptr=new node;
    nptr->data=item;
    nptr->link=NULL;
    head2=nptr;
    }
    else
    {
        ptr2=head2;
        nptr=new node;
       
        while(ptr2!=NULL)
        {   
            if(ptr2->link==NULL)
            {
                nptr->data=item;
                ptr2->link=nptr;
                nptr->link=NULL;
            }   
            ptr2=ptr2->link;
        }
    }
}

void display2()
{
    ptr2=head2;
    cout<<endl<<"entered list is :\n"<<endl;
    while(ptr2!=NULL)
    {
        cout<<ptr2->data<<"-->";
        ptr2=ptr2->link;
    }
    cout<<endl;
}


void display1()
{
    ptr1=head1;
    cout<<endl<<"entered list is :\n"<<endl;
    while(ptr1!=NULL)
    {
        cout<<ptr1->data<<"-->";
        ptr1=ptr1->link;
    }
    cout<<endl;
}


void merge_alternate(int c1, int c2) // merging alternate elements of two linked list:
{
    ptr2=head2;
    ptr1=head1;
   
    if(c1==c2)
    {
        while(ptr1!=NULL)
        {
            fptr1=ptr1->link;
            fptr2=ptr2->link;
           
            ptr1->link=ptr2;
            ptr2->link=fptr1;
           
            ptr1=(ptr1->link)->link;
            ptr2=fptr2;
        }
        display1();
    }
    else
    cout<<"\nBoth list should contain same number of elements \n";
    }


Logic of merge_alternate function

int main()
{
    int choice,count=0,count1=0;
    while(1)
    {
    cout<<"ENTER YOUR CHOICE \n1. INSERTION IN FiRST LIST \n2. INSERTION IN SECOND LIST \n3. FOR DISPLAY LIST1 \n4. DISPLAY LIST2 \n5. MERGE BOTH LIST \n6. EXIT\n "<<endl;
    cin>>choice;
    switch(choice)
    {
        case 1:
            insert1();
            count++;
            break;
        case 2:
            insert2();
            count1++;
            break;
        case 3:
            display1();
            break;
        case 4:
            display2();
            break;
        case 5:
            merge_alternate(count,count1);
            break;
        case 6:
            exit(0);
            break;
        default: cout<<"\n enter a valid choice ";
    }
    }
    getch();
    return 0;
}


Output :