Introduction

The main purpose of this blog is to share our experience with the world and get experience from the world. This will be a new venture for us and we are sure, we shall learn a lot with it. we will try to Explore our mind which will be very helpful for all us. As from its name, its a blog covering all the topics. we will try to post all the interesting topics and give our opinion. Do give your point of view in comments.


"NAVIGATE VIA BLOG ARCHIVE ON RIGHT TO GET WHAT YOU ARE LOOKING FOR"


Friday, October 14, 2011

C Code For System Of Linear Equations Solver

//this is the program  for gaussian elimination method for solving system of equations

#include<stdio.h>         //preprocessor library used in the program

      //prototypes
void starter();           //this function displays the start of the program
void getvalues(int R,int C,float A[][C+1]);//this function get values of augmented values
void printmatrix(int R,int C,float A[][C+1]);           //function  will print the matrix
void Swap(int R,int C,float A[][C+1]);        //function will swap the rows of the matrix
float Echelon(int R1,int C1,float A1[][C1+1]);//function will convert matrix in echelon form
float Infinite(int R2,int C2,float A2[][C2+1],float X1[]); //function for infinite sol. case
float Unique(int R3,int C3, float A3[][C3+1],float X2[]);        //function for no sol. case
void solution(int R,int C,float A[][C+1],float X[]);   //function  shows the solution result
void endresult();                                          //function which ends the program

int main()                                                     //main function starts here
{
    starter();                                                   //call to starter function
    int i,j,R,C;                                    //declaration of variables used in main
              //now it will take user input for how many unknown variables of each equation
    printf("\n\tEnter Number of unknown variables.\n\t");                    //ask the user
    scanf("   %d",&R);                                            //get the value from user
    C=R;                      //no.of unknown vaariables is always equal to no of equations
    float A[R][C+1];          //declaration of array which contains the augmented matrix
    float X[10]={0};          //declaring array which contains row to swap
    getvalues(R,C,A);         //call for getting the input for augmented matrix
    printf("\tThe augmented matrix of the system of equations is\n");
    printmatrix(R,C,A);        //call to show the matrix before echelon form
    Swap(R,C,A);               //call to check the matrix for any zero value and then swap
    Echelon(R,C,A);            //call for converting in echelon form
    printf("\tAfter reducing the matrix in echelon form\n");
    printmatrix(R,C,A);        //print the matrix after echelon form
    solution(R,C,A,X);         //call to display the final result
    endresult();               //call to end the program
       return 0;               //ensures program ends
}
//main bloack ends

          //function  for starting page starts here
void starter()//definition of the funtion starter of void type
{
     //the very first welcoming screen display
    printf("\n_______________________________________________________________");
    printf("\n!!      ****************** ASSLAM U ALAIKUM ******************  !!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!      \t WELCOME TO THE WORLD OF LINEAR EQUATIONS \t!!");
    printf("\n!!      \t YOU CAN SOLVE ANY KIND OF EQUATION USING \t!!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!      \t \tGAUSSIAN ELIMINATION METHOD \t\t!!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!      \t                                            \t!!");
    printf("\n!!______________________________________________________________!!\n");
    printf("\n!!      ----------------GENERAL INSTRUCTION-------------------  !!");
    printf("\n!!      1)Number of equations and variables should be equal.    !!");
    printf("\n!!      2)Gaussian method is applicable for square matrices.    !!");
    printf("\n!!      3)Equations  in the form of matrix are as AX=B  \t!!\n!!\t where\t\t\t\t\t\t\t!!\n!!\t  A is the matrix of coefficients.\t\t\t!!");
    printf("\n!!        B is matrix for constants.\t\t\t\t!!");
    printf("\n!!        X is the unknown matrix which contains variables.     !!");
    printf("\n!!______________________________________________________________!!\n");
   //general instructions for the user are also included so that he could understand
   // what he is doing
}
         //starter function ends here
         //definition of getvalues function starts here
    //it will get the co efficient of the variables and adjust them in an array
