Archive for the ‘Arrays’ Category

Transpose of a Matrix

Posted: May 11, 2010 in 2D Arrays
Tags:

/* Program to print the transpose of a matrix */

#include<stdio.h>

void transpose(int [][10],int [][10],int,int);

main()
{
int a[10][10],t[10][10],m,n;
int i,j;
printf(“\nEnter the order of the matrix: “);
scanf(“%d%d”,&m,&n);
printf(“\nEnter the elements ……….\n”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
transpose(a,t,m,n);
printf(“\nTranspose of the matrix is ……….\n”);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf(“%2d”,t[i][j]);
printf(“\n”);
}
}

void transpose(int a[][10],int t[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
t[j][i]=a[i][j];
}

Advertisements

/* Program to perform matrix addition */

#include<stdio.h>

void matrixAddition(int[][10],int[][10],int[][10],int,int);
void readMatrix(int[][10],int,int);
void printMatrix(int[][10],int,int);

main()
{
int a[10][10],b[10][10],sum[10][10],n,m;
int i,j;
printf(“Enter the order of the matrices: “);
scanf(“%d%d”,&m,&n);
printf(“\nEnter the first matrix…..\n”);
readMatrix(a,m,n);
printf(“\nEnter the second matrix…..\n”);
readMatrix(b,m,n);
matrixAddition(a,b,sum,m,n);
printf(“\nSum of matrices ……\n”);
printMatrix(sum,m,n);
}

void readMatrix(int a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
}

void printMatrix(int a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(“%d  “,a[i][j]);
printf(“\n”);
}
}

void matrixAddition(int a[][10],int b[][10],int sum[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
sum[i][j]=a[i][j]+b[i][j];
}

/* Program to multiply two matrices */

#include<stdio.h>

void readMatrix(int [][10],int m,int n);
void printMatrix(int [][10],int m,int n);
void multiply(int [][10],int [][10],int [][10],int m,int n,int p,int q);

main()
{
int a[10][10],b[10][10],mul[10][10],m,n,p,q;
printf(“\nEnter the order of the first matrix: “);
scanf(“%d%d”,&m,&n);
printf(“\nEnter the matrix……..\n”);
readMatrix(a,m,n);
printf(“\nEnter the order of second matrix: “);
scanf(“%d%d”,&p,&q);
printf(“\nEnter the matrix……..\n”);
readMatrix(b,p,q);
if(n!=p)
printf(“\nMultiplication is not possible!!\n”);
else
{
multiply(a,b,mul,m,n,p,q);
printf(“\nThe product array is…….\n”);
printMatrix(mul,m,q);
}
}

void readMatrix(int a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
}

void printMatrix(int a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(“%2d “,a[i][j]);
printf(“\n”);
}
}

void multiply(int a[][10],int b[][10],int mul[][10],int m,int n,int p,int q)
{
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
mul[i][j]=0;
for(k=0;k<n;k++)
mul[i][j]=mul[i][j]+a[i][k]*b[k][j];
}
}

Sum of N Numbers

Posted: February 13, 2010 in Arrays
Tags: ,

/* Program to find the sum of n numbers */

#include<stdio.h>

main()
{
int n,a[20],i,sum=0;
printf(“\nEnter the value of n: “);
scanf(“%d”,&n);
printf(“\nEnter the numbers………\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
sum+=a[i];
printf(“\nSum: %d\n\n”,sum);
}

Selection Sort

Posted: February 13, 2010 in Arrays
Tags:

/* Program to perform selection sort */

#include<stdio.h>

void selectionSort(int [],int);

main()
{
int a[20],n,i;
printf(“\nEnter the no. of elements: “);
scanf(“%d”,&n);
printf(“\nEnter the elements…….\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
selectionSort(a,n);
printf(“\nThe array after sorting…….\n”);
for(i=0;i<n;i++)
printf(“%d “,a[i]);
printf(“\n\n”);
}

void selectionSort(int a[],int n)
{
int i,pos,j,small,temp;
for(i=0;i<n-1;i++)
{
small=a[i];
pos=i;
for(j=i;j<n;j++)
if(small>a[j])
{
small=a[j];
pos=j;
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}

/* Program to remove duplicates from an array */

#include<stdio.h>

int removeDuplication(int[],int);

main()
{
int a[20],n,i,nodup;
printf(“\nEnter the no. of elements in the array: “);
scanf(“%d”,&n);
printf(“\nEnter the elements……..\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
nodup=removeDuplication(a,n);
if(nodup>0)
{
n-=nodup;
printf(“\nThe array after removing duplication……\n”);
for(i=0;i<n;i++)
printf(“%d  “,a[i]);
}
else
printf(“\nNo duplication found!!”);
printf(“\n\n”);
}

int removeDuplication(int a[],int n)
{
int i,temp,j,k,nodup=0;
for(i=0;i<n;i++)
{
temp=a[i];
for(j=i+1;j<n;j++)
if(a[j]==temp)
{
for(k=j;k<n;k++)
a[k]=a[k+1];
nodup++;
n–;j–;
}
}
return nodup;
}

Pascal’s Triangle

Posted: February 13, 2010 in Arrays
Tags: ,

/* Prograam to print Pascal’s Triangle */

#include<stdio.h>

void pascalTriangle(int);
void space(int);

main()
{
int n;
printf(“\nEnter no. of rows: “);
scanf(“%d”,&n);
printf(“\nThe Pascal Triangle is….\n\n”);
pascalTriangle(n);
}

void pascalTriangle(int n)
{
int a[20],i,j,temp[20];
for(i=0;i<=n;i++)
a[i]=0;
a[1]=1;
for(i=1;i<=n;i++)
{
space(n-i);
temp[0]=0;
for(j=0;j<i;j++)
{
temp[j+1]=a[j]+a[j+1];
printf(“%d “,a[j]+a[j+1]);
}
for(j=0;j<=i;j++)
a[j]=temp[j];

printf(“\n”);
}
}

void space(int n)
{
int i;
for(i=0;i<n;i++)
printf(” “);
}