Friday 2 December 2016

Program to compute matrix determinant and inverse.

Hello everyone.
Once again its me with another program to share.

This program computes the inverse of a matrix using the same method as followed in mathematics.

This program uses a function  called determinant which recursively computes the determinant of a matrix.
Then the adjoint of the matrix is computed in another matrix. All the elements of the adjoint matrix are then divided by the determinant and the inverse is obtained.

Download the code here.





Like our facebook page here.

Monday 28 November 2016

Very Very large binary numbers to decimal.

Hey all!
I know this is getting repetitive (me posting programs which are all related to the number system.) but I promise the next post will be something different.

Meanwhile lets see the program.

In the previous post we made a program to convert a large decimal number to a binary number. Hence, we were able to convert very large numbers to their binary number.

The resulting binary numbers were also very large. So we needed something to convert them back to decimal numbers.

I have accomplished this again using arrays.

Here is my code.

The following screen shots verify that both the programs were running correctly.
I have used the previous program to convert a large decimal number to its binary equivalent which then I gave as input to this program.

See for yourself:





Hit that +1 if you like this post.

Come back often for more cool stuff.
Happy programming!

Saturday 26 November 2016

Very Very large decimal numbers to binary.

Hi all,
I said in my previous post that I'd be working on the problem of the range of input to the program for converting decimal numbers to binary numbers.
The largest number that the program could convert was the maximum number in the range of int. 
But, I worked on a solution that used a string as an input.

So basically, what the program does is as follows:
1. Take the input from the user as a character array named num_tmp (which is made using dynamic memory allocation as it is temporary).

Example :       "12"           //num_tmp

2. Then we have two more arrays namely num and bin. The former stores the actual input number in the form of separate digits. The latter stores the boutput binary number in the same manner but in reverse order(as we read the output binary number upside down while converting a decimal number to binary number).

But there is a problem in storing numbers as their digits - how to you know where the last digit is?
The solution is rather simple. I just added 100 to the integer array after all the digits were converted from string num_tmp (which is terminated by '\0' that helps identify the last digit.) and stored in num. Now the logic is that a single digit lies in the interval [0,9].
For the input in above example, 
                        {1, 2, 100,...}      //num
3. Then we start a loop with the condition that num is not equal to zero.

Now, how can we check if num is zero or not?
I defined a separate function for the purpose. What it does is check every digit of the array and if all the digits (before 100) are zeroes then the number is zero otherwise not.
Inside the loop we use primary school arithmetic to divide the number by 2 and simultaneously adding the remainders (0 or 1 - the binary digits) to the array called bin meant to store the result. A variable j that stores number of digits in bin is incremented and there is no need to terminate bin with 100.

*The remainders are calculated by modding the last digit with 2 (food for thought).

The arrays after the iterations of the loop are shown:

num : {1, 2, 100,...} is_zero(num) : 0    bin : {...}      j : 0
num : {0, 6, 100,...} is_zero(num) : 0   bin : {0,...}   j : 1
num : {0, 3, 100,...} is_zero(num) : 0   bin : {0, 0,...} j : 2
num : {0, 1, 100,...} is_zero(num) : 0   bin : {0, 0, 1,...} j :3
num : {0, 0, 100,...} is_zero(num) : 1   bin : {0, 0, 1, 1,...} j :4

The final result is 1100

Thats it!

Download the code here.

Here is the program in action:



Unfortunately the online decimal to binary converter supported upto 63 digit binary numbers so I can't verify the results over that. Converting this large binary numbers to decimal numbers is another problem.
I'll try to solve this problem in the next post.

However lets see how this program works with extremely large numbers:

  
 As you can see the program works with extremely large numbers. I'll try to make a program to be able to convert numbers this large back to decimal.

See you later. 
Happy coding!

P.S. C Seekers' Facebook page coming soon. Don't forget to like it.
 

Monday 21 November 2016

Convert large decimal numbers to their Binary equivalent.

The other day I stumbled upon this post on codechef which deals with calculating factorials of huge numbers (the factorials of which are way beyond the range of int or long int or long long int).

I noticed that the post suggests to use an array to store the result in form of single digits in an array. Thats when I remembered encountering the same problem while making a program to convert decimal numbers to binary numbers as the resulting numbers are way too large for any in built data type.

This program stores all the remainders in an array and a counter variable keeps track of the number of digits in the array.
Then after it is done calculating all the remainders it just prints them all on the screen in reverse order and voila! That does the job. See screen shot down below.

Unfortunately the largest number you can convert at the moment is 2147483647 as it is the largest number in the range of long int. I will try to solve this in my next post though. Till then Happy Coding.

Give it a go in ideone here.




Wednesday 2 November 2016

Decimal to Binary using RECURSION

So I came up with this program that I feel worth sharing.

Give it a go here: http://ideone.com/XTrHI8


Welcome!

After running a blog about Python language (see here), I have now started learning C language. This blog will be always updated with any program I make and feel worth sharing.

Feel free to comment about the contents of the posts.
...

Happy Coding!