Thursday, 11 April 2013

to count the number of occurences of characters in a given sentence




#include<iostream>
#include<conio.h>
#include<cstring>
#include<string>
using namespace std;
int main()
{string a;
cout<<"enter some text :\n\n";
getline(cin,a);

int l=a.length(),c=1,j,k,i,m;    //c for counting the number of occourences which is stored in freq
int freq[l];
for(j=0;j<l;j++)
   {freq[j]=c;
      for( k=j+1;k<l;k++)
       {   if(a[j]==a[k])              //a[j]  is compared with all of the remaining of characters, if
            {freq[j]=++c;              //it is equal to any of the character , count of freq[j]
            for( m=k;m<l-1;m++)   //corresponding to a[j] is increased.a[k] equal to a[j] is            
            a[m]=a[m+1];               // replaced by a[k+1].similarly a[k+1] by a[k+2].... so on
            k--;l--;
            }
        }c=1;
   }

 cout<<"character\tFrequency\n";
 for(i=0;i<l;i++)
 cout<<a[i]<<"\t\t"<<freq[i]<<endl;


 getch();
 return 0;
}

Tuesday, 19 February 2013

To display the roman numeral of user entered number

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"Enter number to display equivalent roman numeral\n";
    cin>>n;
            for (;n>=1000;n-=1000)
                cout<<"M";
            for (;n>=900;n-=900)
                cout<<"CM";
            for (;n>=500;n-=500)
                cout<<"D";
            for (;n>=400;n-=400)
                cout<<"CD";
            for (;n>=100;n-=100)
                cout<<"C";
            for (;n>=90;n-=90)
                cout<<"XC";
            for (;n>=50;n-=50)
                 cout<<"L";
            for (;n>=40;n-=40)
                cout<<"XL";
            for (;n>=10;n-=10)
                cout<<"X";
            for (;n>=9;n-=9)
                cout<<"IX";
            for (;n>=5;n-=5)
                cout<<"V";
            for (;n>=4;n-=4)
                cout<<"IV";
            for (;n>=1;n-=1)
                cout<<"I";




    return 0;
}




Do visit
http://facebook.com/csedevgroup
http://devendradora.blogspot.in/
http://computersciencedora.blogspot.in/

ragged matrix

ragged matrix is that which has unequal number of elements in the each row

#include<iostream>
using namespace std;
#include<conio.h>
int main()
{int r,j,i,pos[]={2,1,3,5,7,4,6,9,10,8,11,13,12,15,14};
cout<<"enter how many rows u want to display of a ragged matrix";
cin>>r;int a[r][15];
cout<<"Enter the elements\n";
int s=1,n;
for(i=0;i<r;i++)
  { for(j=0;j<pos[i];j++)
      cin>>a[i][j];
  }
cout<<"\n\n\n\n";
for(i=0;i<r;i++)
  { for(j=0;j<pos[i];j++)
    cout<<a[i][j]<<"  ";
   cout<<endl;
  }
       getch();
}

convert decimal number to hexadecimal(using arrays)

#include<iostream>
#include<math.h>
using namespace std;
int main()
{ int dec,i=0,store[20];
cout<<"decimal number please.....\n";
cin>>dec;
while(dec!=0)
{  store[i]=dec%16;   //remainder represents hexadecimal equivalent
   dec=dec/16;
   i++;
}
for(;i>=0;i--)
{if(store[i]>=10)
    cout<<(char)(store[i]+55);   //55 added  to give equivalent ASCII value
     else
    cout<<store[i];
}
return 0;
}

Tuesday, 22 January 2013

to convert binary number to decimal equivalent c++


int main()
{ int n,i,sum,rem;
    sum=0,i=0;
    cout<<"enter the binary number   ";
    cin>>n;
  while(n!=0)
  { if((n%10==0) || (n%10==1))
      {rem=n%10;
      sum=sum+(rem*pow(2,i));
      n=n/10;
      ++i;
      }
      else {cout<<"entered number is not binary";goto a;}
  }
    cout<<"the decimal number is:"<<sum;
    a:
    getch();
}

sum of series 1+x+(x^2)/2!+..............+(x^n)/n! c++ program


#include<iostream>
using namespace std;
#include<conio.h>

int fact(int);
#include<math.h>
int main()
{    double sum=1;
int x,i=1,n;
cout<<"1+x+(x^2)/2!+............+(x^n)/n!\n\n\n\n";
cout<<"enter the value of x    ";
cin>>x;
cout<<"enter th value of n      ";  cin>>n;
while(i<=n)
{
sum=sum+(pow(x,i)/fact (i));
i++;
}
double val=(double)sum;        //typecasting
cout<<"sum is "<<val;
getch();
}

int fact(int j)
{
int fact=1,k=1;
while(k<=j)
{
     fact=fact*k;
     k++;
     }
return fact;
}

How the songs are played randomly in a playlist?? This may be one of the algorithm


#include<iostream>
#include<conio.h>
using namespace std;
class Playlist
{public:
        void random(); /*This function initialises the values from 1 to n and swaps the numbers for odd number count*/
               void swap(int &a,int &b);
};  

int main()
{Playlist dev;
dev.random();
getch();
}

void Playlist::random()
{int n,s,i,p;   //present song number p
cout<<"The number of songs in the playlist  :"; cin>>n;
int a[n],j=1;
cout<<"enter the present song number  : ";  cin>>p; s=p;//s  store 
a[0]=p;
     for(i=++p;i<=n;i++,j++)
     {
     a[j]=i;
     if(j%2!=0)
     swap(a[j-1],a[j]);
     }


   if(j!=n)
   {  for(i=1;i<p;i++,j++)
      {a[j]=i;
      if(j%2!=0)
           swap(a[j-1],a[j]);
     }
    }
 cout<<"\nNew  playlist generated \n";
 for(j=0;j<n;j++)
 cout<<a[j]<<"  ";  
}
void Playlist::swap(int &a,int &b)
{int temp=a;
a=b;
b=temp;
}