void getvalues(int R,int C,float A[][C+1])
{
     int i,j;   //declare i for row check and j for colomn check
     printf("\n\tEnter coefficients which are the elements of matrix A\n");
     int equno=0;  //asking user for input and equno is used to checkfor no of equation
     for (i=0; i<R; i++)     //this for loop will move through each row
     {
         equno++;        //increment in variable
         printf("\tequation %d \n",equno); //show equation no.whose values are entered
         for (j=0;j<C; j++)  //for loop will move through each coloumn
         {
             printf("\t  A[%d][%d]   ",i+1,j+1); 
             scanf("%f",&A[i][j]);    //takes user input and adjust in the array
          }
          printf("\n");    //switch lines
      }
      printf("\tEnter Elements Of Matix B\n");  //asks for values in constant matrix
      for (i=0; i<R; i++)      //loop for rows
      {
          for (j=C; j<C+1; j++)  //loop for coloumn
          {
              printf("\t  B[%d][%d]   ",i+1,j+1);
              scanf("%f",&A[i][j]);   //takes input and adjust in last colomn of array
          }
          printf("\n"); // switch lines
      }
}//getvalues function ends here

    //function for printing matrix
   //printing the values of the array in augmented matrix
void printmatrix(int R,int C,float A[][C+1])
{
     int i,j;  //declare i for row check and j for colomn check
     for (i=0; i<R; i++) //this for loop will move through each row
     {
         for (j=0; j<C+1; j++)   //loop for coloumn
         {
             if(j!=C)  //print the values of matrix A
             {printf("\t%.1f  ",A[i][j]);}
             if(j==C)  //prints the vaalues of matrix B
             {printf("|\t%.1f  ",A[i][j]);}
         }
             printf("\n");//switch lines
     }
     printf("\n\n");//switch lines
}//printmatrix funcion ends here

   //function for swapping rows
  //this swap rows if there is zero in the diagonal
void Swap(int R,int C,float A[][C+1])
{
      int i,j,Counter1,Counter2,Counter3,Temp2,red=0;  //declaration variables
      float Temp[C+1];      //declare an array for swaping assistance
      for (i=0; i<R; i++)      //loop for rows
      {
           for (j=0; j<C+1; j++)      //loop for coloumn
           {    if (i==j)    //checks only for element in diagonal
                {   if  (A[i][i]==0)   //check for 0 in diagonal
                    {   for (Counter3=0; Counter3<R; Counter3++)
                        {    if (A[Counter3][j]!=0)    
                             {
                                 Temp2=Counter3;
                              }
                         }
                         {
                                 for (Counter2=0; Counter2<C+1; Counter2++)
                                  {   //this will swap the rows
                                       Temp[Counter2]=A[i][Counter2];
                                       A[i][Counter2]=A[Temp2][Counter2];
                                       A[Temp2][Counter2]=Temp[Counter2];
                                   }  
                         }
                      }
                  }
             }
        }
}//swap function will end here

  //echelon funcion starts here
  //it will convert matrix in upper trangular form
float Echelon(int R1,int C1,float A1[][C1+1])
{    
      int i,j,Pass1,Pass2,Pass3,K3;  //declaration variables
      //K3 for storing divisor  //all pass1,2,3 for sorting assistance
      Pass2=0;  // initialization of variables

      for (j=0; j<C1; j++)  //loop for rows
      {
          Pass2++;  //increment
          K3=1;     //initalize divisor at 1

          for (i=Pass2; i<R1+1; i++)  //loop for coloumn
          {
              if (j==i-1)      //
              {
                         float K1=A1[i-1][j];  //stores  value for division
                         for (Pass1=0; Pass1<C1+1; Pass1++) //check for rows
                         {
                             if (i==R1 && A1[R1-1][C1-1]==0 && A1[R1-1][C1]!=0)
                             { 
                                                   break;
                             }
                             if (i==R1 && A1[R1-1][C1-1]==0 && A1[R1-1][C1]==0)
                             {
                                                   break;
                             }
                             A1[j][Pass1]=A1[j][Pass1]/K1;
                         }
              }
              if (i==R1)
              {
                    break;
              }
              float K2=A1[i][j];

              for (Pass3=0; Pass3<C1+1; Pass3++)
              {
                  A1[i][Pass3]=A1[i][Pass3]-(K2*A1[i-K3][Pass3]);
              }
              K3++;
          }
       }
}  //echelon function ends

