Sunday, June 8, 2008

Matrix Operations


/* Subhranath Chunder - Matrix Operations */

/* A program to implement the following operation on two matrices:
a. Addition
b. Multiplication
c. Transposition
d. Determinant */

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

/* Function declarations */
void print(int[][100],int,int);
void add(int[][100],int,int,int[][100],int,int);
void multiply(int[][100],int,int,int[][100],int,int);
void transpose(int[][100],int,int);
int determinant(int[][100],int);

/* Main Program */
void main()
{
int inp1[100][100],inp2[100][100],m1,n1,m2,n2,i,j,option=0;
clrscr();

/* The values of the matrices are accepted */
printf("Enter the dimensions of the first matrix in the form mXn: ");
scanf("%dX%d",&m1,&n1);
printf("Enter the values of the first matrix:\n");
for(i=0;i<m1;++i)
{
for(j=0;j<n1;++j)
scanf("%d",&inp1[i][j]);
}
printf("\n\nEnter the dimensions of the second matrix in the form mXn: ");
scanf("%dX%d",&m2,&n2);
printf("Enter the values of the second matrix:\n");
for(i=0;i<m2;++i)
{
for(j=0;j<n2;++j)
scanf("%d",&inp2[i][j]);
}

/* Main Menu - where the user chooses what he wants to do */
while(option==0)
{
clrscr();
printf("You can perform the following operations on the matrices:\n\n1. Addition\n2. Multiplication\n3. Transposition\n4. Determinant\n5. Exit\n\nEnter Choice: ");
scanf("%d",&option);
clrscr();

/* Performs addition on the two matrices */
if(option==1)
{
add(inp1,m1,n1,inp2,m2,n2);
getch();
}

/* Performs multiplication on the two matrices */
if(option==2)
{
multiply(inp1,m1,n1,inp2,m2,n2);
getch();
}

/* Transposes a specified matrix */
while(option==3)
{
printf("Which matrix would you like to transpose?\n\n1. First Matrix\n2. Second Matrix\n\nEnter Choice: ");
scanf("%d",&option);
clrscr();
if(option==1 || option==2)
{
if(option==1)
transpose(inp1,m1,n1);
if(option==2)
transpose(inp2,m2,n2);
getch();
}
else
option=3;
}

/* Finds the determinant of a specified matrix */
while(option==4)
{
printf("Find the determinant of which matrix?\n\n1. First Matrix\n2. Second Matrix\n\nEnter Choice: ");
scanf("%d",&option);
clrscr();
if(option==1 || option==2)
{
if(option==1)
{
if(m1==n1)
printf("The determinant of the matrix is: %d",determinant(inp1,m1));
else
printf("The derterminant of the matrix cannot be determined.");
}
if(option==2)
{
if(m2==n2)
printf("The determinant of the matrix is: %d",determinant(inp2,m2));
else
printf("The derterminant of the matrix cannot be determined.");
}
getch();
}
else
option=4;
}

/* Exits the program */
if(option==5)
break;

/* Returns the control back to main menu */
option=0;
}
}
/* Main Program Ends */

/* Performs addition on the two specified matrices */
void add(int arr1[][100],int m1,int n1,int arr2[][100],int m2,int n2)
{
int arr3[100][100],i,j;
if(m1==m2 && n1==n2)
{
for(i=0;i<m1;++i)
{
for(j=0;j<n1;++j)
arr3[i][j]=arr1[i][j]+arr2[i][j];
}
printf("The sum of the two matrices is given below:\n\n");
print(arr3,m1,n1);
}
else
printf("The matrices cannot be added");
}

/* Performs multiplication on the two specified matrices */
void multiply(int arr1[][100],int m1,int n1,int arr2[][100],int m2,int n2)
{
int arr3[100][100],i,j,k;
if(n1==m2)
{
for(i=0;i<m1;++i)
{
for(j=0;j<n2;++j)
{
arr3[i][j]=0;
for(k=0;k<n1;++k)
arr3[i][j]+=arr1[i][k]*arr2[k][j];
}
}
printf("The product of the two matrices is given below:\n\n");
print(arr3,m1,n2);
}
else
printf("The matrices cannot be multiplied");
}

/* Transposes a specified matrix */
void transpose(int arr[][100],int m,int n)
{
int arr2[100][100],i,j;
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
arr2[j][i]=arr[i][j];
}
printf("The transpose of the matrix is given below:\n\n");
print(arr2,n,m);
}

