Tuesday 12 February 2013

Write a c program to calculate the sum of elements of upper triangle of a nXn matrix using DMA



#include<stdio.h>
#include<conio.h>
  void main()
{
  int **ip,m,n;
  int sum=0,i=0,j=0;
  clrscr();
  printf("\nEnter the row and column: ");
  scanf("%d%d",&m,&n);
  ip=(int**)malloc(m*sizeof(int));
  for(i=0;i<m;i++)
  ip[i]=(int*)malloc(n*sizeof(int));
  printf("\n");
  printf("\nEnter the elements: ");
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
{
  scanf("%d",&ip[i][j]);
}
}
  printf("\nEntered elements are:\n");
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
{
  printf("\t%d",ip[i][j]);
}
  printf("\n");
}
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
{
  if(i<=j)
  sum=sum+ip[i][j];
}
}
  printf("\nSum upper triangle is = %d",sum);
  getch();
}

OUTPUT:

Enter the row and column:
3
3

Enter the elements:
1
2
3
4
5
6
7
8
9

Entered elements are:
      1       2       3
      4       5       6
      7       8       9

Sum upper triangle is = 26

Write a c program to reverse an array elements using D.M.A.



#include<stdio.h>
#include<conio.h>
  void main()
{
  int *ip,n,i;
  int max=0;
  clrscr();
  printf("\nEnter the limit: ");
  scanf("%d",&n);
  ip=(int*)malloc(n*sizeof(int));
  printf("\nEnter the elements:\n");
  for(i=0;i<n;i++)
{
  scanf("%d",&ip[i]);
}
  printf("\nEntered elements are:\n");
  for(i=0;i<n;i++)
{
  printf("\n%d",ip[i]);
  printf("\n");
}
  printf("\nReverse array is:");
  for(i=n-1;i>=0;i--)
{
  printf("\n%d",ip[i]);
}
  getch();
}

OUTPUT:

Enter the limit: 4

Enter the elements:
1
2
3
4

Entered elements are:1 2 3 4

Write a c program to accept mXn matrix from user and display the elements of given matrix using function



#include<stdio.h>
#include<conio.h>
  void main()
{
  void mat(int,int);
  int r,c;
  clrscr();
  printf("\n Enter the limit row \n");
  scanf("%d",&r);
  printf("\n Enter the coloum col \n");
  scanf("%d",&c);
  mat(r,c);
  getch();
}
  void mat (int r,int c)
{
  int a[10][20];
  int i=0,j=0;
  printf("\nEnter the matrix element \n");
  for(i=0;i<r;i++)
{
  for(j=0;j<c;j++)
{
  scanf("%d",&a[i][j]);
}
}
  printf("\nThe entered matrix is:\n\n");
  for(i=0;i<r;i++)
{
  for(j=0;j<c;j++)
{
  printf("\t%d",a[i][j]);
}
  printf("\n");
}
}

OUTPUT:

Enter the limit row
 3

Enter the coloum col
3
Enter the matrix element
1
2
3
4
5
6
7
8
9

The entered matrix is:

    1       2       3
    4       5       6
   7       8       9

Wednesday 6 February 2013

Write a c program to calculate sum of two mXn matrices & store the result in 3 matrix



#include<stdio.h>
#include<conio.h>
  void main()
{
  int m[10][10],n[10][10],p[10][10],i,j;
  clrscr();
  printf("\nEnter the elements of m: ");
  for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
{
  scanf("%d",&m[i][j]);
}
  printf("\n");
}
  printf("\nEnter the elements of n: ");
  for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
{
  scanf("%d",&n[i][j]);
}
  printf("\n");
}
  printf("\nEntered elements of m is:\n");
  for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
{
  printf("\t%d",m[i][j]);
}
  printf("\n");
}
  printf("\nEntered elements of n is:\n");
  for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
{
  printf("\t%d",n[i][j]);
}
  printf("\n");
}
  printf("\nAddition of matrix is: ");
  for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
{
  p[i][j]=m[i][j]+n[i][j];
}
  printf("\n");
}
  for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
{
  printf("\t%d",p[i][j]);
}
  printf("\n");
}
  getch();
}

//OUTPUT:

//Enter the elements of m:
1
2
3
4
5
6
7
8
9

//Enter the elements of n:
1
2
3
4
5
6
7
8
9

//Entered elements of m is:
      1       2       3
      4       5       6
      7       8       9

//Entered elements of n is:
      1       2       3
      4       5       6
      7       8       9

//Addition of matrix is:
      2       4       6
      8       10      12
      14      16      18