Sunday, December 23, 2018

PROGRAM TO DISPLAY TRANSPOSE OF A MATRIX

/*PROGRAM TO DISPLAY TRANSPOSE OF A MATRIX*/

#include<stdio.h>
#include<conio.h>
void main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];
   clrscr();
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);

   printf("Enter the elements of matrix\n");

   for (c = 0; c < m; c++)
      for(d = 0; d < n; d++)
         scanf("%d",&matrix[c][d]);

   for (c = 0; c < m; c++)
      for( d = 0 ; d < n ; d++ )
         transpose[d][c] = matrix[c][d];
   printf("Transpose of entered matrix :-\n");

   for (c = 0; c < n; c++) {
      for (d = 0; d < m; d++)
         printf("%d\t",transpose[c][d]);
      printf("\n");
   }

   getch();
}

OUTPUT:-
sk.

PROGRAM TO DISPLAY MULTIPLICATION OF TWO MATRIX

/*PROGRAM TO DISPLAY MULTIPLICATION OF TWO MATRIX*/

#include<stdio.h>
#include<conio.h>
void main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];
  clrscr();
  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");

  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
      scanf("%d", &first[c][d]);
   
  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if (n != p)
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");

    for (c = 0; c < p; c++)
      for (d = 0; d < q; d++)
        scanf("%d", &second[c][d]);

    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++) {
        for (k = 0; k < p; k++) {
          sum = sum + first[c][k]*second[k][d];
        }

        multiply[c][d] = sum;
        sum = 0;
      }
    }

    printf("Product of entered matrices:-\n");

    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++)
        printf("%d\t", multiply[c][d]);

      printf("\n");
    }
  }

  getch();
}


Output:-
sk.

PROGRAM TO DISPLAY ADDITION OF TWO MATRIX

/*PROGRAM TO DISPLAY ADDITION OF TWO MATRIX*/

#include <stdio.h>
#include<conio.h>
void main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];
   clrscr();
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");

   for (c = 0; c < m; c++)
      for (d = 0; d < n; d++)
         scanf("%d", &first[c][d]);

   printf("Enter the elements of second matrix\n");
      for (c = 0; c < m; c++)
      for (d = 0 ; d < n; d++)
            scanf("%d", &second[c][d]);

   printf("Sum of entered matrices:-\n");

   for (c = 0; c < m; c++) {
      for (d = 0 ; d < n; d++) {
         sum[c][d] = first[c][d] + second[c][d];
         printf("%d\t", sum[c][d]);
      }
      printf("\n");
   }

   getch();
}

OUTPUT:-
sk.

PROGRAM TO DISPLAY LINEAR SEARCH

/*PROGRAM  TO  DISPLAY  LINEAR  SEARCH*/

#include<stdio.h>
#include<conio.h>
void main()
{
   int array[100], search, c, n;
   clrscr();
   printf("Enter the number of elements in array\n");
   scanf("%d",&n);

   printf("Enter %d integer(s)\n", n);

   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);

   printf("Enter the number to search\n");
   scanf("%d", &search);

   for (c = 0; c < n; c++)
       {
      if (array[c] == search)     /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
         break;
      }
   }
   if (c == n)
      printf("%d is not present in array.\n", search);

   getch();
}

OUTPUT:-
sk.

PROGRAM TO MERGE AN ARRAY

/*PROGRAM  TO  MERGE  AN  ARRAY*/

#include<stdio.h>
#include<conio.h>
void merge(int [], int, int [], int, int []);

void main()
 {
  int a[100], b[100], m, n, c, sorted[200];
  clrscr();
  printf("Input number of elements in first array\n");
  scanf("%d", &m);

  printf("Input %d integers\n", m);
  for (c = 0; c < m; c++)
{
 scanf("%d", &a[c]);
  }

 printf("Input number of elements in second array\n");
 scanf("%d", &n);
 printf("Input %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &b[c]);
  }

  merge(a, m, b, n, sorted);

  printf("Sorted array:\n");

  for (c = 0; c < m + n; c++)
{
    printf("%d\n", sorted[c]);
  }

  getch();
}

void merge(int a[], int m, int b[], int n, int sorted[])
{
  int i, j, k;

  j = k = 0;

  for (i = 0; i < m + n;)
 {
    if (j < m && k < n)
 {
      if (a[j] < b[k])
{
        sorted[i] = a[j];
        j++;
      }
      else {
        sorted[i] = b[k];
        k++;
      }
      i++;
    }
    else if (j == m)
 {
    for (; i < m + n;)
 {
        sorted[i] = b[k];
        k++;
        i++;
      }
    }
    else
{
      for (; i < m + n;)
{
        sorted[i] = a[j];
        j++;
        i++;
      }
    }
  }
}


Output:-
sk.

PROGRAM TO SWAP TWO NUMBERS

/*PROGRAM  TO  SWAP  TWO  NUMBERS*/

#include<stdio.h>
#include<conio.h>
void main()
{
 int x, y, temp;
 clrscr();
 printf("Enter the value of x and y\n");
 scanf("%d%d", &x, &y);

 printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 temp = x;
  x    = y;
  y    = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
getch();
}


Output:-
sk.

PROGRAM TO SWAP TWO NUMBERS

/*PROGRAM  TO  SWAP  TWO  NUMBERS*/

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
void main()
{
char first[100], second[100], *temp;
clrscr();
printf("Enter the first string\n");
gets(first);
printf("Enter the second string\n");
gets(second);
printf("\nBefore Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n\n",second);
temp = (char*)malloc(100);
strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);
printf("After Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n",second);
 getch();
}

OUTPUT:-
sk.

Wikipedia

Search results

Search The program