• No results found

Cosom Lab File

N/A
N/A
Protected

Academic year: 2021

Share "Cosom Lab File"

Copied!
37
0
0

Loading.... (view fulltext now)

Full text

(1)

AMITY UNIVERSITY

Computer Oriented Statistical and

Optimization Methods

Submitted to:

Submitted by

:

Ms. upasana Sharma

Jasdeep Singh

BCA (III-A)

A1004814024

(2)

INDEX

S.No. Experiments Signature

1. Write a program to find mean, median and

mode.

2. Write a program to evaluate measure of

dispersion.

3. Write a program to find skewness.

4. Write a program to find Kurtosis.

5. Write a program to find regression line of y on x from any given set of points.

6. Write a program to find regression line of x on y from any given set of points.

7. Write a program to find the solution of linear equations using Simplex Method.

8. Write a program to find the solution of linear equations using Big M Method.

9. Write a program to find the solution of Transportation problem using North-West corner Method.

10. Write a program to find the solution of Transportation problem using Least Cost Method.

11. Write a program to find the solution of

(3)

Program 1. Write a program to find mean, median and mode.

Solution:-#include <iostream> using namespace std; void mode(int[ ], int); void mean(int[ ], int); void sort(int[ ], int); void median(int[ ], int); int main( )

{

int array[15]; float total, mode;

int n = 15;//number of elements in array //fill in the value of array

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

cout << "fill in the "<< i+1 << " number. :"; cin >> array[i];

}

sort(array, n); return 0; }

void mean(int new_array[], int num){ //GET TOTAL & CALCULATE MEAN float total;

for(int i=0;i<num; i++){ total += new_array[i]; }

cout << "The mean is " << total/num << endl; mode(new_array, num);

}

void median(int new_array[], int num){

//CALCULATE THE MEDIAN (middle number) if(num % 2 != 0){// is the # of elements odd? int temp = ((num+1)/2)-1;

cout << "The median is " << new_array[temp] << endl; }

else{// then it's even! :)

cout << "The median is "<< new_array[(num/2)-1] << " and " << new_array[num/2] << endl;

}

mean(new_array, num); }

void mode(int new_array[], int num) { int* ipRepetition = new int[num];

(4)

// alocate a new array in memory of the same size (round about way of defining number of elements by a variable) for (int i = 0; i < num; i++) {

ipRepetition[i] = 0;//initialize each element to 0 int j = 0;//

while ((j < i) && (new_array[i] != new_array[j])) { if (new_array[i] != new_array[j]) { j++; } } (ipRepetition[j])++; } int iMaxRepeat = 0;

for (int i = 1; i < num; i++) {

if (ipRepetition[i] > ipRepetition[iMaxRepeat]) { iMaxRepeat = i;

} }

cout<< "The mode is " << new_array[iMaxRepeat] << endl; }

void sort(int new_array[], int num){ //ARRANGE VALUES

for(int x=0; x<num; x++){ for(int y=0; y<num-1; y++){

if(new_array[y]>new_array[y+1]){ int temp = new_array[y+1]; new_array[y+1] = new_array[y]; new_array[y] = temp; } } } cout << "List: ";

for(int i =0; i<num; i++){ cout << new_array[i] << " "; }

cout << "\n";

median(new_array, num); }

(5)
(6)

Program 2. Write a program to evaluate measure of dispersion. Solution:- #include <stdio.h> #include <math.h> #define MAXSIZE 10 void main() { float x[MAXSIZE]; int i, n;

float average, variance, std_deviation, sum = 0, sum1 = 0; printf("Enter the value of N \n");

scanf("%d", &n);

printf("Enter %d real numbers \n", n); for (i = 0; i < n; i++)

{

scanf("%f", &x[i]); }

/* Compute the sum of all elements */ for (i = 0; i < n; i++)

{

sum = sum + x[i]; }

average = sum / (float)n;

/* Compute variance and standard deviation */ for (i = 0; i < n; i++)

{

sum1 = sum1 + pow((x[i] - average), 2); }

variance = sum1 / (float)n; std_deviation = sqrt(variance);

printf("Average of all elements = %.2f\n", average); printf("variance of all elements = %.2f\n", variance); printf("Standard deviation = %.2f\n", std_deviation); }

(7)
(8)

Program 3. Write a program to find skewness. Solution: #include <cstdlib> #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <math.h> using namespace std; int main() {

cout << "Enter number of elements." << endl;

int n, i, x; double avg, var = 0, skewness = 0, S, k = 0; cin >> n;

