Monday, November 17, 2014

Matrices using Recursion Pyramid



C Program to print number border rectangular pyramid

#include
#include
int main()
{
int number, row, col;
    printf("\nEnter Number of Rows to be display : \n");
    scanf("%d", &number);
    for (row = 1; row <= number; row++)
     {
        for (col = 1; col <= number; col++)
           {
            if (row == 1 || row == number)
            {
                printf("1\t");
            }
               else if (col == 1 || col == number)
               {
                printf("1\t");
               }
            else
               {
                printf("0\t");
            }
        }
        printf("\n");
    }
     return 0;
}

Output

Enter Number of Rows to be display : 6
1    1    1    1    1    1    
1    0    0    0    0    1    
1    0    0    0    0    1    
1    0    0    0    0    1    
1    0    0    0    0    1    
1    1    1    1    1    1  

C Program to print decreasing rectangular pyramid


#include
#include
int main()
{
int number, row, col;
int displaynum;
printf("\n Enter number of rows to be display:\n");
scanf("%d",&number);
for(row=1;row<=number;row++,displaynum--)
{
for (col=1;col<=num;col++)
printf("%d\t",displaynum);
printf("\n");
return 0;
}

Output

Enter number of rows to be display : 5
5        5        5        5        5
4        4        4        4        4
3        3        3        3        3
2        2        2        2        2
1        1        1        1        1

C Program to Print the Rectangle Pyramid of Numbers

#include
#include
int main()
{
printf("\n Enter number of rows to be display: ");
scanf("%d",&number);
for(row=1;number>=row;row++)
{
for(col=1;col<=number;col++)
printf("%d\t”,row);
printf(“\n);
}
return 0;
}

Output

Enter number of rows to be display: 5
1        1        1        1        1
2        2        2        2        2
3        3        3        3        3
4        4        4        4        4
5        5        5        5       5       

C Program to Multiply two Matrices using Recursion

#include
#define max 10
void matrixmultiply(int[max][max], int[max][max]);
int row1, row2, col1, col2;
int res[max][max];
int main()
{
   int mat1[max][max], mat2[max][max], i, j, k;
   printf("Enter the row and column of first matrix: ");
   scanf("%d%d", &row1, &col1);
   printf("Enter the row and column of second matrix: ");
   scanf("%d%d", &row2, &col2);
    if (col1 != row2)
  {
      printf("Matrix multiplication is not possible");
   }
        else
     {
      printf("Enter the First matrix: ");
      for (i = 0; i < row1; i++)
        {
         for (j = 0; j < col1; j++)
        {
            scanf("%d", &mat1[i][j]);
         }
      }
      printf("Enter the Second matrix: ");
      for (i = 0; i < row2; i++)
       {
         for (j = 0; j < col2; j++)
          {
            scanf("%d", &mat2[i][j]);
         }
      }
      printf("\nThe First matrix is: n");
      for (i = 0; i < row1; i++)
       {
         printf("\n");
         for (j = 0; j < col1; j++)
         {
            printf("%d ", mat1[i][j]);
         }
      }
       printf("\nThe Second matrix is: n");
      for (i = 0; i < row2; i++)
        {
         printf("\n");
         for (j = 0; j < col2; j++)
         {
            printf("%d ", mat2[i][j]);
         }
      }
      matrixMultiply(mat1, mat2);
   }
   printf("\nThe multiplication of two matrixes is : \n");
   for (i = 0; i < row1; i++)
    {
      printf("\n");
      for (j = 0; j < col2; j++)
      {
         printf("%d ", res[i][j]);
      }
   }
   return 0;
}

void matrixMultiply(int a[max][max], int b[max][max])
{
   static int sum, i = 0, j = 0, k = 0;
   if (i < row1)
   {
         if (j < col2)
         {
         if (k < col1)
          {
            sum = sum + a[i][k] * b[k][j];
            k++;
            matrixMultiply(a, b);
         }
         res[i][j] = sum;
         sum = 0;
         k = 0;
         j++;
         matrixMultiply(a, b);
      }
      j = 0;
      i++;
      matrixMultiply(a, b);
   }
}

Output

Enter the row and column of first matrix: 2 2
Enter the row and column of second matrix: 2 2
Enter the First matrix: 1 2 3 4
Enter the Second matrix: 1 2 1 2

The First matrix is:

1       2
3       4
The Second matrix is:

1       2
1       2
The multiplication of two matrixes is:

3       6
7       14

No comments:

Post a Comment