facebook like button

29 April, 2011

program 37: matrix multiplication

The need:
     This is a simple program to multiply 2 matrices.
The code: 
--------------------------------------------
#include<stdio.h>
main()
{
    int j,i,k,m1,n1,m2,n2;
    int A[20][25],B[20][25],mul[20][25];         //declaration of 2 dimensional array
    printf("This is a program to get multiplication of two matrices A nad B\n\n");
    printf("Enter the rows in mat A..\n");
    scanf("%d",&m1);
    printf("Enter the columns in mat A..\n");
    scanf("%d",&n1);
    printf("Now keep on entering matrix elements.\n\n");
    for (i=0;i<m1;i++)
    {
        for (j=0;j<n1;j++)
        {
            printf("A%d%d=",i+1,j+1);
            scanf("%d",&k);
            A[i][j]=k;
        }
    }
   
    printf("\nEnter the rows in mat B..\n");
    scanf("%d",&m2);
    if(n1!=m2)
    {
        printf("\n\nDimension error.\nCan not multiply\nexiting...\n");
        exit(0);
    }
    printf("Enter the columns in mat B..\n");
    scanf("%d",&n2);
    printf("Now keep on entering matrix elements.\n\n");
    for (i=0;i<m2;i++)
    {
        for (j=0;j<n2;j++)
        {
            printf("B%d%d=",i+1,j+1);
            scanf("%d",&k);
            B[i][j]=k;
        }
    }
   
    printf("\n\nMatrix A is\n\n");
    for (i=0;i<m1;i++)
    {
        for (j=0;j<n1;j++)
        {
            printf ("%d ",A[i][j]);
        }
    putchar('\n');
    }
   
    printf("\n\nMatrix B is\n\n");
    for (i=0;i<m2;i++)
    {
        for (j=0;j<n2;j++)
        {
            printf ("%d ",B[i][j]);
        }
    putchar('\n');
    }
   
   
   
    for (i=0;i<m1;i++)            //initialisation of mat mul
    {
        for (j=0;j<n2;j++)
        {
             mul[i][j]=0;
        }
    }
   
   
    for (i=0;i<m1;i++)            //multiplication part
    {
        for (j=0;j<n2;j++)
        {
            for (k=0;k<n1;k++)
            {
                mul[i][j]+=A[i][k]*B[k][j];
            }
        }
    }
   
   
    printf("\n\nThe multiplied matrix is\n\n");
    for (i=0;i<m1;i++)
    {
        for (j=0;j<n2;j++)
        {
            printf ("%d ",mul[i][j]);
        }
    putchar('\n');
    }
    getchar();
}
-------------------------------------------- 

The approach: 
This program shows how one can multiply 2 matrices. Run the program. This program asks you to enter the number of rows and columns of the matrix A, then matrix A and similarly matrix B. This also prints both the matrices for user. Then the program multiplies the 2 matrices and prints the output matrix. For multiplication of 2 matrices to be defined number of columns of first matrix should be equal to number of rows of second matrix. In the program does not find those to be equal, it terminates. The 'exit()' functions cause the program to terminate. Read remark for details. Multiplication procedure implemented here is the same as you people follow when you multiply 2 matrices. To know more about how a matrix is stored and printed .see program36.


Remarks:

1. You have noticed that I have written a 0 in parentheses of 'exit()' function. Actually any integer could be written there or even it could be left blank.
2. The number helps in determining the site of termination of the program at run time when there are multiple 'exit()' statements in it.

27 April, 2011

program 36: matrix as 2 dimesional array

The need:
     In most calculations we need matrices. So for a C program to do calculations on matrices it should store the matrix first. This program just takes a matrix as input, stores that and prints back for user.
