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 ;)
Showing posts with label Iteration. Show all posts
Showing posts with label Iteration. Show all posts

Sunday, June 17, 2012

Finding Vertical Histogram of given set of data 2


#include <iostream>#include <iomanip>#include <algorithm>#include <string>#include<conio.h>using namespace std;
#define VALS 30
int main ( void ){   int h[VALS] = { 1, 2, 4, 8, 16, 8, 4, 2, 1, 0, 1, 2, 4, 8, 4, 2,1, 0, 1, 2, 4, 8, 16, 8, 4, 8, 4, 2, 1, 0 };   int height=0;   int y,x;   cout.unsetf(ios::right);   cout.setf(ios::left);   cout << endl;   for (x=0; x<VALS; x++) height = max(height, h[x]);   for (y=height; y>=1; y--)   {       cout << setw(3) << y << "|";       for (x=0; x<VALS; x++)           if (h[x]>=y) cout << " *"; else cout << "  ";       cout << endl;   }   cout << "---+" << string(VALS*2, '-') << endl;   cout << "   |";   for (x=0; x<VALS; x++)   {       if (x<26)           cout << " " << char('a'+x);       else           cout << " " << char('A'+x-26);   }   cout << endl << endl;   getch();   return 0;}


Friday, June 15, 2012

Finding Vertical Histogram of given set of data


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

int main()
  {
     clrscr();
     int i,j,k,c,num,mrk[10],max,tmp;
     do
     {
     printf("Enter the no. of cases (max. 10): ");
     scanf("%d",&num);
     }while(num<=0 || num>10);
     printf("\n\n");

     max = 0;
     for(i=0;i<num;i++)
     {
 do
      {
printf("\nEnter frequency for Case #%d : ",i+1);
   scanf("%d",&mrk[i]);
 }while( mrk[i]>32766 || mrk[i]<0 );
  if(max < mrk[i])
    max = mrk[i];
     }
     printf("\n\n All data has been collected !!");
     getch();
     clrscr();
     for(i=0;i<num;i++)
     {
      c = (mrk[i]*25)/max;
      //gotoxy(5+(5*i),0);
      for(k=0;k<c;k++)
      {
for(j=0;j<3;j++)
{
gotoxy(5+(5*i)+j,k);
printf("*");

}
      }
      gotoxy(5+(5*i),k);
      printf("%d",mrk[i]);

     }

     getch();
     return 0;
  }

Wednesday, June 13, 2012

Finding Horizontal Histogram of given set of data


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

int main()
  {
     int i,j,k,c,num,mrk[100],max;
     printf("Enter the no. of cases : ");
     scanf("%d",&num);
     printf("\n\n");
   
     max = 0;
     for(i=0;i<num;i++)
     {
 do
      {
        printf("\nEnter frequency for Case #%d : ",i+1);
   scanf("%d",&mrk[i]);
 }while( mrk[i]>32767 || mrk[i]<0 );
  if(max < mrk[i])
    max = mrk[i];
     }
     printf("\n\n All data has been collected !!");
     getch();
   
     printf("\n\n\t\t\t\t HISTOGRAM \n");
     for(i=0;i<num;i++)
     {
      c = (mrk[i]*50)/max;
   
      for(k=0;k<3;k++)
      {
  if(k==1)
  { printf("\n Case #%d      ||  ",i+1); }
  else
  { printf("\n              ||  "); }

  for(j=0;j<c;j++)
    printf("*");

  if(k==1)
   printf("  %d",mrk[i]);
      }
      printf("\n");
      getch();            
     }  
     getch();
     return 0;
  }

Monday, June 11, 2012

Finding Norm of Matrix


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

int main()
  {
    int i,j,m,n;
    int mat[10][10];
    double s,nm;
    printf("\n Enter the no. of rows and columns : ");
    scanf("%d %d",&m,&n);
    s = 0;
    for(i=0;i<m;i++)
    {
      printf("Row %d : ",i+1);    
       
      for(j=0;j<n;j++)
      {
        scanf("%d",&mat[i][j]);    
        s += pow(mat[i][j],2);                  
      }              
    }
    nm = sqrt(s);
    printf("\n\nThe Norm of this matrix : %lf",nm);  
    getch();
    return 0;
  }