// infinite function starts
//function willoperate for infinte solutions
float Infinite(int R2,int C2,float A2[][C2+1],float X1[])
{
      int i,j,Pass6,Pass7;
      Pass6=0;
      for (i=0; i<(R2-1); i++)
      {
                 Pass6++;
                 if (Pass6>1)
                 {
                             for (j=1; j<Pass6; j++)
                             {
                                 int Pass7;
                                 Pass7=A2[R2-(i+2)][C2-1];
                                 X1[i]=X1[i]-Pass7-X1[i-1]*A2[R2-(i+2)][C2-(j+1)];
                                 if(j==1)
                                 {
                                         Pass7=0;
                                 }
                             }
                 }
                  else
                  {
                      X1[i]=-A2[R2-2][C2-1];
                   }
        }
}//infinite  function ends here

    //function for unique solution
    //back substitution of unique answers held here
float Unique(int R3,int C3, float A3[][C3+1],float X2[])
{
     int i,j,Pass4,Pass5;  //declaration variables
     Pass4=0;

     for (i=0; i<R3; i++)   //loop for rows
     {
         Pass4++;
         if (Pass4>1)
         {
                     for (j=1; j<Pass4; j++)  //loop for coloumn
                     {
                         X2[i]=X2[i]+A3[R3-(i+1)][C3] -X2[j-1]*A3[R3-(i+1)][C3-j];
                         if (j==1)
                         {
                                  A3[R3-(i+1)][C3]=0;
                          }
                     }
          }
         else
         {
             X2[i]=A3[R3-1][C3];
         }
     }
} //unique function ends here

  //function for displaying answrs starts
  //will show the respective solution of the matrices
void solution(int R,int C,float A[][C+1],float X[])
{
     int i,j;      //declare i for row check and j for colomn check
     printf("\n\t******HERE IS THE ANSWER******\t\n");
     if (A[R-1][C-1]==0 && A[R-1][C]!=0)  //condition for no solution
     {
                        printf("\n\n\t The given system of equation has no solution\n");
     }
     if (A[R-1][C-1]==0 && A[R-1][C]==0)  //condition for infinite solution
      {
                         Infinite(C,R,A,X); //calls infinite function
                         printf("\n\tThe given system of equation has Infinite Solutions\n\t");
                         for (i=0; i<(R-1); i++)  //loop for rows
                         {
                               printf("\tX[%d] = %.2ft\n",i+1,X[R-(i+2)]);
                          }  //displays the result of infinite answer
                               printf("\tX[%d] = t\n",R);
                               printf("\tWhere   -Infinity < t < +Infinity\n");
       }
       if (A[R-1][C-1]!=0)// condition for unique solution
       {
                          Unique(R,C,A,X);//calls for unique function
                          printf("\tEach unknown variable has unique solution\n");
                          for (i=0; i<R; i++) //loop for rows
                          {
                              printf("\t\tX[%d] = %.2f \n",1+i,X[R-i-1]);
                           } //display result for unique solutions
       }      
}   // solution function ends here

  //this will display the good bye screen
  //and will show the group members
void endresult() 
{
     printf("\n\n\t**************THANK YOU FOR USING THIS PROGRAM*****************");
     }//function end result will end here
//the program will end

Thursday, October 13, 2011

C Code For Rat In a Maze Game

//In this code the rat moves by itself

