answersLogoWhite

0

/*

PROGRAM BY SOUMEN KARMAKAR

College of Engineering and Management ,Kolaghat

3rd Year Information Technology

Note: Compile the program under Linux environment using gcc. If you want to compile it under turbo c then modify the program

*/

// Program to solve matrix Minima (Transportation Problem)

#include <stdio.h>

void input_data(int a[][10],int b[][10],int s[10],int d[10]);

void show_data(int a[][10],int b[][10],int s[10],int d[10]);

void matrix_min(int a[][10],int b[][10],int s[10],int d[10]);

int m,n;

int main(void)

{

int a[10][10],b[10][10],s[10],d[10];

printf("\nEnter the no of supply & demand\n");

scanf("%d %d",&m,&n);

input_data(a,b,s,d);

show_data(a,b,s,d);

matrix_min(a,b,s,d);

return (0);

}

void input_data(int a[][10],int b[][10],int s[10],int d[10])

{

int i,j;

printf("\nEnter the cost matrix\n");

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

}

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

b[i][j]=-1;

}

printf("\nEnter the supply \n");

for(i=0;i<m;i++)

scanf("%d",&s[i]);

printf("\nEnter the demand\n");

for(i=0;i<n;i++)

scanf("%d",&d[i]);

}

void show_data(int a[][10],int b[][10],int s[10],int d[10])

{

int i,j;

printf("\n \t\t\tSupply\n");

for(i=0;i<m;i++)

{

printf("\n");

for(j=0;j<n;j++)

{

printf("%d (%d) ",a[i][j],b[i][j]);

}

printf(" | %d",s[i]);

}

printf("\n------------------------\n");

printf("\nDemand ");

printf("%d ",d[i]);

}

void matrix_min(int a[][10],int b[][10],int s[10],int d[10])

{

int i,j,s1,d1,cost,min,t1,t2,sd;

int tag[10][10];

s1=0;

d1=0;

for(i=0;i<m;i++)

{

s1=s1+s[i];

}

printf("\nsupply= %d",s1);

for(j=0;j<n;j++)

{

d1=d1+d[j];

}

printf("\ndemand= %d",d1);

if(s1!=d1)

printf("\nIt is unbalanced\nSo new table is\n\n");

if(s1>d1)

{

n++;

for(i=0;i<m;i++)

{

a[i][n-1]=0;

b[i][n-1]=-1;

}

d[n-1]=s1-d1;

d1=s1;

show_data(a,b,s,d);

}

else if(s1<d1)

{

m++;

for(j=0;j<n;j++)

{

a[m-1][j]=0;

b[m-1][j]=-1;

}

s[m-1]=d1-s1;

s1=d1;

show_data(a,b,s,d);

}

//MAIN CALCULATION

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

tag[i][j]=-1;

}

}

printf("\nTag matrix \n");

for(i=0;i<m;i++)

{

printf("\n");

for(j=0;j<n;j++)

{

printf("%d ",tag[i][j]);

}

}

cost=0;

sd=0;

while(sd!=s1)

{

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

if(tag[i][j]==-1)

{

min=a[i][j];

t1=i;

t2=j;

break;

}

}

}

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

if(tag[i][j]==-1)

{

if(min>a[i][j])

{

min=a[i][j];

t1=i;

t2=j;

}

}

}

}

printf("\nMin =%d\n",min);

if(d[t2]==s[t1])

{

cost=cost+a[t1][t2]*s[t1];

b[t1][t2]=s[t1];

sd=sd+s[t1];

d[t2]=0;

s[t1]=0;

show_data(a,b,s,d);

for(j=0;j<n;j++)

{

if(tag[t1][j]==0)

continue;

tag[t1][j]=0;

}

for(i=0;i<m;i++)

{

if(tag[i][t2]==0)

continue;

tag[i][t2]=0;

}

printf("\nTag matrix \n");

for(i=0;i<m;i++)

{

printf("\n");

for(j=0;j<n;j++)

{

printf("%d ",tag[i][j]);

}

}

}

else if(d[t2]>s[t1])

{

cost=cost+a[t1][t2]*s[t1];

b[t1][t2]=s[t1];

sd=sd+s[t1];

d[t2]=d[t2]-s[t1];

s[t1]=0;

show_data(a,b,s,d);

for(j=0;j<n;j++)

{

if(tag[t1][j]==0)

continue;

tag[t1][j]=0;

}

printf("\nTag matrix \n");

for(i=0;i<m;i++)

{

printf("\n");

for(j=0;j<n;j++)

{

printf("%d ",tag[i][j]);

}

}

}

else