Saturday, June 9, 2012

Finding the Saddle Point of Matrix


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

int main()
  {
    int i,j,m,n,min;
    int mat[10][10];
    printf("\n Enter the no. of rows and columns : ");
    scanf("%d %d",&m,&n);

    for(i=0;i<m;i++)
    {
      printf("Row %d : ",i+1);            
      for(j=0;j<n;j++)
       scanf("%d",&mat[i][j]);  
    }
   
    for(i=0;i<m;i++)
    {
      min = 0;        
      for(j=1;j<n;j++)
      {
        if(mat[i][min] > mat[i][j])
         min = j;            
      }    
      for(j=0;j<m;j++)
      {
        if(mat[j][min] > mat[i][min])
         break;                            
      }
      printf("\n Row %d : ",i+1);
      if(j == m)
       printf("Saddle point exists at Column %d",min+1);
      else
       printf("Saddle point doesn't exists");    
    }    
    getch();
    return 0;
  }

Wednesday, June 6, 2012

Finding Skew-Symmetric Matrix


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

int main()
  {
    int i,j,m,n,min;
    int mat[10][10];
    do
    {
    printf("\n Enter the no. of rows and columns : ");
    scanf("%d %d",&m,&n);
    }while(m!=n);

    for(i=0;i<m;i++)
    {
      printf("Row %d : ",i+1);          
      for(j=0;j<n;j++)
       scanf("%d",&mat[i][j]);  
    }
 
    for(i=1;i<m;i++)
    {        
     for(j=0;j<i;j++)
     {
       if(mat[i][j] != -mat[j][i])
        break;            
     }  
    }  
 
    if(i == m && j+1 == i)
     printf("\n\nIt is a Skew-Symmetric Matrix");
    else
     printf("\n\nIt is not a Skew-Symmetric Matrix");
    getch();
    return 0;
  }

Monday, June 4, 2012

Finding Symmetric Matrix


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

int main()
  {
    int i,j,m,n,min;
    int mat[10][10];
    do
    {
    printf("\n Enter the no. of rows and columns : ");
    scanf("%d %d",&m,&n);
    }while(m!=n);

    for(i=0;i<m;i++)
    {
      printf("Row %d : ",i+1);          
      for(j=0;j<n;j++)
       scanf("%d",&mat[i][j]);  
    }
 
    for(i=1;i<m;i++)
    {        
     for(j=0;j<i;j++)
     {
       if(mat[i][j] != mat[j][i])
        break;            
     }  
    }  
 
    if(i == m && j+1 == i)
     printf("\n\nIt is a Symmetric Matrix");
    else
     printf("\n\nIt is not a Symmetric Matrix");
    getch();
    return 0;
  }

Thursday, May 31, 2012

Finding the Equivalent Number at the Given Base


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

int main()
{
  int i,b,e,tmp,nod=0;
  scanf("%d %d",&b,&e);
  tmp = b;
  while(tmp)
  {
    nod++;      
    tmp = tmp/e;          
  }
  printf("\n");
  while(nod>0)
  {  
   nod--;  
   tmp = 1;      
   for(i=0;i<nod;i++)
    tmp *= e;
   if(e != 16)
    printf("%d",b/tmp);
   else
   printf("%X",b/tmp);
 
   b %= tmp;      
  }
  getch();
  return 0;
}

Sunday, May 27, 2012

Finding a 4-Digit Perfect Square Number of order AABB


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

int main()
{
  int a=1,b=0,i,nm,nms;
  while(1)
  {
   
    nm = 0;
    for(i=1;i<=4;i++)
    {
     if(i<=2)              
      nm = nm*10 + a;
     else
      nm = nm*10 + b;
    }      
    nms = sqrt(nm);
    if(nm==(nms*nms))
    {
     printf("%d is Perfect Square Number of order AABB",nm);                    
     break;  
    }
    else
    {
     b = (++b)%10;
    if(b == a)
     b = (++b)%10;  
    if(b==0)
     ++a;  
    }
  }
  getch();
  return 0;
}

