Sunday 30 December 2012

How the songs are played randomly in a playlist?? This may be 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;
}

Saturday 22 December 2012

Determinant of a nxn Matrix c++ program

#include<iostream>
#include<math.h>
#include<conio.h>
using namespace std;
double d=0;
double det(int n,double mat[10][10]);   // u can replace 10 with any number


int main()
{int n;
cout<<"enter the order of matrix" ;
cin>>n;
double mat[10][10];
int i,j;
cout<<"enter the elements"<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>mat[i][j];
cout<<"\ndeterminant    "<<det(n,mat);
getch();
}


double det(int n,double mat[10][10])
  double submat[10][10]; 
if(n==2) return( (mat[0][0]*mat[1][1])-(mat[1][0]*mat[0][1]));
else
{  for(c=0;c<n;c++)
   {  subi=0;   //submatrix's i value
      for(i=1;i<n;i++)
          {  subj=0;
             for(j=0;j<n;j++)
                    {if(j==c)
                      continue;
                     submat[subi][subj]=mat[i][j];
                     subj++;
                     }
              subi++;
   
            }
      d=d+(pow(-1,c)*mat[0][c]*det(n-1 ,submat));
    }
}
return d;
}

Friday 21 December 2012

Random generation c++ program

computerstechnologydevdora.blogspot.in
devgroupindia.webs.com

#include<iostream>   // based on the principle of prime,odd,even  numbers
using namespace std;
#include<conio.h>
#include<math.h>
int j=0,s=1;bool prime(int num);
int random(int k);
int main()
{ int d=100,i,num;
cout<<"Enter any  number  :";
cin>>num;
cout<<"random numbers generated\n";
for( i=num;i<d;)
   { cout <<abs(i)<<" ";i=random (i);}
getch();
}
int random(int k)
{
    if (prime(k))
  return (k- ++j);
  else if (k%2!=0){
  return (k+ ++s);}
  else
  return k+ ++j;
}
bool prime(int num)
{int c=1;for(int i=2;i<=num;i++)
         if(num%i==0)
         ++c;
         if (c==2)
         return 1;
         else
         return 0;
}

Tuesday 18 December 2012

To substract two numbers without using minus sign c++ program

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{int a,b,c=0,i,f;
cout<<"Enter two numbers to substract :";
cin>>a>>b;
if(b>a)
{i=a;f=b;}
else
{i=b;f=a;}  //  i initial value  f final value
for( ;i<f;i++)
 ++c;
(b>a)?   //conditional operators eqivalent to if (b>a)
cout<<a <<"  -  "  <<b<< "  =  "<<(char)45<< c   //ASCII value of 45 is minus -
:   // else
cout<<a <<"  -  "  <<b <<"  =  "<< c;
getch();
}



visit  computerstechnology
         Facebook Devgroup

To display the elements in a spiral way c++ program









Here middle element  is 1 and the numbers after 1 are displayed in spiral way

#include<iostream>
#include<iomanip>   // for setw(int)  space generating
using namespace std;
#include<conio.h>

int main()
{
int num,r,s=1,i,c,j,k,ord;

cout<<"Enter mid element of square matrix and get the elements in a spiral way  :";
cin>>num;
cout<<"enter odd order of square matrix";
cin>>ord;
int a[ord][ord];r=ord/2;c=ord/2;a[r][c]=num;   //r  row  c  column
for(i=1;i<=ord;i++)
{if(i%2==0)
           {for(j=0;j<s;j++) // i =even control left side
           a[r][--c]=++num;
           for(k=0;k<s;k++)
           a[--r][c]=++num;
           }
         
           else
           {for(j=0;j<s;j++)  //i =odd control right side
           a[r][++c]=++num;
           for(k=0;k<s;k++)
           a[++r][c]=++num;
           }
           s++;
}cout<<setw(ord);
for(r=0;r<ord;r++)
{  for(c=0;c<ord;c++)
  cout<<a[r][c]<<setw(ord);
  cout<<endl;
}
getch();
}

Tuesday 4 December 2012

To find the squareroot of a number without using sqrt() functon

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

int main()
{ float g,r,c=0,n,pg;
cout<<"enter the number to find squareroot   ";
cin>>n;
        g=n/2; pg=n;                                          //g - guess  pg - previous guess
       while(g!=pg)                                           // when g==pg   , then g or pg is the square root
       {r=n/g;
       pg=g;
       g=(g+r)/2;
       }
cout<<"square root \t"<<g<<endl;
getch();       
}

For more visit
http://computersciencedora.blogspot.in

Alphabet pattern

 
#include<iostream>
using namespace std;                
#include<conio.h>
#include<math.h>
int main()
{
 int k=-1,j;
char i,m=71,n=65;
for(int x=1;x<=7;x++)    //for number of lines 7
{
        for(i=65;i<=m;i++)      //for displaying A B C ...
        {
         cout<<i<<" ";
         }
         for(j=1;j<=k;j++)      //for displaying spaces
         {
           cout<<"  ";
         }
    k=k+2;
    if(i==72)
    {
    i--;
    }
i--;
      for(;i>=n;i--) //for displaying alphabets in reverse
      {
     cout<<i<<" ";
      }
      m--;
      cout<<"\n";
 }
  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;
}

to convert binary number to decimal equivalent

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

 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();
}