Showing posts with label Terminal. Show all posts
Showing posts with label Terminal. Show all posts

August 12, 2017

Matrix Multiplication | C Programming

We have discussed about Matrix arithmetic in my recent youtube tutorial.


In that, I have already shown how Matrix addition and subtraction can be done and asked you to try multiplication themselves. Hopefully you have been able to do that Or atleast tried it out before coming here to see my solution.

For this example, I have taken two matrices as follows.

                                       

Matrix1 has 3 rows and 4 columns and Matrix2 has 4 rows and 3 columns. And now, the code to multiply these two matrices.
#include "stdio.h"
main() {

    int matrix1[3][4] = { {1,2,3,4}, {3,4,1,2}, {4,1,2,3} };
    int matrix2[4][3] = { {0,1,2}, {1,2,0}, {2,0,1}, {1,0,2} };
    
    // Values from the matrices. No. of columns in matrix1 = No. of rows in matrix2. Matrix Multiplication possible
    int rows1 = 3, cols1 = 4, rows2 = 4, cols2 = 3;        
    
    // Initialize the resultant matrix with all zeroes
    int mult_matrix[3][3] = {0};
    
    // Loop variables
    int i=0, j=0, k=0;
        
    for(i = 0; i < rows1; i++) {
        for(j = 0; j < cols2; j++) {
            for(k = 0; k < cols1; k++) {
                // Since we are initializing with zero, we can add new values to the previous value to get the final value
                mult_matrix[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
    
    //  Print the new matrix
    for(i = 0; i < rows1; i++) {
        for(j = 0; j < cols2; j++) {
            printf("%d ", mult_matrix[i][j]);
        }
        printf("\n");
    }
}

And when you run this program, you get the following output as one would expect:
12 5 13
8 11 11
8 6 16

Press any key to continue . . .
The comments, along with the explanation in the video should be enough for you to understand this code directly.But, do reach out on social media for any queries.

That's all for this article.
As, always, Have a happy reading and Stay Awesome !

If you have liked this article and would like to see more, subscribe to our Facebook and G+ pages.

Facebook page @ Facebook.com/freblogg

Google Plus Page @ Google.com/freblogg

June 18, 2016

Quick Vim Tips

23:29 Posted by Freblogg , , , , No comments
freblogg-vim-image
Vim is one of the most powerful text editors available. And, hence it is not really possible for everyone to know everything or get the same ideas on improving their work experience. And, so this article includes a few tips and handy shortcuts that will help your productivity just as we have been doing in the Vim series, but individually not extensive enough to get their own dedicated article.

So, here are some useful tips for Vim

Using Xargs | Superuse your Terminal

23:07 Posted by Freblogg , , , , No comments
Xargs is one of the really useful Linux commands that every one should know about. It is a great command that simplifies your task at hand and overall, a  great command to  have in your arsenal. And, its quite simple to use.




So, What is xargs?
Xargs is a command that helps you run a command on a series of things in order.  The easiest way to understand this is by looking at an example. Before, that here is the typical command structure when you use xargs.

<Some Input generator> | ... | xargs <command to run>

Bash Keyboard Shortcuts | ReadLine Tips

22:43 Posted by Freblogg , , , , No comments
Read Line is a editor every body uses but no one knows about. If you have used Linux for anything before, you have used ReadLine. Doesn't ring any bells? Well, that is because, not a lot of people call it that. It is more frequently termed as the "Bash Prompt" or something along those lines.
dsp@freblogg:$ |
So, this prompt you see every time you open a terminal, has got a name. Its called ReadLine. A really lax name, isn't it?  No wonder it didn't catch on.
So anyway, here are a few keyboard shortcuts to help you type efficiently in ReadLine and become a Con-fu master!

October 16, 2015

How to use Aliases in Linux | Superuse your Terminal

14:18 Posted by Freblogg , , , , , , No comments
Well, let’s face it, Linux is cool. Using Linux makes you look cool. But, for new users, may be not so much. Typing those long  commands can be a little intimidating and may even scare them off.
Wouldn’t it be awesome if there was a way, so that you won’t have to type long commands? Or even better, to create your own new commands by which you can do a lot of stuff?
You are in luck, because Aliases help you with that and much more.
 

What are Aliases anyway? 
Alias, as the name suggests is an alternate form of a previously existing command(s). So basically you are creating your own custom command with which you can do a lot of functions.
Enough with the definitions, let’s see an example.
Say you want to go to the Desktop folder. You would have to do something like this,

 [dsp@freblogg]$:~ cd /home/dsp/Desktop
How about instead of  typing all that, you just say desk like this and it takes you to Desktop?