{

cost=cost+a[t1][t2]*d[t2];

b[t1][t2]=d[t2];

sd=sd+d[t2];

s[t1]=s[t1]-d[t2];

d[t2]=0;

show_data(a,b,s,d);

for(i=0;i<m;i++)

{

if(tag[i][t2]==0)

continue;

tag[i][t2]=0;

}

printf("\nTag matrix \n");

for(i=0;i<m;i++)

{

printf("\n");

for(j=0;j<n;j++)

{

printf("%d ",tag[i][j]);

}

}

}

}

printf("\nCost is =%d\n",cost);

}

// Thank You.........

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

How do you Write A program in c language for checking a diagonal matrix?

Write a program in c++ that take input in a integer matrix of size 4*4 and find out if the entered matrix is diagonal or not.


Write a c program to determine whether a matrix is singular or not?

A c program is also known as a computer program. A singular matrix has no inverse. An equation to determine this would be a/c=f. &lt;&lt;&gt;&gt; The determinant of a singular matix is zero.


What is matrix programming in C programming?

C Examples on Matrix OperationsA matrix is a rectangular array of numbers or symbols arranged in rows and columns. The following section contains a list of C programs which perform the operations of Addition, Subtraction and Multiplication on the 2 matrices. The section also deals with evaluating the transpose of a given matrix. The transpose of a matrix is the interchange of rows and columns.The section also has programs on finding the trace of 2 matrices, calculating the sum and difference of two matrices. It also has a C program which is used to perform multiplication of a matrix using recursion.C Program to Calculate the Addition or Subtraction & Trace of 2 MatricesC Program to Find the Transpose of a given MatrixC Program to Compute the Product of Two MatricesC Program to Calculate the Sum & Difference of the MatricesC Program to Perform Matrix Multiplication using Recursion


What is the product of matrix A and C?

It's matrix C.


Write a program to display Identity matrix?

#include &lt;stdio.h&gt; void main() { int a,b,c,i,j; printf("Enter the number of rows for square matrix : "); scanf("%d",&amp;a); for(i=1;i&lt;=a;i++) { for(c=1,j=1;j&lt;=a;j++,c++) { if(c==i) printf("1 "); else printf("0 "); } printf("\n"); } getch(); }

Related Questions

How do you Write A program in c language for checking a diagonal matrix?

Write a program in c++ that take input in a integer matrix of size 4*4 and find out if the entered matrix is diagonal or not.


Write a c program to determine whether a matrix is singular or not?

A c program is also known as a computer program. A singular matrix has no inverse. An equation to determine this would be a/c=f. &lt;&lt;&gt;&gt; The determinant of a singular matix is zero.


C Matrix addition program?

How we can addition Two Matrix plz send coding in C language mahesh dhanotiya astah_mahesh@rediff.com how i can built a square matrix in c,


A c program to square matrix?

A C program to square matrix is a math problem. In the math problem you write down all the outer boundary values of matrix in a circle, then write down the inner value.


Program to display multiplication of two matrix?

The matrix multiplication in c language : c program is used to multiply matrices with two dimensional array. This program multiplies two matrices which will be entered by the user.


A c program to find maximum number in a 3 by 3 matrix?

int matrix[][]; // the matrix to find the max in int max = matrix[0][0]; int r,c; for(r = 0; r &lt; 3; ++r) { for(c = 0; c &lt; 3; ++c) { if(matrix[r][c] &gt; max) { max = matrix[r][c]; } } } // max is now the maximum number in matrix


Write a c program for matrix addition using function?

#include&lt;


Write a C program to find sum of 3 matrices?

matrix


Write a C program using dynamic memory allocation to find the sum of elements of a matrix?

Did you know that memory allocation is not needed to display the matrix? However, the C program is to find the sum of all the elements.


How do you write a C program to find the adjoint of a matrix?

To write a C program to find the adjoint of a matrix, first, you need to create a function to calculate the cofactor of each element in the matrix. Then, construct the adjoint by transposing the cofactor matrix. The program should read the matrix size and elements from user input, compute the cofactors using nested loops, and finally display the adjoint matrix by transposing the cofactor matrix. Make sure to handle memory allocation for dynamic matrices if needed.


Write a c program to find eigenvalue of a matrix?

Yes, do write. That's what you always have to do when you have got a homework-program.


What is matrix programming in C programming?

C Examples on Matrix OperationsA matrix is a rectangular array of numbers or symbols arranged in rows and columns. The following section contains a list of C programs which perform the operations of Addition, Subtraction and Multiplication on the 2 matrices. The section also deals with evaluating the transpose of a given matrix. The transpose of a matrix is the interchange of rows and columns.The section also has programs on finding the trace of 2 matrices, calculating the sum and difference of two matrices. It also has a C program which is used to perform multiplication of a matrix using recursion.C Program to Calculate the Addition or Subtraction & Trace of 2 MatricesC Program to Find the Transpose of a given MatrixC Program to Compute the Product of Two MatricesC Program to Calculate the Sum & Difference of the MatricesC Program to Perform Matrix Multiplication using Recursion