Sunday, February 19, 2012

SWAPPING-VERY IMPORTANT LOGIC


#include<iostream>
using namespace std;

void approach1(int& a, int& b)
{
  cout<<"\nApproach 1"<<endl;
  a^=b^=a^=b;
}

void approach2(int& a, int& b)
{
  cout<<"\nApproach 2"<<endl;
  //b=(a+b)-(a=b); - This should work but doesnt, why?  a =((a = a + b) - (b = a - b)); 
}

void approach3(int& a, int& b)
{
  cout<<"\nApproach 3"<<endl;
  a = ((a = a * b) / (b = a / b));
}



int main()
{
  int a = 13, b = 29;
  cout<<"\nOriginal"<<endl;
  cout<<"a = "<<a<<", b = "<<b<<endl;

  approach1(a, b);
  cout<<"a = "<<a<<", b = "<<b<<endl;

  a = 13, b = 29;
  approach2(a, b);
  cout<<"a = "<<a<<", b = "<<b<<endl;

  a = 13, b = 29;
  approach3(a, b);
  cout<<"a = "<<a<<", b = "<<b<<endl;

  return 0;
}

sorting,union and intersection of elements....


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
 
int main()
{
  int setOne[] = {5, 10, 15, 20, 25};
  int setTwo[] = {50, 40, 30, 20, 10, 11, 21, 31, 41, 51};
 
  int setOneSize = sizeof(setOne) / sizeof(int);
  int setTwoSize = sizeof(setTwo) / sizeof(int);
 
  //Its necessary to sort if not already sorted  sort(setTwo, setTwo + setTwoSize);
 
  vector<int> unionSetVector(setOneSize + setTwoSize); 
  set_union(setOne, setOne + setOneSize, setTwo, setTwo + setTwoSize, unionSetVector.begin());
  
  cout<<"\n1. unionSetVector : ";
  copy(unionSetVector.begin(), unionSetVector.end(), ostream_iterator<int>(cout, " "));
  cout<<endl;
 
  vector<int> intersectionSetVector(min(setOneSize, setTwoSize)); 
  set_intersection(setOne, setOne + setOneSize, setTwo, setTwo + setTwoSize, intersectionSetVector.begin());
 
  cout<<"\n1. intersectionSetVector : ";
  copy(intersectionSetVector.begin(), intersectionSetVector.end(), ostream_iterator<int>(cout, " "));
  cout<<endl;
 
  cout<<endl;
  return 0;
}
 

Simple example of multiset


#include <iostream>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
 int a[ 10 ] = { 7, 22, 9, 1, 18, 30, 100, 22, 85, 13 };
 int aSize = sizeof(a) / sizeof(int);
 std::multiset< int, std::less< int > > intMultiset(a, a + aSize);

 cout << "Printing out all the values in the multiset : ";
 multiset<int>::iterator it;
 for ( it = intMultiset.begin(); it != intMultiset.end(); ++it)
   cout << "  " << *it;
 cout << endl << endl;

 std::ostream_iterator< int > output( cout, " " );

 cout << "There are currently " << intMultiset.count( 15 )
      << " values of 15 in the multiset\n\n";

 intMultiset.insert( 15 );
 intMultiset.insert( 15 );

 cout << "After two inserts, there are currently " << intMultiset.count( 15 )
      << " values of 15 in the multiset\n\n";

 cout << "Printing out all the values in the multiset : ";
 for ( it = intMultiset.begin(); it != intMultiset.end(); ++it)
   cout << "  " << *it;
 cout << endl << endl;

 return 0;
}

Saturday, February 18, 2012

DIG'IT'-----techfest by IT students of Vidyanikethan guyyssss


sample program to avoid confusion with auto and register storage classes....

//Program tested on Microsoft Visual Studio 2008  
 #include<iostream>
using namespace std;

int main()
{
 int i = 123;
 auto int j = 456;
 register int k = 789;

 cout<<"Address of i = " << &i <<", Value = " << i << endl;
 cout<<"Address of j = " << &j <<", Value = " << j << endl;
 cout<<"Address of k = " << &k <<", Value = " << k << endl;

 return 0;
}

Tuesday, August 2, 2011

java code for crc-12 and crc-16

import java.io.*;
import java.util.*;
class Crc
{
    public char find(char c,char d)
    {
        if(c=='0'&&d=='0')
            return '0';
        else if(c=='0'&&d=='1')
            return '1';
        else if(c=='1'&&d=='0')
            return '1';
        else
            return '0';
    }
    public static void main(String[] args)throws IOException
    {
        String s1;
        Scanner p=new Scanner(System.in);
        System.out.println("enter the original data bits");
        s1=p.next();
        int l1=s1.length();
        System.out.println("menu\n1.crc-12\n2.crc-16");
        System.out.println("enter the choice");
        int ch=p.nextInt();
        String s2="";
        if(ch==1)
        {
            s2="000000000000";
        }
        else if(ch==2)
        {
            s2="0000000000000000000";
        }
        int l2=s2.length();
        System.out.println("enter the polynomial coefficient");
        String s3=p.next();
        int l3=s3.length();
        String s4="";
        s4=s1+s2;
        int l4=s4.length();
        int i,j,m;
        char a[]=new char[l4];
        for(i=0;i<l4;i++)
        {
            a[i]=s4.charAt(i);
        }
        char b[]=new char[l3];
        Crc cr=new Crc();
        char temp[]=new char[100];
        for(i=0;i<l3;i++)
        {
            temp[i]=cr.find(a[i],b[i]);
        }
        for(i=0;i<l4-l3;i++)
        {
            for(j=0;j<l3-1;j++)
            {
                temp[i]=temp[j+1];
            }
        temp[j]=a[l3+1];
        if(temp[0]=='0')
        {
            for(m=0;m<l3;m++)
            {
                temp[m]=cr.find(temp[m],'0');
            }
        }
        else
        {
            for(m=0;m<l3;m++)
            {
                temp[m]=cr.find(temp[m],b[m]);
            }
        }
        }
        System.out.println("transmitted frame:");
        for(i=1,j=1;i<l4;i++)
        {
            a[i]=temp[j];
            j++;
        }
        for(i=0;i<l4;i++)
        {
            System.out.print(+a[i]);
        }
        for(i=0;i<l4-l3;i++)
        {
            for(j=0;j<l3-1;j++)
            {
                temp[i]=cr.find(a[i],b[i]);
            }
            temp[j]=a[l3+1];
            if(temp[0]=='0')
            {
                for(m=0;m<l3;m++)
                {
                    temp[m]=cr.find(temp[m],'0');
                }
            }
            else
            {
               
                for(m=0;m<l3;m++)
                {
                    temp[m]=cr.find(temp[m],b[m]);
                }
            }
        }
            int count=0;
            for(i=0;i<l3;i++)
            if(temp[i]=='0')
                count++;
            if(count==l3)
                System.out.println("\ntransmitted code is error free");
            else
                System.out.println("\n it is an error");
    }
}