/* Finds the determinant of a specified matrix */
int determinant(int arr[][100],int n)
{
int temp[100][100],a,b,result=0,i,j,k;
if(n==1)
return(arr[0][0]);
else
{
for(i=0;i<n;++i)
{
a=0;
b=0;
for(j=1;j<n;++j)
{
for(k=0;k<n;++k)
{
if(k!=i)
{
temp[a][b]=arr[j][k];
++b;
}
}
b=0;
++a;
}
result+=arr[0][i]*determinant(temp,n-1)*pow(-1,i);
}
}
return result;
}

/* Prints a matrix of specified dimension */
void print(int arr[][100],int m,int n)
{
int i,j;
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
printf("%d\t",arr[i][j]);
printf("\n");
}
}

A program demonstrating various operations on integer array


/* Subhranath Chunder - A program demonstrating various operations on integer array */

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

/* Funtion declarations */
void printarray1(int[],int);
void printarray2(int[],int);
int maximum(int[],int);
int minimum(int[],int);
void ascend(int[],int);
void descend(int[],int);
int search(int,int[],int);
void insertion(int,int,int[],int);
void deletion(int,int[],int);
void merge(int,int[],int,int[]);
void reverse(int,int[]);

/* Main Program */
void main()
{
int inp[100],n,option=0,i,temp1,temp2;
clrscr();

/* User inputs the elements of the array */
printf("Enter the no. of elements you would like to enter: ");
scanf("%d",&n);
printf("\nEnter the numbers: ");
for(i=0;i<n;++i)
scanf("%d",&inp[i]);

/* Main Menu - where the user chooses what he wants to do */
while(option==0)
{
clrscr();
printf("You can perform the following options on the array.\n\n1. Find the maximum and minimum elements.\n2. Sort.\n3. Search.\n4. Insertion.\n5. Deletion.\n6. Merging.\n7. Reversing.\n8. Exit\n\nEnter your option: ");
scanf("%d",&option);

/* Shows the maximum and minimum element of the array */
if(option==1)
{
clrscr();
printf("The maximum element of the array is: %d",maximum(inp,n));
printf("\n\nThe minimum element of the array is: %d",minimum(inp,n));
getch();
option=0;
}

/* Sort the array either in ascending or descending order */
while(option==2)
{
clrscr();
printf("Sort the array:\n\n1. In Ascending Order.\n2. In Descending Order.\n\nEnter Choice: ");
scanf("%d",&option);
if(option==1 || option==2)
{
if(option==1)
ascend(inp,n);
if(option==2)
descend(inp,n);
printarray2(inp,n);
getch();
option=0;
}
else
option=2;
}

/* Provides the option to search for the position of an element in the array */
if(option==3)
{
clrscr();
printf("Enter the value of the element you want to search: ");
scanf("%d",&temp1);
temp2=search(temp1,inp,n);
if(temp2==-1)
printf("\n\nValue not found.");
else
printf("\n\n%d was found at the following position: %d",temp1,temp2);
getch();
option=0;
}

/* Inserts an element at any user defined position of the array */
if(option==4)
{
clrscr();
printarray1(inp,n);
printf("\n\nEnter the position of the array where you want to insert a new element: ");
scanf("%d",&temp1);
printf("\nEnter the element: ");
scanf("%d",&temp2);
insertion(temp2,temp1,inp,n);
++n;
printarray2(inp,n);
getch();
option=0;
}

/* Removes an element from any given position of the array */
if(option==5)
{
clrscr();
printarray1(inp,n);
printf("\n\nEnter the position of the element that you want to delete: ");
scanf("%d",&temp1);
deletion(temp1,inp,n);
--n;
printarray2(inp,n);
getch();
option=0;
}

/* Merges a set of elements with the existing array */
if(option==6)
{
int inp2[100];
clrscr();
printarray1(inp,n);
printf("\n\nEnter the no elements you would like to merge with the existing array: ");
scanf("%d",&temp1);
printf("Enter the elements: ");
for(i=0;i<temp1;++i)
scanf("%d",&inp2[i]);
merge(n,inp,temp1,inp2);
n=n+temp1;
printarray2(inp,n);
getch();
option=0;
}

/* Reverses an element of the array */
if(option==7)
{
clrscr();
printarray1(inp,n);
printf("\n\nEnter the position of the element that you want to reverse: ");
scanf("%d",&temp1);
reverse(temp1,inp);
printarray2(inp,n);
getch();
option=0;
}

/* Exits the program */
if(option==8)
break;

/* Returns the control back to main menu if an invalid option is selected from the main menu */
else
option=0;
}
}
/* Main Program Ends */

