Showing posts with label C Programming Stuff. Show all posts
Showing posts with label C Programming Stuff. Show all posts

August 26, 2017

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

April 30, 2014

Introduction to Two Dimensional arrays | C Programming

05:23 Posted by Freblogg No comments
C Programming Language allows for the use of multidimensional arrays.
An array what we generally mean is a one - dimensional array. An array can be 1-Dimensional, 2-Dimensional, 3-Dimensional, .... so on. In this particular post (assisted by the YouTube video ) we'll learn about the Two dimensional arrays.
Two dimensional arrays will have 2 dimensions, i.e, it is extended in both horizontal and vertical directions, unlike a 1-D array which is represented in a linear form.
And, just as we can access individual elements in a 1-Dimensional array, we'll be able to access each location in the array, using the respective indices (row number, column number).
A two dimensional array with 'a' rows and 'b' columns can be defined like this.
type name_of_array [a] [b] ;
That will create a 2-Dimensional array in the memory. And, you can address the locations in this array using two indices rather than one (as in a 1-D array).
http://durgaswaroop.blogspot.in/2014/04/introduction-to-two-dimensional-arrays.htmlSo, the addressing would be something like the following (for a array named serica)
serica [0] [0]
serica [0] [1]
serica [0] [2]

February 10, 2014

How to Format Placeholders? | C Programming

08:24 Posted by Freblogg No comments
Placeholders are used in printf and scanf. They act as a holder , a place for the value of the variable. In this tutorial i'll show you how you can format them so as to change the way the value of the variable gets displayed .This is just formatting the way it is displayed but thid won't change the actual value of the variable.



For more videos in this tutorial series,
C Programming Playlist - Click this
For my YouTube channel , Click here


April 29, 2013

Return Values of printf() and scanf()

13:03 Posted by Unknown No comments
           Most of people who know C Programming knows about printf( ) and  scanf() functions .We start using them when we begin learning C programming just assuming they are standard input and output functions ....but when we  come to learn functions ...sometimes we tend to look down upon these functions and neglect them as we have used them so many times. But that is a mistake.

if(mistake==1)
printf("Let's correct our mistake by reading this Post");
else
printf("Just skip this post...so many posts are awaiting you");
printf ( ) ?

April 24, 2013

C program for Anagram checking

18:46 Posted by Unknown , No comments
      The below program is to check whether two words are anagrams or not.The concept used is that we store the frequency of an alphabet in a word .By checking those counts we determine whether they are anagrams  or not ....count same == anagrams.

#include <stdio.h>
#include<conio.h>
int anagramchecker(char [],char []); //declaration of funtion
int main()
{
   char word1[50], word2[50];
   printf("Enter first word:\n");
   gets(word1);
   printf("Enter second word:\n");
   gets(word2);
   anagramchecker(word1,word2); //calling function to check anagrams
   getch(); return 0;
}

Restart Your Computer Using C Program

You can easily do all your typical system operations using C programming. This article will show you how to restart from C programming console. Just Compile and execute the following program.

#include <stdio.h>
#include <stdlib.h>
 main()
{
   char Restart;
 
   printf("Do you want to restart your computer now (y/n)\n");
   scanf("%c",&Restart);
 
   if (Restart== 'y' ||Restart == 'Y')
      system("C:\\windows\\System32\\shutdown /r"); //for windows 7
//  system("C:\\windows\\System32\\shutdown -r"); for windows xp

/* for ubuntu linux
      system("shutdown -"your option") */

   return 0;
}

 This program calls the system function of "stdlib.h" which is used to execute  shutdown.exe which is present in C:\windows\system32 in Windows XP,Windows 7. For more options take a look at this.


Copying folders and files

03:04 Posted by Unknown , 3 comments
#include<stdio.h>
#include<stdlib.h>
int main ()
   {
 int i;
 printf ("\n");
 i = system(" XCOPY [source path] [destination path] [options] ");
 printf ("Returned value is: %d.\n",i);
        return 0;
   }

Copy files and/or directories to another folder. XCOPY is similar to the COPY

Know Your Current Directory | Using C Programming

02:05 Posted by Unknown , No comments
The below program shows the usage of system() function to list down all the files and directories in the current directory:
#include<stdio.h>
#include<stdlib.h> //system( ) is included in this library

int main ()
    {
       int i;
       printf ("\n");
       i=system ("dir");// on windows
       // i=system ("ls"); on linux or unix platforms
       printf ("Returned value is: %d.\n",i);
       return 0;
    }
Another way

System( ) Function in stdlib.h Library

00:39 Posted by Unknown , No comments
The C library function system() passes the command name or program name specified by a command to the host environment to be executed by the command processor. After the execution of the command it returns a value depending on the status of the execution. 

Declaration: 
To use this function,
 int system( const  char *command )    
         When you call this function, It  will invoke the command processor to execute a command. If the command execution is terminated the processor will trasnfer the control back to the program that has called the system command.

Parameters:
                 String containing name of system command or requested variable. 

Return Value:
            It will return an integer value, but the interpretation of that is system dependant. So, on different systems it gets interpreted differently. If an error occured in the program execution, '-1' is returned, Ootherwise it returns the status of command.

Click here to know how you can restart your computer using system function. 

As, always, Have a happy reading and Stay Awesome !
-------------------------------------------------------------------------------------------------
Follow our blog posts @ Follow. So that you won't miss any interesting post and also to be the first to know the answers to many interesting questions.
Follow us on our Facebook  page @ Fre Blogg 
Head over to my You Tube channel for some interesting tutorials @ You Tube

April 22, 2013

Random Numbers Generation

23:03 Posted by Unknown , No comments
Random Number generation is very important aspect of many games, Cryptographic key generation and for other programs with a lot of outcomes. Let us take a look at how we generate those in C language and PHP. 
Before you proceed to the explanation, you need to know that a computer can never generate a Random number. It has to use some parameters to generate that number and so such numbers can never truly be Random. So, they are called Pseudo-Random Numbers and this tutorial deals with these. 

We have a built-in library function namely 'int rand(void )' in C. It gives the random number between 0 and RANDMAX. 
The value of RANDMAX is entirely library dependent, but it is guaranteed to be at least 32767 on any standard library implementation.

Then how to get numbers in the range we want ?
Let's say we want numbers only in between 100 and 1000. It is not directly possible with rand().
We have to use some of our math skills here with the use of  the operator , modulo (%).
I am very sure that you have used this operator before. It gives the remainder when one number is divided by another. (Check this tutorial if you are unsure)

The classy part of using this operator is that the remainder is always less than divider. So, the expression "X % 101" (X is some number) gives always a number less than 101 and greater than or equal to zero.

Let's come to our question , to generate numbers in between 100(101=min) and 1000(999=max) ,we need the expression
                                      result = rand( ) % 899 +101
where 899 is max-min+1 (999-101+1).

November 14, 2012

November 10, 2012