Tuesday, May 8, 2012

Arithmetic Coding



#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 127 
#define SIZE 100 




struct element
{
  char c;
  float pr;
  float rng;
  float UL;
  float LL;     
}el[MAX];


int size;
char msg[SIZE];
float range = 1.00;
float UP;
float DOWN;
float enc;
int str_len;


void make_table()
{
  int i;  
  printf(" Enter the size of elements : "); 
  scanf("%d",&size);
  fflush(stdin);
  for(i=0;i<size;i++) 
  {
    printf("\n #%d Enter character and its probablity (decreasing order) : ",i+1); 
    scanf("%c %f",&el[i].c,&el[i].pr);  
    fflush(stdin); 
    el[i].LL = ((i==0) ? 0.0 :el[i-1].UL);
    el[i].UL = ((i==size-1) ? 1.0 : (el[i].LL + el[i].pr));         
  }
  getch();    
}


void show_table(int num)
{
  printf("\t\tENCODER TABLE\n\tFrequency\tRange\n");   
  for(int i=0;i<size;i++)
  {    
    printf("\n");
    if(i==num)
    { printf("*"); }     
    printf("%c\t%f\t%f-%f",el[i].c,el[i].pr,el[i].LL,el[i].UL);       
  }   
}


int find_char(int num)
{
  int i=0;
  if(num != -1)
  {
   while(el[i].c != msg[num])
   { i++; }
   
  }
  else
  {
    while(!(enc <= el[i].UL && enc >= el[i].LL))
    { i++; }      
  }
  return i;
}


void modify_table(int num)
{
  range = el[num].UL - el[num].LL;
  for(int i=0;i<size;i++)
  {
    el[i].LL = ((i==0) ? el[num].LL : el[i-1].UL) ;     
    el[i].UL = ((i==size-1) ? (el[0].LL+range) : (el[i].LL + (el[i].pr*range)));    
  }  
}


void refresh_table()
{
  for(int i=0;i<size;i++)
  {
    el[i].LL = ((i==0) ? 0.0 :el[i-1].UL);
    el[i].UL = ((i==size-1) ? 1.0 : (el[i].LL + el[i].pr));       
  }   
}
void encode_text()
{
  fflush(stdin);   
  printf("\n Enter the string to be encoded : ");
  gets(msg);   
  fflush(stdin);
  int i=0;
  int j;
  while(msg[i] != '\0')
  {
    printf("\n\n\n Word Checked : %c \n",msg[i]);           
    j = find_char(i);     
    show_table(j);  
    modify_table(j);  
    getch();    
    i++;
  } 
  DOWN = el[j].LL;
  UP = el[j].UL;
  str_len = i;
  printf("\n\n The encoded range : %f-%f for a string of %d characters",DOWN,UP,str_len);
  refresh_table();
}


void decode_text()
{
  int j;   
  printf("\n Enter the encoded value : "); 
  scanf("%f",&enc);    
  fflush(stdin);
  printf("\n\n ... Decoding Text ... \n\n");   
  while(str_len)
  {
    j = find_char(-1);     
    printf("\n\n Word decoded : %c",el[j].c);
    enc = (enc - el[j].LL)/(el[j].UL - el[j].LL);       
    printf("\n New encoded value : %f",enc);
    str_len--;     
  }
}


int main()
{
 int cho;
 do
 {   
 printf("\n\n\n\t MENU \t\n"); 
 printf("\n1. Create Table ");
 printf("\n2. Encode Text "); 
 printf("\n3. Decode Text "); 
 printf("\n\n Choose the option : "); 
 scanf("%d",&cho);
 switch(cho)
 {
   case 1  : make_table();  break;
   case 2  : encode_text();  break;
   case 3  : decode_text();  break;
   default : break;             
 } 
 }while(cho>=1 && cho<=3);
 printf("\n\n\t Thanks for using the Program !! \t\n\n"); 
 return 0;  
}

Saturday, May 5, 2012

Floyd-Warshall Algorithm 2


#include<conio.h>
#include<stdio.h>
#define INFNITY 999
int a[10][10],n;



