Monday, 31 December 2012

C/C++ Puzzles: PART - 13

The Problem with Implicit Type Conversion

  In C and C++ implicit type conversion is often performed by the Compiler when two unequal data types are used in an expression. In most of the cases implicit conversion works fine. But sometimes it might lead to unexpected results and chaos. For example consider the program given below.

#include<stdio.h>
int main()
{
   int a= -1;
   unsigned int b= 1;
   if(a<b)
  {
     printf("\nThis is the expected Output, since a<b!!!\n");
  }
  else
  {
     printf("\nThis is the actual Output!!!\n");
  }
  return 0;
}


Download.

The expected Output is the message "This is the expected Output, since a<b", but the actual Output is the second message "This is the actual Output".
The Figure Below Shows the Output
Why such an Output? The reason is the implicit type conversion by the Compiler. The integer a (By default integer is signed) is converted to an unsigned int and then it is compared with the unsigned integer b. Now the value of b is 1 and the value of a is a very large positive integer (4294967295) since the sign bit is also considered as a magnitude bit. Thus when compared a is larger than b. Hence the else part of the program is executed.    

Wednesday, 26 December 2012

C/C++ Puzzles: PART - 12

Stringizing Operator ( # )

  Stringizing Operator in C/C++ converts macro parameters to string literals without expanding the parameter definition. This is a Preprocessor operator in C/C++. The example program given below illustrates the working of Stringizing Operator.

#include<stdio.h>
#define String(i) #i
int main()
{
   int tot1=10,tot2=20,tot3=30,i,TotVal;
   TotVal=tot1+tot2+tot3;
   printf("\n\nThe Stringizing Operator is used to print TotVal.\n\n");
   printf("\nThe value of "String(TotVal)" is %d\n\n",TotVal);
}
Token Pasting Operator ( ## ) 

  This is another Preprocessor Operator in C/C++. The token pasting (##) operator eliminates any white space around it and concatenates the non-whitespace characters together. It can only be used in a macro definition. The example program given below illustrates the working of Token Pasting Operator.

#include<stdio.h>
#define join(a,b) a##b
int main()
{
   int tot1=10,tot2=20,tot3=30,i,TotVal;
   TotVal=tot1+tot2+tot3;
   printf("\n\nThe Token Pasting Operator is used to print TotVal.\n\n");
   printf("\nThe value of TotVal is %d\n\n",join(Tot,Val));
}


Download.

C/C++ Puzzles: PART - 11

The Obfuscated C Code
  
  What is obfuscation? Simply speaking it is the art of complicating simple things so that others won't understand your intentions. The same principle can be applied to programming. There are two ways of writing programs. The first one is to write simple code which can be understood by anyone. This is a good quality expected from a programmer working in a Team. The second approach is to write code which can not be understood by anyone. This may not be a highly desirable situation always. But there are many reasons why people write such obscure code. To protect the logic of the code, to hide malicious code, to participate in competitions etc are some reasons. There are contests where you have to write obscure code to win. One such contest is the International Obfuscated C Code Contest. Click here to view the official website of IOCCC. 

The C program I wrote below will give you an idea about Program Obfuscation.

#include <stdio.h>
#include <string.h>
#define _______ ;
#define What p##r##i##n##t##f(
#define is "%s\n"
#define This ,_____);}
#define ______ m##a############i##n
#define __o__ int
#define __0__ char
#define __O__ strcpy
#define ooo ++
#define oo /2
__o__ ______(__o__ __, __0__
*___[]){__o__
_=0;__0__ ____[32]_______ __0__ _____
[32] _______ __O__
(____,"aHbeglhl1oj oWuotrklmdq!b"
) _______
for (_=0 _______
_<26 _______ _ ooo )_____[_ oo ]
=____[++_] _______
What
is
This


Download.

The Output of the Program is Shown Below

I have used the Token-Pasting operator (##) and the fact that underscore ( _ ) is a valid identifier to obfuscate the C program.

I think the C program given below is even better.

#include<stdio.h>
#define _ ;
#define __ ,
#define ___ (
#define ____ )
#define _____ {
#define ______ }
#define _______ +
#define ________ printf
#define _________ scanf
#define __________ while
#define ___________ int
#define ____________ main
#define OO =
#define OOO 1
#define o "
#define ooo "%d"
#define oooo <=
#define o00o *
#define o0o &
#define o000o [
#define oo0oo ]
#define o0 char
o0 __0__ o000o 20 oo0oo OO _____ 69 __ 78 __ 84 __ 69 __ 82 __
32 __ 78  __ 79 __ 58 ______ _
o0 __00__ o000o 30 oo0oo OO _____ 70 __ 65 __ 67 __ 84 __
79 __ 82 __ 73 __ 65 __ 76 __ 32 __ 73  __ 83 __ 58 ______ _
___________ ____________ ___ ____
_____
  ___________ O OO OOO  __ OOOO __ OOOOO OO OOO _
  ________  ___ __0__ ____ _
  _________ ___ ooo __ o0o OOOO ____ _
  __________ ___ O oooo OOOO ____
  _____
    OOOOO OO OOOOO o00o O _
    O OO O _______ OOO _
  ______
  ________  ___ __00__ ____ _
  ________ ___ ooo __ OOOOO ____ _
______


The Output of the Program is Shown Below

Here also I used the fact that Underscore ( _ ) is a valid identifier in C. The program finds the factorial of a number. The messages are printed using the ASCII values of letters. That is the reason why you don't see any letters in the program.

Computer Science Tools: PART - 2


  Here I have given links to a few more useful tools for Computer Science enthusiasts. Most of them are Open Source Software, the rest are Freeware. 

  • Eclipse - Eclipse is an IDE for Java. It is one of the best available for Java development. Click here to download Eclipse.
  • MySQL - MySQL is one of the best Open Source relational database management system (RDBMS) available free of cost. Click here to download MySQL.
  • WampServer - WampServer (Apache, PHP, MySQL on Windows) is a Windows web development  environment. Click here to download WampServer.
  • Python - Python is a general-purpose high-level programming language. Click here to download the Python Interpreter. 
  • VirtualBox - If you want to run Linux over your Windows based system the best tool available is   Oracle VM VirtualBox. Running an Operating System using VirtualBox is entirely different from running the same OS using a Live CD. Click here to download Oracle VM VirtualBox. 
  • 7-ZIP - 7-ZIP is an Open Source file archiver. It can read and write almost all the archive formats and far far better than WinZip. Click here to download 7-ZIP.  

Computer Science Tools: PART - 1


  What ever is the career of an individual, a good professional is in need of the right tools to excel in his work. So what are the tools required by a Computer Science professional? Well, we need good Hardware and Software as tools to perform our duty in earnest. Obtaining costly hardware may not be a viable option to many. But the case of obtaining good software is different, it is often very cheap and some times free. But there is a lack of knowledge regarding good technical tools in Computer Science. Here in this post I have given links to a few useful Computing tools which are useful to both students and practitioners of Computer Science. Most of the tools I suggest are Open Source Software and the rest are Freeware. If you know any better alternatives, please suggest them.
  • Code::Blocks - This is the best I come across in 2012. It is an IDE for C/C++. It can be used on both Linux and Windows. This is especially useful for students working on Windows OS. The usual Turbo C++ IDE by Borland is a bore and will not work on Windows 7 and Windows 8. The last updated version of Turbo C++ was released on 1994. So it is high time to adopt a tool like Code::Blocks. If you are using Code::Blocks on Windows please install MinGW and set the path in Code::Blocks. Click here to download Code::Blocks. 
  • MinGW - MinGW (Minimalist GNU for Windows) is a gcc like compiler for C/C++ in Windows. If you are running Code::Blocks in Windows OS first you need to install MinGW in your system. Click here to download MinGW.    
  • Scilab Scilab is an open source tool for numerical computation. It is a free software alternative of MATLAB. If you are good with MATLAB, it will be very easy to shift to Scilab. Click here to download Scilab.
  • MikTex - Miktex is an excellent implementation of TeX/LaTeX and related programs for Windows. Miktex is available for all versions of Windows. Click here to download Miktex. 
  • Texmaker - Texmaker is a free cross platform Latex editor. Before installing Texmaker you should install Miktex in your system. Click here to download Texmaker.
  • TrueCrypt - If you want to hide some sensitive data in your system TrueCrypt is the best. It is a free open source disk encryption software for Windows, Linux and MacOS. Click here to download TrueCrypt.

C/C++ Puzzles: PART - 10


  Project Euler
   If you are interested in solving difficult problems in Computer Science, then please visit Project Euler. They offer problems whose solution requires both programming skills and mathematical knowledge. Some of the problems only need to use existing algorithms. But many often the complexity of the problem arises from the sheer magnitude of the numbers involved. To give you a flavor of the problems involved, I am reproducing a problem from Project Euler.


The problem can be solved in many different ways. I went for the easiest and least efficient technique. The brute force approach. I wrote a C program and it took 30 minutes to solve the problem in my computer.

Figure Below Shows the Output

Click here to download the C Program to solve the Highly Divisible Triangular Number problem.

Once you solve a particular problem the Project Euler Team is kind enough to provide you with algorithms to solve the problem. Here is one of the algorithms they offered to solve the problem of highly divisible triangular number.

 

C/C++ Puzzles: PART - 9


  Printing Numbers from 1 to 100 
(Without using If, Switch, For, While, Do While and Goto)

 Recently I come across the above mentioned problem. You have to write a C program to print numbers from 1 to 100. But you should not use If, Switch, For, While, Do While or Goto. Here are the three solutions I felt O.K.

//Solution 1
C is case sensitive. So is C++. So the restriction does not apply to the C/C++ keywords if, switch, for, while, do or goto. So you could use the keywords and write an ordinary C program to print the numbers.

//Solution 2
You could use printf( ) function to print the numbers. printf("1\n2\n3\n......99\n100");. May not be the best solution. But it is also a solution.

//Solution 3
Use the ternary operator (? : ) in C/C++. The program is given below.
#include<stdio.h>
int i=0;
int main()
{
   i++;
   printf("\n%d",i);
   (i>99)?i:main();
}


Download

//Solution 4
Use the Token Pasting operator in C/C++. Token Pasting operator allows you to merge separate tokens. The solution is given below.
#include<stdio.h>
#define LOOP wh##ile
int main()
{
   int i=1;
   LOOP(i<=100)
   {
      printf("\n%d",i);
      i++;
   }
}


There might be other solutions also. If you know any please post them. 

Tuesday, 25 December 2012

C/C++ Puzzles: PART - 8


The Problem with Side Effects in C/C++

  What is a side effect in a programming language? Well it is defined as the result of an operator, expression, statement, or function that persists even after the operator, expression, statement, or function has finished being evaluated. 

x=10;
x++;

The increment operator changes the value of x permanently. After the statement has finished executing, x will have the value 11. This is an example for side effects.

x=10;
cout<<(x+1);

There is no side effect here. The output will be 11. But the value of x is still 10.

The problem with side effects is that in C/C++ there is no standard order of evaluation as far as the left and right side of a single operator is concerned. For example in the statement C=A+B we don't know whether A will be accessed first or B will be accessed first. In some situations we might get unexpected results due to this. Consider the code given below.

a=10;
b=(++a * 2)+(++a * 4);
printf("Value is %d\n",b);

If the left hand side of the operator + is evaluated first we will get 70 as output. If the right hand side is evaluated first the output will be 68. Try the program on different compilers and you will understand the problems with side effects in C/C++. 

C/C++ Puzzles: PART - 7


Printing "Hello World" Without using Semi Colon

  How to write a C/C++ program which prints the message "Hello World" on the screen, which does not contain a semi colon. So you should write a program without a single semi colon.

//Solution 1
#include<stdio.h>
int main()
{
  if(printf("\nHello World!!!\n"))
  {
  }
}


//Solution 2
#include<stdio.h>
int main(int argc, char *argv[printf("\nHello World!!!\n")])
{
}

Download

//Solution 3
#include <stdio.h>
int main(void *Hello[printf("\nHello world!!!\n")]) {}

Download 

There might be other solutions. If you come across any such solution please post a comment.


C/C++ Puzzles: PART - 6

The Peculiar Behavior of sizeof
  
  sizeof is the only keyword in C/C++ which can be used as an operator. sizeof is used to find out the storage space required by a particular variable or constant. But there is a behavioral abnormality shown by the sizeof operator. Consider the program given below.

 #include<stdio.h>
int main()
{
    int a,b;
    a=10;
    b=sizeof(++a);
    printf("a is %d",a);
    printf("\nb is %d\n",b);
    return 0;
}

Download

The expected output is "a is 11" and "b is 4". But the actual output is "a is 10" and "b is 4". Reason? sizeof is an operator and inside which no expression is evaluated. If you are using Turbo C++ by Borland, the output will be "a is 10" and "b is 2" because the size of integer is 2.

The Figure Below Shows the Output


To clarify the doubts, consider the following program.

#include<stdio.h>
int main()
{
    int a,b,c,d;
    c=1000;
    a=10;
    b=20;
    d=sizeof(c=a+b);
    printf("c is %d",c);
    printf("\nd is %d\n",d);
    return 0;
}
 
 
Here also the expression inside the sizeof operator is not evaluated and the output is "c is 1000" and "d is 4". If the compiler used is Turbo C++ by Borland, then the output will be "c is 1000" and "d is 4" since the size of integer is 2 bytes. 
 
The Figure Given Below Shows the Output
 


C/C++ Puzzles: PART - 5


There are no Automatic Global Variables

 Consider the program given below.

#include<stdio.h>
auto int aaa;
int main()
{
    aaa=222;
    printf("Value is %d\n",aaa);
    return 0;
}



Download

When you compile the program you will get the following error, "file-scope declaration of 'aaa' specifies 'auto' ". Reason? Global variables should never be declared as automatic variables (auto).

The following program given below works properly.

#include<stdio.h>
int aaa;
int main()
{
    aaa=222;
    printf("Value is %d\n",aaa);
    return 0;
}

Download 

The Figure Given Below Shows the Output

C/C++ Puzzles: PART - 4


No Initialization for External Variables

Consider the program given below.

#include<stdio.h>
int main()
{
    extern int aaa=10;
    printf("Value is %d\n",aaa);
    return 0;
}

Download 

When you compile the program you will get the following error message, " 'aaa' has both 'extern' and initializer". Reason? You should not initialize an externally declared variable. 

The version given below corrects the error.

#include<stdio.h>
int main()
{
    extern int aaa;
    printf("Value is %d\n",aaa);
    return 0;
}
aaa=333;

The Figure Given below shows the Output


Monday, 24 December 2012

C/C++ Puzzles: PART - 3


Static Variables are Initialized at Compile Time

Consider the Two programs given below.

//Program 1
#include<stdio.h>
int aaa=10;
int main()
{
    static int bbb=aaa;
    printf("Value is %d\n",bbb);
    return 0;
}

Download

//Program 2
#include<stdio.h>
const int aaa=10;
int main()
{
    static int bbb=aaa;
    printf("Value is %d\n",bbb);
    return 0;
}


Download
 

Both the programs will give you the following error message on Code::Blocks 10.05, "Initializer element is not constant". Reason? Static variables are initialized at compile time. So either it should be initialized with a constant value or leave the initialization to the compiler, which will initialize the static variable to zero.

The program given below corrects the error.

#include<stdio.h>
#define aaa 10
int main()
{
    static int bbb=aaa;
    printf("Value is %d\n",bbb);
    return 0;
}

Download 

The program below declares bbb as an automatic variable and there are no errors. Remember the default storage class is automatic (auto).

#include<stdio.h>
int aaa=10;
int main()
{
    int bbb=aaa;
    printf("Value is %d\n",bbb);
    return 0;
}

Download

C/C++ Puzzles: PART - 2


Array Index should be a Constant Value

Consider the two programs given below.

// Program 1
#include<stdio.h>
int main()
{
    int max=10;
    char aaa[max]="\nHello";
    printf("%s",aaa);
}


Download 

// Program 2
#include<stdio.h>
int main()
{
    const int max=10;
    char aaa[max]="\nHello";
    printf("%s",aaa);
}

Download 


Both the programs will give you the following error message, "variable-sized object may not be initialized" on Code::Blocks 10.05. Reason? Initialization of variables should be done with constant values.

The following modified program works properly.


#include<stdio.h>
#define max 10
int main()
{
    char aaa[max]="\nHello";
    printf("%s",aaa);
}


Download

C/C++ Puzzles: PART - 1


  C/C++ puzzles are regularly asked in all the tests conducted for Computer Science students. GATE, Technical round of a job interview, Test for PhD admission what ever is the nature of the test being conducted there will be C/C++ puzzles. Many often what we think absolutely impossible becomes utterly convincing once the logic is explained. Here I have included some puzzles which are repeatedly asked in various tests. I have compile the programs using Code::Blocks 10.05. So if you use a different compiler some of the program's output may differ. But most of the programs will show the same output irrespective of the compiler used.

main( ) is also a Function

What will be the Output of the program given below.

#include<stdio.h>
int i=0;
int main()
{
    printf("\nHello");
    i++;
    if(i<10)
      main();
    return 0;
}

Download 

The Figure Shows the Output of the Program


The program will print Hello exactly 10 times. Because the Global variable i is common for all the main( ) function being called.

Now Consider the modified program given below.

#include<stdio.h>
int main()
{
    int i=0;
    printf("\nHello");
    i++;
    if(i<10)
      main();
    return 0;
}

You will get an infinite loop. Why? The variable i is not Global. Each main( ) function will have its own copy of the variable i. So the value of i never becomes greater than 10.

Now consider another modified version of the program.

#include<stdio.h>
static int main()
{
    int i=0;
    printf("\nHello");
    i++;
    if(i<10)
      main();
    return 0;
}

Download 

You will get an error. Why? The main( ) can not precede with a static keyword. But remember main( ) can precede with an extern keyword.
 

C/C++ Program illustrating Spatial Locality


  Spatial locality is the expectation of a Computer System that a particular program will use data from neighboring locations of already used data. C programming language uses a row major ordering to allocate array data. That means the data is stored row wise in contiguous memory locations. So spatial locality tells us to access data in a row wise order.  

Consider the two loops given below.

// LOOP 1
   for(i =0; i < 700; i++)
   {
      for(j=0; j< 700; j++)
      {
            a[ i ][ j ]=1;     // Here elements are accessed row wise.
       }
    }

// LOOP 2
   for(i =0; i < 700; i++)
  {
      for(j=0; j< 700; j++)
      {
            a[ j ][ i ]=1;     // Here elements are accessed row wise.
       }
    }

 Which loop will take more time to execute? Even though both the loops sets exactly 490000 locations to 1, the first loop is much more faster than the second loop. Reason is spatial locality and row major ordering of C programs. If you were executing the same algorithm in MATLAB the second loop will execute faster since MATLAB follows a column major ordering for storing array data.

The Figure Given Below Shows the Output of the C Program


Click here to download the C Program illustrating the principle of Spatial Locality. 
Click here to download the C++ Program illustrating the principle of Spatial Locality. 


Variable Length Argument List Function: C Program


  What is a variable length argument list function in C? An excellent example is the printf( ) and scanf( ) functions provided by the header file <stdio.h>. In printf( ) and scanf ( ), the number of arguments passed is not fixed. Now the question is whether we can write such functions? The answer is yes. To write we have to use the header file <stdarg.h>. There are three macros and one data type provided by the stdarg.h header file. va_start, va_end, va_arg are the macros and va_list is the data type.
  • va_list    - This data type is used to create an data object which can hold the values passed by the function.
  • va_arg   - This macro is used to access the values stored in the data object of va_list.
  • va_start - This macro initializes the data object of the va_list type.
  • va_end   - This macro denotes the normal exit from the function by freeing the data.
Click here to download the C Program illustrating Variable Length Argument List Functions.

Sunday, 23 December 2012

GATE Computer Science Question Papers: PART - 3


  Here I have included a few more question papers of GATE Examination in Computer Science and Engineering conducted for post graduate studies.  

GATE Computer Science Question Papers: PART - 2


  Here I have included a few more question papers of GATE Examination in Computer Science and Engineering conducted for post graduate studies. 


GATE Computer Science Question Papers: PART - 1


  Here I have included question papers of GATE Examination in Computer Science and Engineering conducted for post graduate studies.

Saturday, 22 December 2012

PhD Admission Test Question Papers: PART - 2


  Here I have given a few question papers of various Admission Tests conducted for PhD in Mathematics. Every test conducted for PhD in Computer Science and Engineering also contains a large number of questions from various Mathematical topics. Especially from areas like Linear Algebra, Discrete Mathematics, Probability, Graph Theory etc. So if a Computer Science student goes through these Mathematical problems, it might help him.

PhD Admission Test Question Papers: PART - 1


  Here I have given a few question papers and model papers of various Admission Tests conducted for PhD in Computer Science and Engineering. 

Inverse of a Matrix


  The Inverse of a matrix exists only if the determinant is non zero. For every non singular matrix there exists a unique Inverse. If A is a matrix, then the inverse of the matrix is denoted by A-1. The product of a matrix and its Inverse equals the identity matrix I. There for we have the relation AA-1 = A-1A = I. Here inverse is obtained by applying elementary row transformations on the given matrix and similar changes are also made on an identity matrix of the same order. When the given matrix is transformed into an Identity Matrix the Identity Matrix becomes the Inverse Matrix. 

Figure Given Below Shows a 3 X 3 Matrix.


 Figure Given Below Shows the Inverse of the Given Matrix.


Figure Given Below Shows the Product of the Matrix and its Inverse.


Click here to download the C Program to find the Inverse of a Matrix.
Click here to download the C++ Program to find the Inverse of a Matrix.

         

Thursday, 20 December 2012

Solving Linear Equations using Gauss Jordan Elimination


  Gauss Jordan elimination is performed on the coefficient matrix to make it a diagonal matrix. Corresponding changes are also made on the product matrix. The method is often used to solve system of linear equations. Elementary row operations are used to obtain the diagonal matrix in Gauss elimination method. The program may fail to produce a result if one of the diagonal elements is reduced to zero, even when there is a solution. In such a case please run the program again after changing the order of the equations entered into the coefficient matrix.

The Figure Given Below Shows the Output for Three Equations in Three Unknown Variables.


Click here to download the C Program to solve Linear Equations by applying Gauss Jordan Elimination.
Click here to download the C++ Program to solve Linear Equations by applying Gauss Jordan Elimination.

Gauss Jordan Elimination: C/C++ Program


  Gauss Jordan elimination is one of the many operations we can perform on matrices. Here I have provided a program which finds the diagonal matrix corresponding to the given matrix. It is often used to solve system of linear equations. Elementary row operations are used to obtain the diagonal matrix in Gauss Jordan elimination method.

A 3 X 3 Matrix



 The Figure Below Shows the Diagonal Matrix Obtained by Gauss Jordan Elimination.


Click here to download the C Program to perform Gauss Jordan Elimination.

Click here to download the C++ Program to perform Gauss Jordan Elimination.

Tuesday, 18 December 2012

Solving Linear Equations using Gauss Elimination


  Gauss elimination is performed on the coefficient matrix to make it an upper triangular matrix. Corresponding changes are also made on the product matrix.  Then back substitution is used to obtain the solutions. The method is often used to solve system of linear equations. Elementary row operations are used to obtain the upper triangular matrix in Gauss elimination method. The program may fail to produce a result if one of the diagonal elements is reduced to zero, even when there is a solution. In such a case please run the program again after changing the order of the equations entered into the coefficient matrix.

The Figure Below Shows the Output For Four Equations in Four Unknown Variables. 

Click here to download the C Program to solve Linear Equations by applying Gauss Elimination.
Click here to download the C++ Program to solve Linear Equations by applying Gauss Elimination.

Monday, 17 December 2012

Simple C++ Programs: PART - 5


  A few more simple programs in C++ for beginners. I have compiled the programs using an IDE called Code::Blocks. It will be better if you use the same IDE. Code::Blocks is a very good IDE which can be used for compiling C/C++ Programs in Windows 7.

Some Simple C Programs: PART - 5


 A few more simple programs in C for beginners. I have compiled the programs using an IDE called Code::Blocks. It will be better if you use the same IDE. Code::Blocks is a very good IDE which can be used for compiling C/C++ Programs in Windows 7.

Gauss Elimination: C/C++ Program


  Gauss elimination is one of the many operations we can perform on matrices. Here I have provided a program which finds the upper triangular matrix corresponding to the given matrix. It is often used to solve system of linear equations. Elementary row operations are used to obtain the upper triangular matrix in Gauss elimination method.

The C++ Program is given below.
#include<iostream>
#define MAX 10
using namespace std;
int lcm(int x,int y);
int main()
{
    int i,j,k,r,c,a[MAX][MAX],b[MAX][MAX],det=1,l,d1,d2;
    cout<<"\nEnter the number of Rows of the Matrix: ";
    cin>>r;
    cout<<"\nEnter the number of Columns of the Matrix: ";
    cin>>c;
    if(r==c)
    {
       cout<<"\nEnter the Elements of the Matrix:\n";
       for(i=0;i<r;i++)
       {
          for(j=0;j<c;j++)
          {
             cin>>a[i][j];
          }
       }
       for(i=0;i<r-1;i++)
       {
           for(j=i+1;j<r;j++)
           {
             l=lcm(a[i][i],a[j][i]);
             if(l!=0&&(a[i][i]!=0&&a[j][i]!=0))
             {
               l=(a[i][i]*a[j][i])/l;
               d1=l/a[i][i];
               d2=l/a[j][i];
               a[j][i]=0;
               for(k=i+1;k<r;k++)
               {
                 a[j][k]=(d2*a[j][k])-(d1*a[i][k]);
               }
             }
           }
        }
        cout<<"\nThe given Matrix after Gauss Elimination is:\n";
        for(i=0;i<r;i++)
        {
          for(j=0;j<c;j++)
          {
             cout<<a[i][j]<<"\t";
          }
          cout<<"\n";
        }
     }
     else
     {
        cout<<"\nThis is not a Square Matrix!!!\n";
     }
     return 0;
}
int lcm(int x,int y)
{
    int t;
    while (y != 0)
    {
      t=y;
      y=x%y;
      x=t;
    }
    return x;
}

Figure Given Below Shows a 3 X 3 Matrix.

After Performing Gauss Elimination the Matrix is Shown Below.

Click here to download the C Program to perform Gauss Elimination.

Click here to download the C++ Program to perform Gauss Elimination.


Wednesday, 12 December 2012

Determinant of a Matrix using Recursion



  A matrix is a rectangular array representation of numbers. Determinant exists only for square Matrices which have equal number of rows and columns. Here a recursive algorithm is used to find out the determinant of the given matrix.

                               

                            The Determinant of the given matrix is 0.

Click here to download the C Program to find the Determinant of a Matrix.
Click here to download the C++ Program to find the Determinant of a Matrix.