The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Friday, September 9, 2011

Row Average, column Average and Matrix Average of a Random Matrix


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5

int mat[max][max];

void main()
{
int i,j;
int row_sum[max];
int col_sum[max];
int mat_sum = 0;
clrscr();
randomize();
printf("\n\n RANDOM MATRIX \n\n");
for(i=0;i<max;i++)
{
 row_sum[i] = 0;
 printf("\n");
 for(j=0;j<max;j++)
 {
  if(i==0)
   col_sum[j] = 0;
  mat[i][j] = random(10);
  col_sum[j] += mat[i][j];
  row_sum[i] += mat[i][j];
  mat_sum += mat[i][j];
  printf(" %d",mat[i][j]);
 }
}

getch();

printf("\n\n Row Sums");
for(i =0; i<max; i++)
 printf("\n Row %d = %d",i+1,row_sum[i]);

printf("\n\n Column Sums");
for(i =0; i<max; i++)
 printf("\n Column %d = %d",i+1,col_sum[i]);

printf("\n\n Matrix Sum = %d",mat_sum);
getch();
}

Wednesday, September 7, 2011

Bingo Game


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5

int tckt1[max][max],tckt2[max][max],tckt[max*max];
char ctckt1[max][max],ctckt2[max][max];

void create_tckt(char ctckt[max][max],int tkt[max][max])
{
int a,j=0;
int flg;
for(int i=0;i<max*max;i++)
{
 do
 {
 flg = 0;
  do
  {
  a = rand() % 26;
  }while(a==0);
  for(int k=0;k<i;k++)
   {
    if(a == tckt[k])
     { flg = 1; break; }
   }
  if(flg == 0)
   tckt[i] = a;
 }while(flg==1);
}
cout<<endl;
int l = 0;
for(i=0;i<max;i++)
{
 for(j=0;j<max;j++)
  {
   tkt[i][j] = tckt[l];
   ctckt[i][j] = ' ';
   l++;
  }
}
}

void disp_tckt(char ctckt[max][max],int tkt[max][max])
{
for(int i=0;i<max;i++)
{
 cout<<"\n\n";
 for(int j=0;j<max;j++)
  {
   printf("%3d%c",tkt[i][j],ctckt[i][j]);
  }
}
}

void search(int num, int tckt[max][max], char ctckt[max][max])
{
 int flag = 0;
 for(int i=0;i<max;i++)
 {
  for(int j=0;j<max;j++)
  {
   if(tckt[i][j] == num)
   {
    ctckt[i][j] = '*';
    flag = 1; break;
   }
  }
 }
 if(flag == 1)
  cout<<"\n\nthe Elemenbt has been found !!";
 else
  cout<<"\n\nthe element has not been found !!";
 cout<<"\n\n After draw of the new element ur ticket : \n";
 disp_tckt(ctckt,tckt);
}

int chk_bingo(char ctkt[max][max])
{
 int cnt;
 for(int i=0;i<max;i++)
 {
  cnt = 0;
  for(int j=0;j<max;j++)
   {
     if(ctkt[i][j] == '*')
      cnt++;
   }
  if(cnt == 5)
    return 1;
 }

 for(i=0;i<max;i++)
 {
  cnt = 0;
  for(int j=0;j<max;j++)
   {
     if(ctkt[j][i] == '*')
      cnt++;
   }
  if(cnt == 5)
    return 1;
 }

 cnt = 0;
 for(i=0;i<max;i++)
 {
   if(ctkt[i][i] == '*')
      cnt++;
  }
  if(cnt == 5)
    return 1;

 cnt = 0;
 for(i=0;i<max;i++)
 {
   if(ctkt[i][max-1-i] == '*')
      cnt++;
  }
  if(cnt == 5)
    return 1;
 return 0;
}

