Wednesday, 24 February 2016

C prgram to show that C= A * Transpose(B) where A,B,C are 3x3 matrices

#include <stdio.h>

int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], transpose[10][10], multiply[10][10];

  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]);

  // transpose of second matrix

  for (c = 0; c < m; c++)
for( d = 0 ; d < n ; d++ )
transpose[d][c] = second[c][d];

  // multiply A * Transpose(B)

    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++) {
        for (k = 0; k < p; k++) {
sum = sum + first[c][k]*transpose[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");
    }
  }

  return 0;
}

C prgram to find out nPr and nCr

#include <stdio.h>

long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);

int main()
{
   int n, r;
   long ncr, npr;

   printf("Enter the value of n and r\n");
   scanf("%d%d",&n,&r);

   ncr = find_ncr(n, r);
   npr = find_npr(n, r);

   printf("%dC%d = %ld\n", n, r, ncr);
   printf("%dP%d = %ld\n", n, r, npr);

   return 0;
}

long find_ncr(int n, int r) {
   long result;

   result = factorial(n)/(factorial(r)*factorial(n-r));

   return result;
}

long find_npr(int n, int r) {
   long result;

   result = factorial(n)/factorial(n-r);

   return result;
}

long factorial(int n) {
   int c;
   long result = 1;

   for (c = 1; c <= n; c++)
      result = result*c;

   return result;
}

C program to find the second largest element of an array

#include <stdio.h>
#include<stdlib.h>

int sl(int a[], int n){
int max, sec_max,i;
if(a[1]<a[0]) {
max=a[0];
sec_max=a[1];
}else {
max=a[1];
sec_max=a[0];
}
for( i=2;i<n;i++) {
if(a[i]>max) {
sec_max=max;
max=a[i];
}
if(a[i]<max && a[i]>sec_max) {
sec_max=a[i];
}
}
return sec_max;
}


int main()
{
    int p;
int array[]= {1,4,8,10,14,9,7,6,5,3,2};//{1,2,5,4,3,0};   //{2,1,6,5,4,8};
int n = sizeof(array)/sizeof(array[0]);
p=sl(array, n);
printf("Second largest element is %d",p);
return 0;
}

C program to print the sum of each row and each column of a matrix using dynamic array

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

void main()
{
int **a,r,c,i,j,sum=0;
clrscr();
//Accepting size of row and column
printf("Enter size of row n");
scanf("%d",&r);
printf("Enter size of column n");
scanf("%d",&c);

//Allocating memory to array
a=(int **)malloc(r*sizeof(int *));
for(i=0;i<r;i++)
a[i]=(int *)malloc((c+1)*sizeof(int));

printf("Enter array elements n");

for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]); //Accepting array elements from user

 for (i = 0; i < r; i++)
    {
 for (j = 0; j < c; j++)
        {
sum = sum + a[i][j] ;
        }
        printf("Sum of the %d row is = %d\n", i, sum);
        sum = 0;
    }
    sum = 0;
for (j = 0; j < c; j++)
    {
 for (i = 0; i < r; i++)
        {
sum = sum + a[i][j];
        }
        printf("Sum of the %d column is = %d\n", j, sum);
        sum = 0;
}

getch();
 }