/* Prints the structure of the array */
void printarray1(int a[],int n)
{
int i;
printf("\nThe structure of the array is given below:\n\n------------------\n%8s%10s\n------------------","Position","Element");
for(i=0;i<n;++i)
printf("\n%8d%10d",i,a[i]);
printf("\n------------------\n");
}

/* Prints the structure of the array after any modification */
void printarray2(int a[],int n)
{
int i;
printf("\nThe structure of the modified array is given below:\n\n------------------\n%8s%10s\n------------------","Position","Element");
for(i=0;i<n;++i)
printf("\n%8d%10d",i,a[i]);
printf("\n------------------\n\n<Press any key to continue>\n");
}

/* Returns the value of the maximum element of an array */
int maximum(int a[],int n)
{
int max,i;
max=a[0];
for(i=1;i<n;++i)
{
if(a[i]>max)
max=a[i];
}
return(max);
}

/* Returns the value of the minimum element of an array */
int minimum(int a[],int n)
{
int min,i;
min=a[0];
for(i=1;i<n;++i)
{
if(a[i]<min)
min=a[i];
}
return(min);
}

/* Sorts an array in ascending order */
void ascend(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;++i)
{
for(j=0;j<n-1-i;++j)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

/* Sorts an array in descending order */
void descend(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;++i)
{
for(j=0;j<n-1-i;++j)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

/* Searches for the position of any particular value from an array */
int search(int key,int a[],int n)
{
int i;
for(i=0;i<n;++i)
{
if(a[i]==key)
return(i);
}
return(-1);
}

/* Inserts an element at any given position of an existing array */
void insertion(int element,int pos,int a[],int n)
{
int i;
for(i=n;i>pos;--i)
a[i]=a[i-1];
a[pos]=element;
}

/* Deletes an element from any given position of an existing array */
void deletion(int pos,int a[],int n)
{
int i;
for(i=pos;i<n-1;++i)
a[i]=a[i+1];
}

/* Merges a set of elements with an existing array */
void merge(int n,int a[],int m,int b[])
{
int i,j;
for(i=n,j=0;i<m+n;++i,++j)
a[i]=b[j];
}

/* Reverse the value of an element at any given position of an array */
void reverse(int pos,int a[])
{
int i,sum=0;
while(a[pos]>0)
{
sum=sum*10+a[pos]%10;
a[pos]/=10;
}
a[pos]=sum;
}

Binary Search Using Recursion


/* Subhranath Chunder - Binary Search using Recursion */

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

void sort(int a[],int n);
int search(int t,int a[],int lb,int ub);
void main()
{
int n,inp[200],i,key,result;
clrscr();
printf("Enter the no. of terms: ");
scanf("%d",&n);
printf("Enter the values: ");
for(i=0;i<n;++i)
scanf("%d",&inp[i]);
printf("Enter the value to search: ");
scanf("%d",&key);
sort(inp,n);
printf("The sorted array is given below:\n");
for(i=0;i<n;++i)
printf("%d ",inp[i]);
result=search(key,inp,0,n-1);
if(result==-1)
printf("\nValue Not Found");
else
printf("\nThe value was found at position %d of the sorted array.",result);
getch();
}

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

int search(int key,int a[],int lb,int ub)
{
int middle;
if(lb<=ub)
{
middle=(lb+ub)/2;
if(a[middle]==key)
return(middle);
if(key<a[middle])
return search(key,a,lb,middle-1);
else
return search(key,a,middle+1,ub);
}
return(-1);
}

Binary Search


/* Subhranath Chunder - Binary Search */

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

void sort(int a[],int n);
int search(int t,int a[],int n);
void main()
{
int n,inp[200],i,key,result;
clrscr();
printf("Enter the no. of terms: ");
scanf("%d",&amp;n);
printf("Enter the values: ");
for(i=0;i<n;++i)
scanf("%d",&amp;inp[i]);
printf("Enter the value to search: ");
scanf("%d",&amp;key);
sort(inp,n);
printf("The sorted array is given below:\n");
for(i=0;i<n;++i)
printf("%d ",inp[i]);
result=search(key,inp,n);
if(result==-1)
printf("\nValue Not Found");
else
printf("\nThe value was found at position %d of the sorted array.",result);
getch();
}

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

int search(int key,int a[],int n)
{
int lb,ub,middle;
lb=0;
ub=n-1;
while(lb<=ub)
{
middle=(lb+ub)/2;
if(a[middle]==key)
return(middle);
if(key<a[middle])
ub=middle-1;
else
lb=middle+1;
}
return(-1);
}