int game(char ctckt1[max][max],int tckt1[max][max],char ctckt2[max][max],int tckt2[max][max])
{
 int num[max*max];
 int ns=0;
 int res1,res2;
 int a,flg;
 for(int j=0;j<max*max;j++)
 {
 getch(); clrscr();
 do
 {
  flg = 0;
  do
  {
  randomize();
  a = rand() % 26;
  }while(a==0);
 for(int i=0;i<ns;i++)
  {
  if(a == num[i])
   { flg = 1; break; }
  }
  if(flg == 0)
   { num[ns] = a; ns++; }
 }while(flg == 1);
 cout<<"\n\n the List so far :";
 for(int i=0;i<ns;i++)
 cout<<" "<<num[i];
 search(a,tckt1,ctckt1);
 getch();
 search(a,tckt2,ctckt2);
 getch();
 res1 = chk_bingo(ctckt1);
 res2 = chk_bingo(ctckt2);
 if(res1==1)
  return 1;
 else if (res2 == 1)
  return 2;
 else if(res1 == 1 && res2 == 1)
  return 3;
 }
}

void main()
{
clrscr();
int result;
create_tckt(ctckt1,tckt1);
disp_tckt(ctckt1,tckt1);
cout<<"\n\n";
create_tckt(ctckt2,tckt2);
disp_tckt(ctckt2,tckt2);
result = game(ctckt1,tckt1,ctckt2,tckt2);
if (result == 1)
cout<<"\n\nPLAYER 1 WINS";
else if (result == 2)
cout<<"\n\nPLAYER 2 WINS";
else
cout<<"\n\nDRAW";
getch();
}

Tuesday, September 6, 2011

Decimal-Binary and Decimal-Hexadecimal Conversion


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#define max 10

 class number
  {
   private :
     int dec;
     long bin;
     char hdec[max];
   public  :
     void indec()
      {
       cout<<"Enter any decimal number :- ";
       cin>>dec;
      }

     void dec2bin()
      {
       int i = 0,n,dec2 ;
       bin = 0;
       dec2 = dec;
       while(dec2)
{
n = dec2 % 2;
bin += n*pow(10,i);
i++;
dec2 /= 2;
}
       cout<<"\n\n\t Its binary equivalent is :-"<<bin;
      }

     void dec2hxdc()
      {
       int i=0,n;
       while(dec != 0 )
{
n = dec%16;
if(n==10)   hdec[i] = 'A';
else if(n==11)    hdec[i] = 'B';
else if(n==12)    hdec[i] = 'C';
else if(n==13)    hdec[i] = 'D';
else if(n==14)    hdec[i] = 'E';
else if(n==15)    hdec[i] = 'F';
else           hdec[i] = n+48 ;
i++;
dec /= 16;
}
hdec[i]='\0';
strrev(hdec);
cout<<"\n\n\t n Its hexadecimal equivalent is :- ";
puts(hdec);
      }

     };

 void main()
 {
  clrscr();
  number nm;
  nm.indec();
  nm.dec2bin();
  nm.dec2hxdc();
  getch();
 }

Monday, September 5, 2011

Binary-Decimal and Decimal-Hexadecimal Conversion


#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#define max 10
 void main()
  {
   clrscr();
   long bin;
   char hdec[max];
   int i,x,n,fb,dec;
   do
   {
   printf("Enter the Binary No. :- ");
   scanf("%ld",&bin);
   i = fb = dec = 0;
   while(bin)
     {
       n = bin % 10;
       dec += n*pow(2,i);
       i++;
       bin /= 10;
       if( n != 0 && n != 1)
{
  fb = 1;
  break;
}
     }
    if(fb == 1)
     printf("\n\n\t\t ERROR :- This is not a binary number .. Retype Again !! \n\n");
   }while(fb == 1);
   i = 0;
   printf("\n\n\t Its decimal equivalent is :- %d",dec);
   printf("\n\n\t n Its hexadecimal equivalent is :- ");
   while(dec != 0 )
    {
      n = dec%16;
       if(n==10) hdec[i] = 'A';
       else if(n==11)    hdec[i] = 'B';
       else if(n==12)    hdec[i] = 'C';
       else if(n==13)    hdec[i] = 'D';
       else if(n==14)    hdec[i] = 'E';
       else if(n==15)    hdec[i] = 'F';
       else         hdec[i] = n+48 ;
       i++;
      dec /= 16;
    }
   hdec[i]='\0';
   strrev(hdec);
   puts(hdec);

   getch();
  }