cout << "Enter elements." << endl; vector<int> v; int a[] = {2, 11, 8, 10, 1, 13, 9, 5, 9, 2, 10, 3, 8, 6, 17, 2, 10, 15, 14, 5}; for (i = 0; i < n; i++) { x = a[i]; v.push_back(x); } sort(v.begin(), v.end()); for (i = 0; i < n; i++) cout << v[i] << " ";

int sum = accumulate(v.begin(), v.end(), 0); avg = (double)sum/n;

cout << "\nMean = " << avg << endl; vector<int>::iterator it;

if (n % 2 != 0) {

x = (n+1)/2; it = v.begin() + x;

nth_element(v.begin(), it, v.end());

cout << "Median = " << *(v.begin() + x -1) << endl; } else { float m; x = (n)/2 - 3; it = v.begin() + x; m = (*(v.begin()+ x - 1) + (*(v.begin() + x - 2 )))/2; cout << "Median = " << m << endl;

(9)

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

var += (v[i] - avg)*(v[i] - avg); }

var = (double)(var)/(n - 1);

cout << "Variance = " << var << endl; S = (double)sqrt(var);

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

skewness += (v[i] - avg)*(v[i] - avg)*(v[i] - avg); skewness = skewness/(n * S * S * S);

cout << "Skewness = " << skewness << endl; system("PAUSE");

return EXIT_SUCCESS; }

(10)

Program 4. Write a program to find Kurtosis. Solution:-#include <cstdlib> #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <math.h> using namespace std; int main() {

cout << "Enter number of elements." << endl;

int n, i, x; double avg, var = 0, skewness = 0, S, k = 0; cin >> n;

cout << "Enter elements." << endl; vector<int> v; int a[] = {2, 11, 8, 10, 1, 13, 9, 5, 9, 2, 10, 3, 8, 6, 17, 2, 10, 15, 14, 5}; for (i = 0; i < n; i++) { x = a[i]; v.push_back(x); } sort(v.begin(), v.end()); for (i = 0; i < n; i++) cout << v[i] << " ";

int sum = accumulate(v.begin(), v.end(), 0); avg = (double)sum/n;

cout << "\nMean = " << avg << endl; vector<int>::iterator it;

if (n % 2 != 0) {

x = (n+1)/2; it = v.begin() + x;

nth_element(v.begin(), it, v.end());

cout << "Median = " << *(v.begin() + x -1) << endl; }

else {

float m; x = (n)/2 - 3;

(11)

it = v.begin() + x;

m = (*(v.begin()+ x - 1) + (*(v.begin() + x - 2 )))/2; cout << "Median = " << m << endl;

}

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

var += (v[i] - avg)*(v[i] - avg); }

var = (double)(var)/(n - 1);

cout << "Variance = " << var << endl; S = (double)sqrt(var);

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

skewness += (v[i] - avg)*(v[i] - avg)*(v[i] - avg); skewness = skewness/(n * S * S * S);

cout << "Skewness = " << skewness << endl; for (i = 0; i < n; i++)

k += (v[i] - avg)*(v[i] - avg)*(v[i] - avg)*(v[i] - avg); k = k/(n*S*S*S*S);

k -= 3;

cout << "Kurtosis = " << k << endl; system("PAUSE");

return EXIT_SUCCESS; }

(12)

Program 5. Write a program to find regression line of y on x from any given set of points.

Solution:

#include<iostream> Using namespace std; float mean(float *a, int n);

void deviation(float *a, float mean, int n, float *d, float *S); void main()