voidprintArray(){
inti,j;
for(i=0;i<n;i++)
    {
for(j=0;j<n;j++)
        {
printf("%d\t",a[i][j]);
        }
printf("\n");
    }
}
voiduAdjacencyMatrix(){
inti,j,c=1,w;
for(i = 0;i < n; i++)
    {
for(j = 0;j < i; j++)
        {
a[i][j] = INFNITY ;
a[j][i] = INFNITY ;

        }
a[i][i] = 0;   
    }

while(c!=-1)
    {
printf ("\ni , j , w :");           
scanf("%d%d%d",&i,&j,&w);
a[i][j]=w;
     //printf ("c? :");
scanf("%d",&c);
    }
printArray();
getch();
}

int main()
{
int d[10][10], da[10][10],i,j,l,m,k;
printf("enter the number of vertices : ");
scanf("%d",&n);
uAdjacencyMatrix();
for(i=0;i<n;i++)
 {
for(j=0;j<n;j++)
                 {
d[i][j]=a[i][j];
                 }
 }

for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
                 {
for( j=0;j<n;j++)
                     {
if(d[i][j]<d[i][k]+d[k][j])
                                 {
da[i][j]=d[i][j];
                                 }
else
                                 {
da[i][j]=d[i][k]+d[k][j];
                                 }
                     }
                 }
for(l=0;l<n;l++)
                 {
for(m=0;m<n;m++)
                                 {
d[l][m]=da[l][m];
                                 }
                 }
}                
for(l=0;l<n;l++)
                 {
printf("\n");
for(m=0;m<n;m++)
                                 {
printf("%d\t\t",da[l][m]);
                                 }
                 }                
getch();
return(0);
}

Thursday, May 3, 2012

Kruskal's Algorithm 2


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define INFNITY 999

int a[10][10],n,p[10];
voidprintArray(){
inti,j;
for(i=0;i<n;i++)
    {
for(j=0;j<n;j++)
        {
printf("%d\t",a[i][j]);
        }
printf("\n");
    }
}
voiduAdjacencyMatrix(){
inti,j,c=1,w;
for(i = 0;i < n; i++)
    {
for(j = 0;j < i; j++)
        {
a[i][j] = INFNITY;
a[j][i] = INFNITY;

        }
a[i][i] = INFNITY;

    }

while(c!=-1)
    {
printf ("\ni , j , w :");           
scanf("%d%d%d",&i,&j,&w);
a[i][j]=a[j][i]=w;
     //printf ("c? :");
scanf("%d",&c);
    }
printArray();
getch();
}


intfindParent(int a)
{
while(a!=p[a])
     a=p[a];
return a;
}


void unite(inta,int b)
{
inti,j;
     i=findParent(a);
     j=findParent(b);
if(i<j)
p[j]=i;
else
p[i]=j;

}
voidsetParent()
{
int i;
for (i=0;i<n;i++)
p[i]=i;
}

inttotalEdges()
{
inti,j,count=0;
for (i=0;i<n;i++)
      {
for(j=0;j<i;j++)
       {
if(a[i][j]!=INFNITY)
count++;
       }   
      }
      //printf("Count : %d",count);
return count;
}

voidminEdge(int *aa,int *bb)
{
inti,j,w=INFNITY;

for (i=0;i<n;i++)
      {
for(j=0;j<i;j++)
       {
if(a[i][j]<w)
          {
          w=a[i][j];
          *aa=i;
          *bb=j;
          }          
       }   
      }

}
int main()
{

printf("enter the number of vertices : ");
scanf("%d",&n);
uAdjacencyMatrix();
setParent();
intcount,u=0,v=0,w=0;
count=totalEdges();
while(count>0)
    {
minEdge(&u,&v);          
if(findParent(u) != findParent(v))
       {
if(u<v)
printf("\n%d --> %d",u,v);
else
printf("\n%d --> %d",v,u);
w+=a[u][v];
unite(u,v);                
       }


a[u][v]=a[v][u]=INFNITY;
count--;
    }

printf("\nWeight : %d",w);

getch();
return(0);

}

Thursday, April 26, 2012

Job Sequencing Problem


#include<conio.h>
#include<stdio.h>
#define max 20
#define limit -1