Sunday, September 4, 2011

Various Matrix Operations


#include<conio.h>
#include<iostream.h>
#define max 3

class matrix
{
 private :
  int mx[max][max];
  int det;
  int tmx[max][max];
  float imx[max][max];
  int cf[max][max];
  int adj[max][max];
 public :
  void inp_dat();
  int find_mdet(int,int);
  void find_trns(int);
  void pro_dat();
};

int i,j,k;

void matrix :: inp_dat()
{
 for(i=1;i<=max;i++)
 {
  cout<<"\n Enter the elements for Row #"<<i<<" :";
  for(j=1;j<=max;j++)
   cin>>mx[i][j];
 }
 for(i=1;i<=max;i++)
 {
  cout<<"\n Row #"<<i<<" :";
  for(j=1;j<=max;j++)
   cout<<" "<<mx[i][j];
 }
}

void matrix :: find_trns(int choice)
{
 for(int i=1;i<=max;i++)
 {
  cout<<"\n Row #"<<i<<" :";
  for(int j=1;j<=max;j++)
  {
  if(choice == 1)
   {
   tmx[i][j] = mx[j][i];
   cout<<" "<<tmx[i][j];
   }
  else
   {
   adj[i][j] = cf[j][i];
   cout<<" "<<adj[i][j];
   }
  }
 }
}

int matrix :: find_mdet(int r,int c)
{
 int ar[4],res;
 k = 0;
 for(i=1;i<=max;i++)
 {
  for(j=1;j<=max;j++)
  {
   if(i != r && j != c)
   {
    ar[k] = mx[i][j];
    k++;
   }
  }
 }
 res = ar[0]*ar[3]-ar[1]*ar[2];
 return res;
}

void matrix :: pro_dat()
{
 int prod = 1;
 det = 0;
 for(i=1;i<=max;i++)
 {
  det += prod*mx[1][i]*find_mdet(1,i);
  prod *= -1;
 }
 cout<<"\n\n Its Determinant is : "<<det;
 cout<<"\n\n Its Transpose is : \n\n ";
 find_trns(1);
 cout<<"\n\n Its co-factor is : \n";
 for(i=1;i<=max;i++)
 {
  cout<<"\n Row #"<<i<<" :";
  for(j=1;j<=max;j++)
  {
  cf[i][j] = prod*find_mdet(i,j);
  cout<<" "<<cf[i][j];
  prod *= -1;
  }
 }
 cout<<"\n\n Its Adjoint is : \n ";
 find_trns(2);
 if(det == 0)
  cout<<"\n\n It's Inverse doesn't exists as its Determinant is Zero.";
 else
 {
  cout<<"\n\nIts Inverse is : \n";
  for(i=1;i<=max;i++)
  {
   cout<<"\n Row #"<<i<<" :";
   for(j=1;j<=max;j++)
   {
   imx[i][j] = (float) adj[i][j]/det;
   cout<<" "<<imx[i][j];
   }
  }
 }
}

void main()
{
clrscr();
matrix mat;
mat.inp_dat();
mat.pro_dat();
getch();
}

Saturday, September 3, 2011

Polynomial Equation


#include<stdio.h>
#include<conio.h>

struct poly
{
 int coef;
 int xexp;
 int yexp;
 int zexp;
 struct poly *link;
}*ply1,*ply2,*sply,*rear,*nw,*ptr,*locp,*loc,*loca;

int cho;