The code: 
--------------------------------------------
#include<stdio.h>
main()
{
    int j,i,k,m,n;
    int mat[20][25];         //declaration of 2 dimensional array
    printf("Enter the rows..\n");
    scanf("%d",&m);
    printf("Enter the columns..\n");
    scanf("%d",&n);
    printf("Now keep on entering matrix elements.\n\n");
    for (i=0;i<m;i++)
    {
        for (j=0;j<n;j++)
        {
            printf("Enter element %d%d.\n",i+1,j+1);
            scanf("%d",&k);
            mat[i][j]=k;
        }
    }
    printf("\nThe matrix is\n\n");
    for (i=0;i<m;i++)
    {
        for (j=0;j<n;j++)
        {
            printf ("%d ",mat[i][j]);
        }
    putchar('\n');
    }
}
--------------------------------------------
The approach: 
This program shows how one can implements 2 dimensional integer array to store a matrix in a C program. Run the program. This program asks you to enter the number of rows and columns of the matrix a and then the matrix and prints that matrix for user. The program uses 2 for loops for taking input from user and 2 loops for printing the output. Here I have assumed that you are very much familiar to nested loops(loop within a loop).

Remarks:
1. In this program you have seen use of 2 dimensional integer array. In C we can have array of any dimension for any data-type but their use is not that common.

program 35: printing the positions of a matrix

The need:
     This program takes takes dimensions of a matrix as input and prints all the positions of that matrix. Here position means the row and column of an element.
The code: 
--------------------------------------------
#include<stdio.h>
main()
{
    int j,i,m,n;
    printf("Enter the number of rows..\n");
    scanf("%d",&m);
    printf("Enter the number of columns..\n");
    scanf("%d",&n);
    for (i=0;i<m;i++)
    {
        for (j=0;j<n;j++)
        {
            printf ("%d%d ",i+1,j+1);
        }
    printf("\n");
    }
} 
--------------------------------------------
The approach: 
This program illustrates how we can print in 2 dimensional space(like matrix here). Run the program. This program asks you to enter the number of rows and columns of the matrix and then prints index of each element of that matrix. I did not make this program at that time but I have put its here intentionally because in next post I am going to tell you how to store a matrix in a C program. I am going to introduce the concept of 2 dimensional arrays which can store a matrix.

program 34: printing a triangle of stars(*)

The need:
     This program takes a whole paragraph as input and calculates total number of characters, words and lines.
The code: 
--------------------------------------------
#include<stdio.h>
main()
{
    int j,i,k;
    printf("Enter the number of stars in the base..");
    scanf("%d",&k);
    for (i=1;i<=k;i++)
    {
      for (j=0;j<i;j++)
      {
         printf ("*  ");
      }
      printf("\n");
    }
}
--------------------------------------------
The approach: 
This program illustrates how we can print in 2 dimensional space(like matrix in next posts). Run the program. This program asks you to enter the number of stars in the base. Give an integer from 1 to 30(so that you can see the output clearly). You can give a bigger integer but the shape of triangle may not be clearly visible at that number. As you can see the output, the program create a right angle triangle of stars. This is done by two nested for loops here. Each run of inner for loop cause a single * to be printed on screen. Each run of outer loop has i number of runs of inner loop. So each run of outer loop prints i number of stars on the screen followed by a new line.

program 33: program to count lines and words

The need:
     This program takes a whole paragraph as input and calculates total number of characters, words and lines.

The code: 
--------------------------------------------
//A program to count lines
#include<stdio.h>
#include<string.h>
main( )
{
    char line[500],ctr;
    int i,c,end=0,characters=0,words=0,lines=0;
    printf("key in text =>   ");
    while(end==0)
    {
        c=0;
        while((ctr=getchar())!='\n')
        line[c++]=ctr;
        line[c]='\0';
        if(line[0]=='\0')
        break;
        else
        {
            words++;
            for(i=0;line[i]!='\0';i++)
            if(line[i]==' '|| line[i]=='\t')
            words++;
        }
        lines=lines+1;
        characters=characters+strlen(line);
    }
    printf("\n");
    printf("no lines=> %d\n",lines);
    printf("no of words=> %d\n",words);
    printf("no of characters=> %d\n",characters);
}
--------------------------------------------
 
The approach:
First of all run the program. This program takes input until you press enter 2 times continuously thus allowing you to enter more than one line (recall that in previous programs whenever you gave characters as input to your program and pressed ENTER key it stopped taking more characters). After taking input the program gives the output.