struct job
{
 int jno;
 int pr;    
 int ddl;    
}jb[max],tmp;

int seq[max];
int ss;

int size;

void inp_dat()
{
  printf("\n Enter the no. of Job : ");
  scanf("%d",&size);

  for(int i=0;i<size;i++)
  {
    jb[i].jno = i+1;
    printf("\n Enter Profit and Deadline of Job#%d : ",jb[i].jno);
    scanf("%d %d",&jb[i].pr,&jb[i].ddl);      
  }  
}

void disp_dat()
{
  for(int i=0;i<size;i++)
  {
    printf("\n\n\t Job#%d :",jb[i].jno);
    printf("\n Profit : %d",jb[i].pr);
    printf("\t Deadline : %d",jb[i].ddl);    
  }  

}

void Job_Seq()
{
  int i,j,temp;
  printf("\n\n Sorting Jobs in the decreasing order of Profit");
  int min = limit;
  int tpr = 0;
  ss = 0;
  for(i=0;i<size;i++)
  {
    for(j=0;j<size-i-1;j++)
    {
      if(jb[j].pr < jb[j+1].pr)
      {
        tmp = jb[j];
        jb[j] = jb[j+1];
        jb[j+1] = tmp;        
      }
    }
  }
 

 disp_dat();
 
 for(i=0;i<size;i++)
 {
   if(min < jb[i].ddl || ss+1 <= min)
   {
      seq[ss] = jb[i].jno;
      tpr += jb[i].pr;
      ss++;    
      if(min <= jb[i].ddl)
        min = jb[i].ddl;
      else
      {
        temp = seq[ss-1];
        for(j=ss-1;j>0;j--)
         seq[j] = seq[j-1];
        seq[0] = temp;
      }
     
   }                    
 }  

 printf("\n The Job Sequence is :");
 for(i=0;i<ss;i++)
  printf(" %d",seq[i]);
 printf("\n\n Total Maximum Profit : %d",tpr);
}

int main()
{
 inp_dat();  
 Job_Seq();
 getch();
 return 0;
}

Monday, April 16, 2012

Longest Common Sub-sequence


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 20

int c[max+1][max+1];
char b[max+1][max+1];
char x[max],y[max];
int m,n;

void showLCS(int i,int j)
{
  if(i!=0 && j!=0)
  {
    if(b[i][j] == '\\')
    {
      showLCS(i-1,j-1);    
      printf("%c",x[i-1]);        
    }    

    else if(b[i][j] == '|')
     showLCS(i-1,j);

    else
     showLCS(i,j-1);
  }  
}

int main()
{
 int i,j;  

 printf("\n Enter 1st string : ");
 gets(x);

 printf("\n Enter 2nd string : ");
 gets(y);

 m = strlen(x);
 n = strlen(y);

 for(i=1;i<=m;i++)
 {
  c[i][0] = 0;
  b[i][0] = ' ';
 }

 for(j=0;j<=n;j++)
 {
  c[0][j] = 0;
  b[0][j] = ' ';
 }

 for(i=1;i<=m;i++)
 {
  for(j=1;j<=n;j++)
  {
    if(x[i-1] == y[j-1])
    {
      c[i][j] = c[i-1][j-1]+1;
      b[i][j] = '\\';          
    }


    else
    {
      if(c[i-1][j] >= c[i][j-1])
      {
        c[i][j] = c[i-1][j];
        b[i][j] = '|';
      }

      else
      {
        c[i][j] = c[i][j-1];
        b[i][j] = '_';
      }      
    }
  }
 }

 for(i=0;i<=m;i++)
 {
   printf("\n");
   for(j=0;j<=n;j++)
    printf(" %c%d",b[i][j],c[i][j]);              
 }

 getch();
 printf("\n\n The LCS of both strings : ");
 showLCS(m,n);
 getch();
 return 0;  
}

Tuesday, April 10, 2012

Travelling Salesperson Problem


#include<stdio.h>
#include<conio.h>
#define max 10
#define INFINITY 1000

int w[max][max];
intn_size;
int p[max];
ints_size;
int stack[max];
int route[max];