void ins_poly()
{
 //cho = 0;
 do
 {
 nw = new poly;
 printf("\n Enter the coefficient : ");
 scanf("%d",&nw->coef);
 printf("\n Enter the power for 'x' : ");
 scanf("%d",&nw->xexp);
 printf("\n Enter the power for 'y' : ");
 scanf("%d",&nw->yexp);
 printf("\n Enter the power for 'z' : ");
 scanf("%d",&nw->zexp);
 nw->link = NULL;
 if(ply1 == NULL)
   ply1 = nw;
 else
 {
  locp = NULL;
  loc = ply1;
  while(loc != NULL && ( nw->xexp < loc->xexp || (nw->xexp == loc->xexp && nw->yexp < loc->yexp ) || (nw->xexp == loc->xexp && nw->yexp == loc->yexp && nw->zexp < loc->zexp )) )
  {
   locp = loc; loc = loc->link;
  }
  if(nw->xexp == loc->xexp && nw->yexp == loc->yexp && nw->zexp == loc->zexp )
  {
   loc->coef += nw->coef;
   delete nw;
  }
  else
  {
  if(locp == NULL)
  {
   nw->link = ply1;
   ply1 = nw;
  }
  else
  {
   nw->link = loc;
   locp->link = nw;
  }
 }
 }
 printf("\n\n Enter '1' to continue : ");
 scanf("%d",&cho);
 }while(cho == 1);

 do
 {
 nw = new poly;
 printf("\n Enter the coefficient : ");
 scanf("%d",&nw->coef);
 printf("\n Enter the power for 'x' : ");
 scanf("%d",&nw->xexp);
 printf("\n Enter the power for 'y' : ");
 scanf("%d",&nw->yexp);
 printf("\n Enter the power for 'z' : ");
 scanf("%d",&nw->zexp);
 nw->link = NULL;
 if(ply2 == NULL)
   ply2 = nw;
 else
 {
  locp = NULL;
  loc = ply2;
  while(loc != NULL && ( nw->xexp < loc->xexp || (nw->xexp == loc->xexp && nw->yexp < loc->yexp ) || (nw->xexp == loc->xexp && nw->yexp == loc->yexp && nw->zexp < loc->zexp )) )
  {
   locp = loc; loc = loc->link;
  }
  if(nw->xexp == loc->xexp && nw->yexp == loc->yexp && nw->zexp == loc->zexp )
  {
   loc->coef += nw->coef;
   delete nw;
  }
  else
  {
  if(locp == NULL)
  {
   nw->link = ply2;
   ply2 = nw;
  }
  else
  {
   nw->link = loc;
   locp->link = nw;
  }
  }
 }
 printf("\n\n Enter '1' to continue : ");
 scanf("%d",&cho);
 }while(cho == 1);
}

void add_poly()
{
 loc = ply1;
 loca = ply2;
 sply = NULL;
 while(loc != NULL && loca != NULL)
 {
  nw = new poly;
  if(loc->xexp > loca->xexp || loc->yexp > loca->yexp || loc->zexp > loca->zexp )
  {
   nw = loc;
   loc = loc->link;
  }
  else if(loc->xexp < loca->xexp || loc->yexp < loca->yexp || loc->zexp < loca->zexp )
  {
   nw = loca;
   loca = loca->link;
  }
  else
  {
   nw = loc;
   nw->coef = loca->coef + loc->coef;
   loc = loc ->link;
   loca = loca ->link;
  }
  nw->link = NULL;
  if(sply == NULL)
   sply = rear = nw;
  else
  {
   rear->link = nw;
   rear = nw;
  }
 }
 while(loc != NULL)
 {
  nw = new poly;
  nw = loc;
  rear->link = nw;
  rear = nw;
  loc = loc->link;
 }
 while(loca != NULL)
 {
  nw = new poly;
  nw = loca;
  rear->link = nw;
  rear = nw;
  loca = loca->link;
 }
}