{

float a[20],b[20],dx[20],dy[20];

float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0; float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0; char type_coff[7];

int n=0,i=0; clrscr();

printf("Enter the value of n: "); scanf("%d",&n);

printf("Enter the values of x and y:\n"); for(i=0;i scanf("%f%f",&a[i],&b[i]); mean_x=mean(a,n); mean_y=mean(b,n); deviation(a,mean_x,n,dx,&sx); deviation(b,mean_y,n,dy,&sy); for(i=0;i sum_xy=sum_xy+dx[i]*dy[i]; corr_coff=sum_xy/(n*sx*sy);

printf("Enter the type of regression coefficient as 'x on y' or 'y on x': "); fflush(stdin); gets(type_coff); if(strcmp(type_coff,"x on y")==1) { reg_coff_xy=corr_coff*(sx/sy);

printf("\nThe value of linear regression coefficient is %f",reg_coff_xy);

}

else if(strcmp(type_coff,"y on x")==1) {

(13)

printf("\nThe value of linear regression coefficient is %f",reg_coff_yx);

} else

printf("\nEnter the correct type of regression coefficient."); getch();

}

float mean(float *a, int n) {

float sum=0, i=0;

for(i=0;i sum=sum+a[i]; sum=sum/n;

return (sum); }

void deviation(float *a, float mean, int n, float *d, float *s) { float sum=0,t=0; int i=0; for(i=0;i { d[i]=a[i]-mean; t=d[i]*d[i]; sum=sum+t; } sum=sum/n; *s=sqrt(sum); } OUTPUT:

(14)

Program 6. Write a program to find regression line of x on y from any given set of points.

Solution: #include <iostream> #include <iomanip> using namespace std; int main() { double x[7] = { 1.5, 2.4, 3.2, 4.8, 5.0, 7.0, 8.43 }; double y[7] = { 3.5, 5.3, 7.7, 6.2, 11.0, 9.5, 10.27 }; Maths::Regression::Linear A(7, x, y);

cout << " Slope = " << A.getSlope() << endl;

cout << "Intercept = " << A.getIntercept() << endl << endl; cout << "Regression coefficient = " << A.getCoefficient() << endl;

cout << endl << "Regression line values" << endl << endl; for (double i = 0.0; i <= 3; i += 0.6) { cout << "x = " << setw(3) << i << " y = " << A.getValue(i); cout << endl; } return 0; }

(15)

OUTPUT:

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- How many values You are Entering7 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- Enter coressponding Elements X & Y

1 0.5 2 2.5 3 2 4 4 5 3.5 6 6 7 5.8 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- Y=-0.371429+0.871429X

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-Program 7. Write a program to find the solution of linear equations using Simplex Method.

Solution: #include <stdio.h> #include <math.h> #define CMAX 10 #define VMAX 10 int NC, NV, NOPTIMAL,P1,P2,XERR; double TS[CMAX][VMAX]; void Data() { double R1,R2; char R; int I,J;

printf("\n SIMPLEX METHOD\n\n");

printf(" MAXIMIZE (Y/N) ? "); scanf("%c", &R);

printf("\n NUMBER OF VARIABLES OF THE FUNCTION ? "); scanf("%d", &NV);

printf("\n NUMBER OF CONSTRAINTS ? "); scanf("%d", &NC); if (R == 'Y' || R=='y')

R1 = 1.0; else

R1 = -1.0;

printf("\n INPUT COEFFICIENTS OF THE FUNCTION:\n"); for (J = 1; J<=NV; J++) {

(16)

TS[1][J+1] = R2 * R1; }

printf(" Right hand side ? "); scanf("%lf", &R2); TS[1][1] = R2 * R1;

for (I = 1; I<=NC; I++) {

printf("\n CONSTRAINT #%d:\n", I); for (J = 1; J<=NV; J++) {

printf(" #%d ? ", J); scanf("%lf", &R2); TS[I + 1][J + 1] = -R2;

}

printf(" Right hand side ? "); scanf("%lf", &TS[I+1][1]); }

printf("\n\n RESULTS:\n\n");

for(J=1; J<=NV; J++) TS[0][J+1] = J;

for(I=NV+1; I<=NV+NC; I++) TS[I-NV+1][0] = I; } void Pivot(); void Formula(); void Optimize(); void Simplex() { e10: Pivot(); Formula(); Optimize();

if (NOPTIMAL == 1) goto e10; } void Pivot() { double RAP,V,XMAX; int I,J; XMAX = 0.0; for(J=2; J<=NV+1; J++) { if (TS[1][J] > 0.0 && TS[1][J] > XMAX) { XMAX = TS[1][J]; P2 = J; } } RAP = 999999.0;

for (I=2; I<=NC+1; I++) { if (TS[I][P2] >= 0.0) goto e10; V = fabs(TS[I][1] / TS[I][P2]); if (V < RAP) { RAP = V; P1 = I; } e10:;} V = TS[0][P2]; TS[0][P2] = TS[P1][0]; TS[P1][0] = V; }

(17)

void Formula() {; int I,J;

for (I=1; I<=NC+1; I++) { if (I == P1) goto e70; for (J=1; J<=NV+1; J++) { if (J == P2) goto e60; TS[I][J] -= TS[P1][J] * TS[I][P2] / TS[P1][P2]; e60:;} e70:;} TS[P1][P2] = 1.0 / TS[P1][P2]; for (J=1; J<=NV+1; J++) { if (J == P2) goto e100; TS[P1][J] *= fabs(TS[P1][P2]); e100:;}

for (I=1; I<=NC+1; I++) { if (I == P1) goto e110; TS[I][P2] *= TS[P1][P2]; e110:;} } void Optimize() { int I,J;

for (I=2; I<=NC+1; I++) if (TS[I][1] < 0.0) XERR = 1; NOPTIMAL = 0; if (XERR == 1) return; for (J=2; J<=NV+1; J++) if (TS[1][J] > 0.0) NOPTIMAL = 1; } void Results() { int I,J;

if (XERR == 0) goto e30;

printf(" NO SOLUTION.\n"); goto e100; e30:for (I=1; I<=NV; I++)

for (J=2; J<=NC+1; J++) { if (TS[J][0] != 1.0*I) goto e70;

printf(" VARIABLE #%d: %f\n", I, TS[J][1]); e70: ;}

printf("\n ECONOMIC FUNCTION: %f\n", TS[1][1]); e100:printf("\n"); } void main() { Data(); Simplex(); Results(); }

(18)

OUTPUT:

MAXIMIZE (Y/N) ? y

NUMBER OF VARIABLES OF THE FUNCTION ? 2 NUMBER OF CONSTRAINTS ? 3

INPUT COEFFICIENTS OF THE FUNCTION: #1 ? 4

#2 ? 10

Right hand side ? 0

CONSTRAINT #1: #1 ? 2

#2 ? 1

Right hand side ? 50 CONSTRAINT #2:

#1 ? 2 #2 ? 5

Right hand side ? 100 CONSTRAINT #3:

#1 ? 2 #2 ? 3

Right hand side ? 90

RESULTS:

VARIABLE #2: 20.000000

ECONOMIC FUNCTION: 200.000000 8

(19)

Program 8. Write a program to find the solution of linear equations using Big M Method.

Solution: #include <stdio.h> int bk=1,g,l; void max(); void min(); int main() { int i;

printf("\nNOTE: SLACK VARIABLES HELP IN FORMING THE UNIT MAQTRIX. BUT

OBJECTIVE\n");

printf("FUNCTIONS WITH <= WILL PROVIDE SLACK VARIABLES BUT THOSE

WITH >= WILL NOT.\n");

printf("HENCE TO MAKE A UNIT MATRIX, WE MUST TAKE HELP OF ARTIFICIAL

VARIABLES. THIS\n");

printf("INCLUDE ANOTHER COEFFICIENT CALLED BIG-M. “);

printf("\n OBJECTIVE? Maximization(1) or Minimization(2) or any other to Exit...:\n"); scanf("%d",&i); if (i==1) max(); else if (i==2) min();

(20)

else return 0; } void min() { int i,j,row,col,kc,kr,sub,var,flag=-1,k; double sum,max,ba,kn; double cj[20],basis[20],c[20],colmat[20],tab[20][20]; bk=0;

printf("\n\nTAKE \"M\" AS A BIT LARGER THAN ANY LARGEST NUMBER IN THE

printf("\nTOTAL NUMBER OF >= EQUALITIES IN SUBJECTIVE FUNCTIONS:\n");

scanf("%d", &g);

printf("\nTOTAL NUMBER OF <= EQUALITIES IN SUBJECTIVE FUNCTIONS:\n");

scanf("%d", &l);

printf("\nTOTAL NUMBER OF \"ARTIFICIAL VARIABLES\": %d\n",g); printf("ENTER THE TOTAL NUMBER OF VARIABLES IN OBJECTIVE FUNCTION:\n");

scanf("%d", &var);

printf("\nSTART ENTERING THE COEFFICIENTS OF THE OBJECTIVE FUNCTION:\n");

for(i=0;i<var;i++) {

scanf("%lf", &cj[i]); }

printf("\nENTER TOTAL NUMBER OF SUBJECTIVE FUNCTIONS:\n"); scanf("%d", &sub);

//DECIDING TABLE DIMENSION col=var+4;

row=sub+2;

for(i=1;i<row-1;i++) {

printf("\nENTER ELEMENTS IN %d-th ROW:\n",i); for(j=4;j<col;j++) {scanf("%lf",&tab[i][j]); } }

printf("\nENTER THE BASIS:\n"); for(i=0;i<sub;i++)

scanf("%lf", &basis[i]);

printf("\nENTER THE VALUES OF \"C\" FROM OBJECTIVE FUNCTION\n");

for(i=0;i<sub;i++) scanf("%lf", &c[i]);

printf("\nENTER THE COLUMN MATRIX:\n"); for(i=0;i<sub;i++)

(21)

scanf("%lf", &colmat[i]); //INITIALIZING THE TABLE for(i=1;i<row-1;i++) tab[i][0]=i; for(i=1;i<row-1;i++) { tab[i][1]=basis[i-1]; tab[i][2]=c[i-1]; tab[i][3]=colmat[i-1]; }

printf("\nPRINTING THE MATRIX YOU HAVE INSERTED:\n"); printf("---\n"); for(i=1;i<row-1;i++) { for(j=0;j<col;j++) { printf("%.3lf\t",tab[i][j]); } printf("\n"); } printf("---\n"); for(i=4;i<col;i++) tab[row-1][i]=0; tab[row-1][3]=0;

//STARTING THE ITERATION for(k=0;k<10;k++) { //INITIALIAZING FLAG flag=-1.00; //Z0 for(i=1;i<row-1;i++) tab[row-1][3]=tab[row-1][3]+tab[i][3]*tab[i][2]; //Zj-Cj for(i=4;i<col;i++) { sum=0; for(j=1;j<row-1;j++) sum=tab[j][i]*tab[j][2]+sum; tab[row-1][i]=sum-cj[i-4]; } //FINDING MAXIMUM IN Zj-Cj max=tab[row-1][4]; kc=4; for(i=4;i<col;i++) { if(max<tab[row-1][i])

(22)

{

max=tab[row-1][i]; kc=i;

} }

//FINDING b/a RATIO for(j=1;j<row-1;j++) { if(tab[j][kc]>0) { ba=(colmat[j-1]/tab[j][kc]); goto pop; } } pop: kr=j; for(;j<row-1;j++) {

if((tab[j][kc]>0) && ((colmat[j-1]/tab[j][kc])<ba)) kr=j;

}

//SWAPPING KEY COLUMN WITH BASIS tab[kr][1]=kc-3;

kn=tab[kr][kc]; tab[kr][2]=cj[kc-4];

//DIVIDING OTHER ROWS BY THE FORMULA for(i=1;i<row-1;i++) { if(i==kr) continue; else { for(j=3;j<kc;j++) tab[i][j]=tab[i][j]-((tab[i][kc]*tab[kr][j])/kn); } } printf("\n\n"); for(i=1;i<row-1;i++) { if(i==kr) continue; else { for(j=kc+1;j<col;j++) tab[i][j]=tab[i][j]-((tab[i][kc]*tab[kr][j])/kn); }

(23)

} for(i=1;i<row-1;i++) { if(i==kr) continue; else tab[i][kc]=tab[i][kc]-((tab[i][kc]*tab[kr][kc])/kn); }

//DIVIDING KEY ROW BY KEY NUMBER for(i=3;i<col;i++)

tab[kr][i]=tab[kr][i]/kn;

//CHECKING IF Zj-Cj ARE ALL NEGATIVE for(i=4;i<col;i++) { if(tab[row-1][i]>0) { flag=1; } }

//BREAKING THE LOOP if(flag==-1)

goto sos; }

sos:

printf("\nTHE SOLUTION IS...\n"); for(i=1;i<row-1;i++) printf("X%d=%lf\n",(int)tab[i][1],tab[i][3]); } void max() { int i,j,row,col,kc,kr,sub,var,flag=-1,k; double sum,max,ba,kn; double cj[20],basis[20],c[20],colmat[20],tab[20][20]; bk=0;

printf("\n\nTAKE \"M\" AS A BIT SMALLER THAN ANY SMALLEST NUMBER IN

THE EQUATIONS...\n\n\n");

printf("\nTOTAL NUMBER OF >= EQUALITIES IN SUBJECTIVE FUNCTIONS:\n");

scanf("%d", &g);

printf("\nTOTAL NUMBER OF <= EQUALITIES IN SUBJECTIVE FUNCTIONS:\n");

scanf("%d", &l);

printf("\nTOTAL NUMBER OF \"ARTIFICIAL VARIABLES\": %d\n",g); printf("ENTER THE TOTAL NUMBER OF VARIABLES IN OBJECTIVE FUNCTION:\n");

(24)

scanf("%d", &var);

printf("\nSTART ENTERING THE COEFFICIENTS OF THE OBJECTIVE FUNCTION:\ n"); for(i=0;i<var;i++) { scanf("%lf", &cj[i]); }

printf("\nENTER TOTAL NUMBER OF SUBJECTIVE FUNCTIONS:\n"); scanf("%d", &sub);

//DECIDING TABLE DIMENSION col=var+4;

row=sub+2;

for(i=1;i<row-1;i++) {

printf("\nENTER ELEMENTS IN %d-th ROW:\n",i); for(j=4;j<col;j++)

{

scanf("%lf",&tab[i][j]); }

}

printf("\nENTER THE BASIS:\n"); for(i=0;i<sub;i++)

scanf("%lf", &basis[i]);

printf("\nENTER THE VALUES OF \"C\" FROM OBJECTIVE FUNCTION\n");

for(i=0;i<sub;i++) scanf("%lf", &c[i]);

printf("\nENTER THE COLUMN MATRIX:\n"); for(i=0;i<sub;i++)

scanf("%lf", &colmat[i]); //INITIALIZING THE TABLE for(i=1;i<row-1;i++) tab[i][0]=i; for(i=1;i<row-1;i++) { tab[i][1]=basis[i-1]; tab[i][2]=c[i-1]; tab[i][3]=colmat[i-1]; }

printf("\nPRINTING THE MATRIX YOU HAVE INSERTED:\n"); printf("---\n");

for(i=1;i<row-1;i++) {

for(j=0;j<col;j++) {

(25)

printf("%.3lf\t",tab[i][j]); } printf("\n"); } printf("---\n"); for(i=4;i<col;i++) tab[row-1][i]=0; tab[row-1][3]=0;

//STARTING THE ITERATION for(k=0;k<10;k++) { //INITIALIAZING FLAG flag=-1.00; //Z0 for(i=1;i<row-1;i++) tab[row-1][3]=tab[row-1][3]+tab[i][3]*tab[i][2]; //Zj-Cj for(i=4;i<col;i++) { sum=0; for(j=1;j<row-1;j++) sum=tab[j][i]*tab[j][2]+sum; tab[row-1][i]=sum-cj[i-4]; } //FINDING MAXIMUM IN Zj-Cj max=tab[row-1][4]; kc=4; for(i=4;i<col;i++) { if(max<tab[row-1][i]) { max=tab[row-1][i]; kc=i; } }

//FINDING b/a RATIO for(j=1;j<row-1;j++) { if(tab[j][kc]>0) { ba=(colmat[j-1]/tab[j][kc]); goto pop; } } pop: kr=j;

(26)

for(;j<row-1;j++) {

if((tab[j][kc]>0) && ((colmat[j-1]/tab[j][kc])<ba)) kr=j;

}

//SWAPPING KEY COLUMN WITH BASIS tab[kr][1]=kc-3;

kn=tab[kr][kc]; tab[kr][2]=cj[kc-4];

//DIVIDING OTHER ROWS BY THE FORMULA for(i=1;i<row-1;i++) { if(i==kr) continue; else { for(j=3;j<kc;j++) tab[i][j]=tab[i][j]-((tab[i][kc]*tab[kr][j])/kn); } } printf("\n\n"); for(i=1;i<row-1;i++) { if(i==kr) continue; else { for(j=kc+1;j<col;j++) tab[i][j]=tab[i][j]-((tab[i][kc]*tab[kr][j])/kn); } } for(i=1;i<row-1;i++) { if(i==kr) continue; else tab[i][kc]=tab[i][kc]-((tab[i][kc]*tab[kr][kc])/kn); }

//DIVIDING KEY ROW BY KEY NUMBER for(i=3;i<col;i++)

tab[kr][i]=tab[kr][i]/kn;

//CHECKING IF Zj-Cj ARE ALL POSITIVE for(i=4;i<col;i++)

{

if(tab[row-1][i]<0) flag=1;

(27)

}

//BREAKING THE LOOP if(flag==-1)

goto sos; }

sos:

printf("\nTHE SOLUTION IS...\n"); for(i=1;i<row-1;i++)

printf("X%d=%lf\n",(int)tab[i][1],tab[i][3]); }

Program 9. Write a program to find the solution of

Transportation problem using North-West corner Method.

Solution: #include<stdio.h> #include<iostream.h> void main() { int a[10][10],m,n,sum=0,f=0,e=0,d,c; cout<<"enter the no. of companies"; cin>>n;

cout<<"enter the no. of warehouses"; cin>>m;

for(f=1;f<=m;f++) {

for(e=1;e<=n;e++) {

cout<<"enter values of warehouse"<<m; cout<<"and company"<<n;

}

for(f=1;f<=m;f++) {

(28)

cout<<"enter value of supply"; cin>>c[f];

}

for(e=1;e<=n;e++) {

cout<<"enter value of demand"; cin>>d[e]; } while(c[f]<d[e]) { if(d[f]<c[e]) { c[e]=c[e]-d[f]; sum=sum+(c[e][f]*[f]) f=f+1; } else { if(d[f]>c[e]) { d[f]=d[f]-c[e]; sum=sum+(c[e][f]*c[e]) e=e+1; } else { if(d[f]==c[e]) { d[f]=d[f]-c[e]; sum=sum+(c[e][f]*c[e]) e=e+1; f=f+1; } } } }

cout<<"the optimal cost"; cin>>sum;

} }

(29)

Program 10. Write a program to find the solution of Transportation problem using Least Cost Method.

Solution: #include<conio.h> #include<stdio.h> void main() { int c[20] [20],i,j,min,m,n,b,d,c2,c1,p,q,dem[20],sup[20],rf[20],cf[20],sum= 0; clrscr();

printf("\nEnter the row & column:"); scanf("%d%d",&m,&n);

printf("\nEnter the cost:"); for(i=0;i<m;i++)

{ for(j=0;j<n;j++) scanf("%d",&c[i][j]); }

printf("\nEnter the demand:"); for(i=0;i<n;i++)

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

printf("\nEnter the supply:"); for(i=0;i<m;i++)

scanf("%d",&sup[i]); printf("\nMatrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++)

(30)

printf(" %d ",c[i][j]); printf("%d",sup[i]); printf("\n"); } for(j=0;j<n;j++) printf("%d ",dem[j]); for(i=0;i<m;i++) rf[i]=0; for(i=0;i<n;i++) cf[i]=0; b=m;d=n; while(b>0&&d>0) { min=1000; for(i=0;i<m;i++) { if(rf[i]!=1) { for(j=0;j<n;j++) { if(cf[j]!=1) { if(min>c[i][j]) { min=c[i][j]; p=i; q=j; } } } } } if(sup[p]<dem[q]) c1=sup[p]; else c1=dem[q]; for(i=0;i<m;i++) { if(rf[i]!=1) { for(j=0;j<n;j++) { if(cf[j]!=1) { if(min==c[i][j]) { if(sup[i]<dem[j]) c2=sup[i]; else c2=dem[j]; if(c2>c1) { c1=c2; p=i; q=j; } } }

(31)

} } } printf("\n %d %d %d ",min,p,q); if(sup[p]<dem[q]) { sum=sum+c[p][q]*sup[p]; dem[q]-=sup[p]; rf[p]=1; b--; } else if(sup[p]>dem[q]) { sum=sum+c[p][q]*dem[q]; sup[p]-=dem[q]; cf[q]=1; d--; } else if(sup[p]==dem[q]) { sum=sum+c[p][q]*sup[p]; rf[p]=1; cf[q]=1; b--; d--; } printf("\n %d",sum); } printf("\n\n %d",sum); getch(); } OUTPUT:

(32)

Program 11. Write a program to find the solution of Transportation problem using Vogel’s Method.

Solution:

#include<stdio.h> #include<conio.h> void sort(int a[],int n) { int temp,j,k;

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

{ for(k=j+1;k<n;k++) { if(a[j]>a[k])

(33)

a[j]=a[k]; a[k]=temp; } }}} void main() { int i,j,b,p,d,k,m,n,c[20][20],cf[20],rf[20],a[20],cp[20],rp[20]; int sup[20],dem[20],max,min,s,t,sum=0; clrscr();

printf("\nEnter the row & column:"); scanf("%d%d",&m,&n);

printf("\nEnter the cost:"); for(i=0;i<m;i++)

{ for(j=0;j<n;j++) scanf("%d",&c[i][j]); }

printf("\nEnter the demand:"); for(i=0;i<n;i++)

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

printf("\nEnter the supply:"); for(i=0;i<m;i++) scanf("%d",&sup[i]); printf("\nMatrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(" %d ",c[i][j]); printf("%d",sup[i]); printf("\n"); } for(j=0;j<n;j++) printf("%d ",dem[j]); for(i=0;i<m;i++) rf[i]=0; for(i=0;i<n;i++) cf[i]=0; b=m,d=n; while(b>0&&d>0) { for(i=0;i<m;i++) rp[i]=-1; for(i=0;i<n;i++) cp[i]=-1; for(i=0;i<m;i++) { k=0; if(rf[i]!=1) { for(j=0;j<n;j++) { if(cf[j]!=1) a[k++]=c[i][j]; }

(34)

if(k==1) rp[i]=a[0]; else { sort(a,k); rp[i]=a[1]-a[0]; }} } for(i=0;i<n;i++) { k=0; if(cf[i]!=1) { for(j=0;j<m;j++) { if(rf[j]!=1) a[k++]=c[j][i]; } if(k==1) cp[i]=a[0]; else { sort(a,k); cp[i]=a[1]-a[0]; }} for(i=0;i<m;i++) a[i]=rp[i]; for(j=0;j<n;j++) a[i+j]=cp[j]; max=a[0]; p=0; for(i=1;i<m+n;i++) { if(max<a[i]) { max=a[i]; p=i; }} printf("\n\n %d %d",max,p); min=1000; if(p>m-1) { p=p-m; if(cf[p]!=1) { for(i=0;i<m;i++) { if(rf[i]!=1) { if(min>c[i][p]) { min=c[i][p]; s=i; t=p; }}}} } else { if(rf[p]!=1) { for(i=0;i<n;i++) { if(cf[i]!=1)

(35)

{ if(min>c[p][i]) { min=c[p][i]; s=p; t=i; }}}} } printf("\n\n %d %d %d",min,s,t); if(sup[s]<dem[t]) { sum+=c[s][t]*sup[s]; dem[t]-=sup[s]; rf[s]=1; b--; } else if(sup[s]>dem[t]) { sum+=c[s][t]*dem[t]; sup[s]-=dem[t]; cf[t]=1; d--; } else if(sup[s]==dem[t]) { sum+=c[s][t]*dem[t]; cf[t]=1; rf[s]=1; b--; d--; } } printf("\n\n %d ",sum); getch(); } OUTPUT:

Output: Output of Vogel Approximation Method

5 3 6 2 19 (1) 4 7 9 1 37 (3) 3 4 7 5 34 (1) 16 18 31 25

(36)

(1) (1) (1) (1) 5 3 6 1111 19 (2) 4 7 9 1111 12 (3) 3 4 7 1111 34 (1) 16 18 31 0 (1) (1) (1) (0) 5 3 6 1111 19 (2) 1111 1111 1111 1111 0 (0) 3 4 7 1111 34 (1) 4 18 31 0 (2) (1) (1) (0) ---5 1111 6 1111 1 (1) 1111 1111 1111 1111 0 (0) 3 1111 7 1111 34 (4) 4 0 31 0 (2) (0) (1) (0) ---1111 ---1111 6 ---1111 1 (1105) 1111 1111 1111 1111 0 (0) 1111 1111 7 1111 30 (1104) 0 0 31 0 (0) (0) (1) (0) ---1111 ---1111 ---1111 ---1111 0 (0) 1111 1111 1111 1111 0 (0) 1111 1111 7 1111 30 (1104) 0 0 30 0 (0) (0) (1104) (0) ---1111 ---1111 ---1111 ---1111 0 (0) 1111 1111 1111 1111 0 (0) 1111 1111 1111 1111 0 (0) 0 0 0 0