int min;

voidin_dat()
{
printf("\n Enter the number of nodes : ");
scanf("%d",&n_size);
fflush(stdin);

int i;

for(i=0;i<n_size;i++)
 {
w[i][i] = 0;
for(int j=i+1;j<n_size;j++)
  {
printf("\n\n Enter the weight of edge '%c' and '%c': ",65+i,65+j);
scanf("%d",&w[i][j]);
w[j][i] = w[i][j];
  }
 }
}

voiddis_dat()
{
printf("\n The Path adjacent matrix \n");
printf("\n ");

for(int i=0;i<n_size;i++)
 {
printf("\n");
for(int j=0;j<n_size;j++)
  {
printf("\t %d",w[i][j]);

  }
 }
getch();
fflush(stdin);
}

voidfindshort(intsr,intct,intss)
{

if(ct == 0)
  {
inttdst=0;    
 //  printf("\n Checking ss= %d sr='%c' :",ss,65+sr);   
   // for(int j=0;j<ss;j++)    
   //   {
    //    printf(" %c",65+stack[j]);   
     // }

for(int k=0;k<ss-1;k++)
    {
tdst += w[stack[k]][stack[k+1]];

     //printf(" p[%c]=%c",65+stack[k],65+stack[k+1]);
    }
if(min >tdst)
    {
min = tdst;
for(int l=0;l<ss+n_size-1;l++)
      {
route[l] = stack[l];

       //printf(" p[%c]=%c",65+stack[k],65+stack[k+1]);
      }
    }
  }
else
  { 
  //int ret = -1;
  //int min = INFINITY;
  //inttdst;
for(int i=0;i<n_size;i++)
  {
intind = 0;                    
if(w[sr][i] != 0)
    {
    //  printf("\n Checking ss= %d sr='%c' i='%c' :",ss,65+sr,65+i);         
for(int j=0;j<ss;j++)    
      {
      //  printf(" %c",65+stack[j]);   
if(stack[j] == i)
        {
ind = -1;         
        }    
      } 
     // printf(" (%d)",min); 

if(ind != -1)
      {
      // printf(" .. VALID .. ");     
stack[ss] = i;     
       //ret = 0;    
       //printf(" w[%c][%c] = %d",65+sr,65+i,w[sr][i]);    
findshort(i,ct-1,ss+1);      
       } 
      }
    }
  } 

}

void MSG()
{
chars,d;
intsi,di;   
printf("\nEnter the source node : ");
scanf("%c",&s);
fflush(stdin);
si = (int) s-65;
stack[0] = si;
s_size = 1;
min = INFINITY;
findshort(si,n_size-1,1);
printf("\n The shortest distance from '%c' : %d",s,min);
getch();
printf("\n The shortest path : ");
for(int k=0;k<n_size;k++)
  {
printf(" %c",65+route[k]);
  }
getch();
}

int main()
{
// clrscr();
in_dat();
dis_dat();
MSG();
return 0;
}

Sunday, April 8, 2012

Strassen's Multiplication


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

int main()
{
  int a[2][2],b[2][2],c[2][2],i,j;
  int m1,m2,m3,m4,m5,m6,m7;
  printf("Enter the 4 elements of first matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

  printf("Enter the 4 elements of second matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&b[i][j]);

  printf("\nThe first matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",a[i][j]);
  }
  printf("\nThe second matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",b[i][j]);
  }
  m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);  
  m2= (a[1][0]+a[1][1])*b[0][0];
  m3= a[0][0]*(b[0][1]-b[1][1]);
  m4= a[1][1]*(b[1][0]-b[0][0]);
  m5= (a[0][0]+a[0][1])*b[1][1];
  m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
  m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

  c[0][0]=m1+m4-m5+m7;
  c[0][1]=m3+m5;
  c[1][0]=m2+m4;
  c[1][1]=m1-m2+m3+m6;
   printf("\nAfter multiplication using \n");
   for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",c[i][j]);
   }
   getch();
   return 0;
}

Sunday, April 1, 2012

1's Complement of a Number



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

 void main()
  {
int n;
scanf("%d",&n);
int bin[20];    // Binary Equivalent Number of 'n'
int k = 0;
int i;
while(n != 0)
{
 bin[k] = n % 2;    // Computing Binary Equivalent Number of 'n' in Reverse Order
 k++;
 n = n/2 ;
}

 printf("\n\n");
for(i=k-1;i>=0;i--)
 printf(" %d",bin[i]);

for(i=0;i<k;i++)
 bin[i] = (bin[i]==1)?0:1;  // Complementing Binary Equivalent Number of 'n'

printf("\n\n");
for(i=k-1;i>=0;i--)
 printf(" %d",bin[i]);

i = 0;
n = 0;
int prod = 1;

while(i<k)
{
 printf(" %d",prod);
 n = n + (bin[i]*prod);               // Computing 'n' from Binary Equivalent Number
 prod *= 2;

 ++i;
}
printf("\n %d",n);

getch();
}

Sunday, October 9, 2011

Adding of Two Numbers in Binary Form


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

 void main()
  {
    clrscr();
     int i,num1,num2,num3=0,nd1=0,nd2=0,nd3=0,n1,n2,n3,c=0;
     long bin1=0,bin2=0,bin3=0,pval,b1,b2,b3;
     printf("\n Enter any number : ");
     scanf("%d %d",&num1,&num2);
      for(pval=1;num1!=0;num1/=2,pval*=10)
 bin1 += (num1%2)*pval;

      for(pval=1;num2!=0;num2/=2,pval*=10)
 bin2 += (num2%2)*pval;

     printf("\n\n Their Binary equivalents are : ");
     printf("%ld %ld",bin1,bin2);
     b1=bin1;
     b2=bin2;
     while(b1)
      {
b1/=10;
nd1++;
      }
     while(b2)
      {
b2/=10;
nd2++;
      }

     printf("\n\n Now , adding both these number in binary format ...");
     printf("\n\n %10ld \n +%9ld \n =",bin1,bin2);

    if( nd1>nd2 )
    {
      for(i=0;i<nd1;i++)
{
 n1=bin1%10;
 n2=bin2%10;
  if(c==0)
   {
     if( (n1==0 && n2==1) || (n2==0 && n1==1) )
{
  n3 = 1;
  c = 0;
}
     else if(n1==0 && n2==0)
{
  n3 = 0;
  c = 0;
}
     else
{
  n3 = 0;
  c = 1;
}
   }

  else
   {
    if( (n1==0 && n2==1) || (n2==0 && n1==1) )
{
  n3 = 0;
  c = 1;
}
     else if(n1==0 && n2==0)
{
  n3 = 1;
  c = 0;
}
     else
{
  n3 = 1;
  c = 1;
}

   }
 bin3 += (long) (n3*pow(10,i));
 bin1 /= 10;
 bin2 /= 10;
}
    }


    else
    {
      for(i=0;i<nd2;i++)
{
 n1=bin1%10;
 n2=bin2%10;
  if(c==0)
   {
     if( (n1==0 && n2==1) || (n2==0 && n1==1) )
{
  n3 = 1;
  c = 0;
}
     else if(n1==0 && n2==0)
{
  n3 = 0;
  c = 0;
}
     else
{
  n3 = 0;
  c = 1;
}
   }

  else
   {
    if( (n1==0 && n2==1) || (n2==0 && n1==1) )
{
  n3 = 0;
  c = 1;
}
     else if(n1==0 && n2==0)
{
  n3 = 1;
  c = 0;
}
     else
{
  n3 = 1;
  c = 1;
}
   }

 bin3 += (long)(n3*pow(10,i));
 bin1 /= 10;
 bin2 /= 10;
}
    }
    if(c==1)
     bin3 += pow(10,i);
    printf("%9ld",bin3);
    b3 = bin3;
    while(b3)
     {
       b3 /= 10;
       nd3++;
     }
    printf("\n\n The result is therefore : ");
    for(i=nd3;i>0;i--)
     {
       long x = pow(10,i-1);
       int y = bin3 / x;
       num3 += ( y * pow(2,i-1) );
       bin3 %= x;
     }
    printf("%d",num3);
    getch();
  }

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