void main()
{
 clrscr();
 ins_poly();
 printf(" (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ply1->coef,ply1->xexp,ply1->yexp,ply1->zexp);
 ptr = ply1->link;
 while(ptr != NULL)
 {
  printf(" + (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ptr->coef,ptr->xexp,ptr->yexp,ptr->zexp);
  ptr = ptr->link;
 }

  printf("\n\n (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ply2->coef,ply2->xexp,ply2->yexp,ply2->zexp);
 ptr = ply2->link;

 while(ptr != NULL)
 {
  printf(" + (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ptr->coef,ptr->xexp,ptr->yexp,ptr->zexp);
  ptr = ptr->link;
 }
 add_poly();

 printf("\n\n\n (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",sply->coef,sply->xexp,sply->yexp,sply->zexp);
 ptr = sply->link;
 while(ptr != NULL)
 {
  printf(" + (%d)*pow(x,%d)*pow(y,%d)*pow(z,%d)",ptr->coef,ptr->xexp,ptr->yexp,ptr->zexp);
  ptr = ptr->link;
 }
 getch();
}

Friday, September 2, 2011

RSA Encryption (Test Phase)


/* C program for the Implementation Of RSA Algorithm */

#include<stdio.h>          
#include<conio.h>
#include<string.h>
#include<stdlib.h>

int phi,n,e,d,FLAG;
char M[] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]_
abcdefghijklmnopqrstuvwxyz{|}~`";
char C[100];
int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return 0;
}
FLAG = 0;
}

void printfact(int num)
{
 printf("\n Factors of %d:- ",num);
 for(int i=1;i<=num/2;i++)
 {
  if(num%i == 0)
   printf(" %d",i);
 }
 printf("\n");
}

void encrypt()
{ int j,i;
clrscr();
printf("\n \t ........ Encryption Phase ........\n");
int k=strlen(M);
for(j=0;j<k;j++)
{
printf("\n Initially --> %d %c \n",M[j],M[j]);
int t=M[j];

int s = 1;
for(i=0;i< e;i++)
{
s=(s*t)%n;
printf("%d",s);
}
s = s%n;
C[j]=s;
printf("\n Now --- > %c %d\n",C[j],C[j]);
}

printf("\n\tEncrypted keyword :\n");
for(i=0;i<j;i++)
printf(" %c",C[i]);
getch();
}

void decrypt()
{
int j,i;
printf("\n \t ........ Decryption Phase ........\n");
for(j=0;j<strlen(C);j++)
{
int k=C[j];
int t = 1;
printf("\n\n Earlier .. %d %c --> ",C[j],C[j]);
for(i=0;i< d;i++)
{t=(t*k)%n;
printf(" %d",t);
}
t = t%n;
M[j]=t;
printf("\n Now ... %c %d -->",M[j],M[j]);
}
printf("\n\tDecrypted keyword :");
for(i=0;i<j;i++)
printf(" %c",M[i]);
getch();
}

int main()
{
clrscr();
int p,q,s,i;
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);

printfact(phi);

do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
fflush(stdin);
puts(M);
printf("\n\n");
for(i=0;i<strlen(M);i++)
 printf("  %d",M[i]);
encrypt();
printf("\n\n");
for(i=0;i<strlen(M);i++)
 printf("  %d",C[i]);
//
//printf("\n\nEnter the Cipher text\t: ");
//gets(C);
clrscr();
decrypt();

printf("\n\n");
for(i=0;i<strlen(M);i++)
 printf("  %d",M[i]);
printf("\n");
puts(M);
getch();
return 0;
}

Thursday, September 1, 2011

The 3n+1 Problem


#include<stdio.h>
#include<conio.h>

void _3nplus1(int n,int&pt)
{
 pt++;
 if(n != 1)
 {
  if(n % 2 == 0)
    _3nplus1(n/2,pt);
  else
    _3nplus1(3*n+1,pt);
 }
}

void main()
{
 clrscr();
 int i,j,k=0,l=0;
 scanf("%d %d",&i,&j);

 do
 {
  _3nplus1(i,k);
  i++;
  if(k > l)
   l = k;
  k = 0;
 }while(i<=j);
 printf("\n\n%d",l);
 getch();
}