Skip to content

Instantly share code, notes, and snippets.

@tomrockdsouza
Created June 2, 2017 11:07
Show Gist options
  • Save tomrockdsouza/9396ba50969c9923aa399b5cb2ebbd2a to your computer and use it in GitHub Desktop.
Save tomrockdsouza/9396ba50969c9923aa399b5cb2ebbd2a to your computer and use it in GitHub Desktop.
C Implementation for Matrix Method using Transportation Problem SCM
/**
* @main-author Tomrock D'souza, St. Francis Institute Of Technology, University of Mumbai, 2017
* Email: [email protected]
* No reproduction in whole or part without maintaining this notice
* and imposing this condition on any subsequent users.
* Example: https://postimg.org/image/y55004t9z/
*/
#include<stdio.h>
#include<conio.h>
/*
* storMat is te matrix that stores values of the numerical.
* numFact is the Max Factories or Importers
* numSupp is the Max Exporters
*/
int storMat[30][30], i, j, numSupp, numFact;
/*
* checker function checks if either
* all importer Demands are satisfied or
* all Exporters have no supply after each iterraion
*/
int checker() {
int a=0,b=0;
for (i = 0; i <= numSupp; i++) {
if (storMat[i][numFact] != 0) {
a = 1;
break;
}
}
for (i = 0; i <= numFact; i++) {
if (storMat[numSupp][i] != 0) {
b = 1;
break;
}
}
return a+b;
}
/*
* printr function prints the Numerical's current status
*/
void printr() {
for (i = 0; i < numSupp; i++) {
for (j = 0; j < numFact; j++) {
printf("%d\t", storMat[i][j]);
}
printf("| %d", storMat[i][j]);
printf("\n");
}
for(j=0;j<=8*numFact;j++){printf("-");}
printf("\n");
for (j = 0; j < numFact; j++) {
printf("%d\t", storMat[numSupp][j]);
}
printf("\n");
}
/*
* Checks the next avialable minimum in the list and performs and iteration returning it's amount
*/
long checkMin() {
int a=9999, b,k1,k2;
long g;
for (i = 0; i < numSupp; i++) {
for (j = 0; j < numFact; j++) {
if ((storMat[i][j] < a) &&(storMat[numSupp][j] > 0)&&(storMat[i][numFact] > 0)) {
k1 = i;
k2 = j;
a = storMat[k1][k2];
}
}
}
/*
* At this stage the least cost Exporter is found for this particular iteration
* Further the deduction operations will be performed
*/
if (storMat[k1][numFact] < storMat[numSupp][k2]) {
storMat[numSupp][k2] -= storMat[k1][numFact];
b = storMat[k1][numFact];
storMat[k1][numFact] = 0;
} else if (storMat[k1][numFact] > storMat[numSupp][k2]) {
storMat[k1][numFact] -= storMat[numSupp][k2];
b = storMat[numSupp][k2];
storMat[numSupp][k2] = 0;;
} else {
b = storMat[k1][numFact];
storMat[k1][numFact] = 0;
storMat[numSupp][k2] = 0;
}
printf("\nCost By Factory%d and Exporter%d = %d * %d = %d\n", k1, k2, b, a, g = b * a);
return g;
}
int main() {
long p = 0, q;
/*
* Take Inputs of Max Importers and Exporters aka (Factories and Exporters)
* User must enter all values in postive integer format only.
*/
printf("Enter number of Factories: ");
scanf("%d", & numFact);
printf("Enter number of Exporters: ");
scanf("%d", & numSupp);
for (i = 0; i < numSupp; i++) {
for (j = 0; j < numFact; j++) {
printf("Cost for Factory%d and Exporter%d = ", j,i);
scanf("%d", & storMat[i][j]);
}
printf("Stock with Exporter%d = ", i);
scanf("%d", & storMat[i][j]);
}
for (j = 0; j < numFact; j++) {
printf("Needed by Factory%d =", j);
scanf("%d", & storMat[numSupp][j]);
}
printf("\n");
printr();
/*
* At this step all values are taken as input and output is displayed
* This while loop performers Iteration on min Function and also checker function as it's condition.
*/
while (checker() > 1) {
q = checkMin();
printf("p= %ld + %ld = ", p, q);
printf("%ld\n", p += q);
printr();
}
printf("\nFinal Estimated Cost: %ld", p);
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment