0% found this document useful (0 votes)
40 views3 pages

Northwest

This document contains C++ code to implement the North West Corner Rule algorithm to solve a transportation problem. It defines classes and functions to get input data, validate that the problem is balanced, calculate the optimal solution by assigning quantities using NWC rule, and output the results including the minimum transportation cost. The code takes in supply quantities, demand quantities, and cost coefficients as input, calculates the optimal allocation of supplies to demands, and displays the solution matrix and total cost.

Uploaded by

virusyadav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Northwest

This document contains C++ code to implement the North West Corner Rule algorithm to solve a transportation problem. It defines classes and functions to get input data, validate that the problem is balanced, calculate the optimal solution by assigning quantities using NWC rule, and output the results including the minimum transportation cost. The code takes in supply quantities, demand quantities, and cost coefficients as input, calculates the optimal allocation of supplies to demands, and displays the solution matrix and total cost.

Uploaded by

virusyadav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

// TO CLACULATE THE OPTIMAL SOLUTION USING NORTH WEST CORNER RULE. //Roll no ---- 114 #include<iostream.h> #include<conio.h> #include<stdlib.

h> class North_West { int a,b,c,cost; int supply[5],demand[8],sup[5],dem[8]; int var[10][10],temp[10][10]; public: void getdata(); void valid(); void calculate(); void cost1(); }; void North_West::getdata() { cout<<"Enter the no. of the supply items"; cin>>a; cout<<"\nEnter the values for supply"; for(int i=0;i<a;i++) { cin>>supply[i]; sup[i]=supply[i]; } cout<<"\nEnter the no of demand items"; cin>>b; cout<<"\nEnter the values of demand"; for( i=0;i<b;i++) { cin>>demand[i]; dem[i]=demand[i]; } cout<<"\nEnter the coefficients"; for(i=0;i<a;i++) for(int j=0;j<b;j++) cin>>var[i][j]; } void North_West::valid() // to check valid prob. or not { int sum,sum1; sum=0;sum1=0; for(int i=0;i<a;i++) sum=sum+supply[i]; for(i=0;i<b;i++) sum1=sum1+demand[i]; if(sum==sum1) { cout<<"\nGiven problem is balanced"; cout<<"\nPlease press enter"; getch();

} else { cout<<"\nThe given problem is not balanced"; cout<<"\n Please try again"; getch(); exit(0); } } void North_West::calculate() { int r,c; r=0;c=0; cost=0; while (r<a) { while (c<b) { if(supply[r]<demand[c]) { temp[r][c]=supply[r]; cost=cost+(temp[r][c]*var[r][c]); demand[c]=demand[c]-supply[r]; r++; } else if(supply[r]>demand[c]) { temp[r][c]=demand[c]; cost=cost+(temp[r][c]*var[r][c]); supply[r]=supply[r]-demand[c]; c++; continue; } else { temp[r][c]=demand[c]; cost=cost+(temp[r][c]*var[r][c]); supply[r]=0; demand[c]=0; r++;c++; } } } } void North_West::cost1() { clrscr(); int r,c; r=8;c=5; for(int i=0;i<a;i++) { r=8; for(int j=0;j<b;j++) { gotoxy(r,c); cout<<var[i][j]; r=r+3;

} c=c+2; } r=19;c=4; gotoxy(r,c); cout<<"Supply"; r=21;c=5; for(i=0;i<a;i++) { gotoxy(r,c); cout<<sup[i]; c=c+2; } r=1;c=11; gotoxy(r,c); cout<<"Demand"; r=8;c=11; for(i=0;i<b;i++) { gotoxy(r,c); cout<<dem[i]; r=r+3; } cout<<"\nTotal Transportation Cost="<<cost; getch(); } void main() { clrscr(); North_West obj; obj.getdata(); obj.valid(); obj.calculate(); obj.cost1(); } -:OUTPUT:Enter the no. of the supply items3 Enter the values for supply 6 1 10 Enter the no of demand items 4 Enter the values of demand 7 5 3 2 Enter the coefficients 2 3 11 7 1061 5 8 15 9 Given problem is balanced 2 1 5 7 3 0 8 5 Supply 11 7 6 6 1 1 15 9 10 3 2

Please press enter

Demand

Total Transportation Cost=116

You might also like