(37)

(0) (0) (0) (0) ---5 3|18 6|1 2 4|12 7 9 1|25 3|4 4 7|30 5 1111 1111 7 1111 30 (1104) X[1][2] = 18 X[1][3] = 1 X[2][1] = 12 X[2][4] = 25 X[3][1] = 4 X[3][3] = 30

References

Related documents

The main hypothesis of this thesis is that the failure of USAID civil society assistance to achieve Democratization, using the NGO Service Center Program as a

Thus, it is potentially worth shedding light on the contribution of each of these variables to students’ L2 achievement; considering the fact that teachers’

The aim of the investigation presented in this paper is to evaluate the antibacterial and antioxidant activities of various extracts of Piper nigrum on several

SRT_zero – simple response time at zero distance; SRT_shoulder – simple response time at shoulder distance; CRT_zero – choice response time at zero distance; CRT_shoulder

The results of the study show how the influence of attitude dimensions with the original TAM theory where there are variable perceptions of usability and perceived

Additionally, the data suggest that the levels of client advocacy do not differ between CPAs and non-CPAs as shown in prior literature, that the clients of National Tax

The result shows that heroism is highlighted through the major characters and the conclusive points are some of the significant characters such as Mowgli, Father Wolf, Mother

Motivated by the above discussion, the aim of the present paper is to discuss the eects of nanouid on peristaltic transport of a fourth grade uid in an asymmetric channel with