thespeedracer38

North West Corner Method by Me

Mar 28th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class NWCM{
  7.     int cost[10][10], capacity[10], requirement[10], allotment[10][10];
  8.     int cap_size, req_size;
  9.     public:
  10.         NWCM(int cap_size, int req_size);
  11.         void input();
  12.         void displayAllotment(bool show_cap_req = true);
  13.         void displayCost();
  14.         void computeTransportationCost();
  15.         bool requirementFulfilled();
  16.         int min(int num1, int num2);
  17. };
  18.  
  19. int NWCM::min(int num1, int num2){
  20.     return num1 < num2 ? num1 : num2;
  21. }
  22.  
  23. NWCM::NWCM(int cap_size, int req_size){
  24.     NWCM::cap_size = cap_size;
  25.     NWCM::req_size = req_size;
  26.     // Initializing cost, allotment, requirement, capacity matrix
  27.     for(int i = 1; i <= cap_size; i++){
  28.         capacity[i] = 0;
  29.         requirement[i] = 0;
  30.         for(int j = 1; j <= req_size; j++){
  31.             cost[i][j] = 0;
  32.             allotment[i][j] = 0;
  33.         }
  34.     }  
  35. }
  36.  
  37. void NWCM::input(){
  38.     cout << "Enter the elements of cost matrix, capacity, requirement-->\n" << endl;
  39.     // Taking input in cost matrix
  40.     for(int i = 1; i <= cap_size; i++){
  41.         for(int j = 1; j <= req_size; j++){
  42.             cout << "cost[" << i << "][" << j << "] = ";
  43.             cin >> cost[i][j];
  44.         }
  45.     }
  46.     // Taking input in capacity matrix
  47.     for(int i = 1; i <= cap_size; i++){
  48.         cout << "capacity[" << i << "] = ";
  49.         cin >> capacity[i];
  50.     }
  51.     // Taking input in requirement matrix
  52.     for(int i = 1; i <= req_size; i++){
  53.         cout << "requirement[" << i << "] = ";
  54.         cin >> requirement[i];
  55.     }
  56. }
  57.  
  58. void NWCM::displayAllotment(bool show_cap_req){
  59.     for(int i = 1; i <= cap_size; i++){
  60.         for(int j = 1; j <= req_size; j++){
  61.             cout << allotment[i][j] << "\t";
  62.         }
  63.         if(show_cap_req){
  64.             cout << capacity[i];
  65.         }
  66.         cout << endl;  
  67.     }
  68.     if(show_cap_req)
  69.     {
  70.         // Requirement
  71.         for(int i = 1; i <= req_size; i++){
  72.             cout << requirement[i] << "\t";
  73.         }
  74.     }
  75.     cout << endl;
  76. }
  77.  
  78. void NWCM::displayCost(){
  79.     for(int i = 1; i <= cap_size; i++){
  80.         for(int j = 1; j <= req_size; j++){
  81.             cout << cost[i][j] << "\t";
  82.         }
  83.         cout << endl;
  84.     }
  85. }
  86.  
  87. void NWCM::computeTransportationCost(){
  88.     int row = 1, col = 1;
  89.     int iteration = 0;
  90.     // We continue perfoming allotments till all the capacity
  91.     // and requirement
  92.     while(!requirementFulfilled()){
  93.         cout << "requirementFulfilled() = " << requirementFulfilled() << endl;
  94.         cout << "Allotment Matrix after iteration-" << iteration << "--->" << endl;
  95.         displayAllotment();
  96.         cin.get();
  97.         cout << "\n\n\n";
  98.         int min_allotment = min(capacity[row], requirement[col]);
  99.         allotment[row][col] = min_allotment;
  100.         capacity[row] = capacity[row] - min_allotment;
  101.         requirement[col] = requirement[col] - min_allotment;
  102.         if(capacity[row] == 0){
  103.             row = row + 1;
  104.         }
  105.         if(requirement[col] == 0){
  106.             col = col + 1;
  107.         }
  108.         iteration = iteration + 1;
  109.     }
  110.     int total_cost = 0;
  111.     for(int i = 1; i <= cap_size; i++){
  112.         for(int j = 1; j <= req_size; j++){
  113.             total_cost += allotment[i][j] * cost[i][j];
  114.         }
  115.     }
  116.     cout << "Elements in allotment matrix-->" << endl;
  117.     displayAllotment(false);
  118.     cout << endl << endl;
  119.     cout << "Elements in cost matrix-->" << endl;
  120.     displayCost();
  121.     cout << endl << endl;
  122.     cout << "Total transportation cost using NWCM = " << total_cost << endl;
  123. }
  124.  
  125. bool NWCM::requirementFulfilled(){
  126.     bool req = true, cap = true;
  127.     // Checking if all the elements of the requirement matrix is 0
  128.     for(int i = 1; i <= req_size; i++){
  129.         if(requirement[i] != 0){
  130.             req = false;
  131.             break;
  132.         }
  133.     }
  134.     // Checking if all the elements of the capacity matrix is 0
  135.     for(int i = 1; i <= cap_size; i++){
  136.         if(capacity[i] != 0){
  137.             cap = false;
  138.             break;
  139.         }
  140.     }
  141.     return req && cap;
  142. }
  143.  
  144.  
  145. int main() {
  146.     system("cls");
  147.     cout << "Enter the number of capacities(1-9) = ";
  148.     int cap;
  149.     cin >> cap;
  150.     cout << "Enter the number of requirements(1-9) = ";
  151.     int req;
  152.     cin >> req;
  153.     NWCM obj(cap, req);
  154.     obj.input();
  155.     obj.computeTransportationCost();
  156.     return 0;
  157. }
Add Comment
Please, Sign In to add comment