#include <math.h>
#include <stdio.h>
#include <time.h>
int x,y;
int main ()
{
int i,j,z,v,s,b,c,q=0,m,n,o,u;
char a[x][y];
for (i=0;i<x;i++)
{for (j=0;j<y;j++)
{a[i][j]='X';}}
a[x-1][0]='C';
a[x-1][y-1]='R';
z=x/2;
v=x;
for (i=0;i<=z;i++)
{
a[v-1][1]='T';
v--; }
s=y/2;
b=2;
for (i=0;i<(y-3);i++)
{
a[x/2][b]='T';
b++;
}
c=x/2;
for(i=0;i<=z;i++)
{
a[c][y-2]='T';
c++;}
for (i=0;i<(x-1);i++)
{for (j=0;j<y;j++)
    {q=rand()%2;
    if (a[i][j]!='T')
    {{if (q==0)
     {a[i][j]='o';}
    if (q==1)
     {a[i][j]='X';}}       
         }}}
for (i=0;i<x;i++)
{for(j=0;j<y;j++)
{if (a[i][j]=='T')
    a[i][j]='o';}}
for(i=0;i<x;i++)
{for (j=0;j<y;j++)
{printf("%c ",a[i][j]);} printf("\n");  }
u=a[x-1][y-1];
n=x-1;
o=y-1;
for(i=0;i>-1;i++)
{  
sleep (1);
system("clear");

//left
if ((a[n][o-1]!='X') && o>0 && a[n][o-1]!='1')
{ a[n][o-1]='R'; a[n][o]='1';
    for(i=0;i<x;i++)
    {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
    o=o-1;if (a[n][o]==a[x-1][0])
    {printf("\n\t\t\t\tYOU WON\n"); break;}
  // left (force right)
  if (o==0 && a[n+1][o]=='X' && a[n-1][o]!='X')
   {a[n][o+1]='R'; a[n][o]='1';
    sleep(1);
    system("clear");
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
    o=o+1;}
continue;
}

//down
if((a[n+1][o]!='X') && n<(x-1) && a[n+1][o]!='1')
{  a[n+1][o]='R';
   a[n][o]='1';
   for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
   printf("%c ",a[i][j]); printf("\n");}
n=n+1;if (a[n][o]==a[x-1][0])
{printf("\n\t\t\t\tYOU WON\n"); break;}
   // down (force right)
if (a[n+1][o]=='X') {a[n][o+1]='R'; a[n][o]='1';
    sleep(1);
    system("clear");
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
o=o+1;} continue; }

//up
{if ((a[n-1][o]!='X') && n>0)
{ a[n-1][o]='R';
  a[n][o]='1';
  for(i=0;i<x;i++)
  {for (j=0;j<y;j++)
  printf("%c ",a[i][j]); printf("\n");}
n=n-1;
   if (a[n][o]==a[x-1][0])
      {printf("\n\t\t\t\tYOU WON\n"); break;}
  // up (force down)
  if (a[n-1][o]=='X' && a[n][o-1]=='X' && a[n][o+1]=='X')
      {a[n+1][o]='R';
      a[n][o]='1';
      for(i=0;i<x;i++)
      {for (j=0;j<y;j++)
      printf("%c ",a[i][j]); printf("\n");}
      n=n+1; if (a[n][o+1]!='X') {a[n][o+1]='R'; a[n][o]='1';
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
o=o+1;} }

continue;}}

//right
if((a[n][o+1]!='X') && (o<(y-1)) )
{a[n][o+1]='R'; a[n][o]='1';
    for(i=0;i<x;i++)
   {for (j=0;j<y;j++)
    printf("%c ",a[i][j]); printf("\n");}
o=o+1;
if (a[n][o]==a[x-1][0])
{printf("\n\t\t\t\tYOU WON\n"); break;} continue;
}

// force down

if (a[n+1][o]=='1')

      {a[n+1][o]=='R'; a[n][o]=='S';}

  }

}// end loop
}//end main       

Related Posts with Thumbnails