c++ - how to code this northwest method -


i'm student trying learn programming , have never done complex coding before. lecturer gave me task on northwest corner method. followed code found on internet there seems problems code cannot figure out i'm still beginner. did readings still couldn't figure out problems , quite sure there many problems lies coding. i'm thankful willing take on coding. in advance. :)

#include "stdafx.h" int _tmain(int argc, _tchar* argv[]) {     return 0; }  using namespace std;  #include <iostream> #include <fstream>  const int row_max =4; const int col_max =4; int i,j;  //create supply_array , require_array float supply_array[row_max]; float require_array[col_max];  //creating cost matrix , unit matrix float cost_matrix[row_max][col_max]; float unit_matrix[row_max][col_max];  //initialize cost_matrix  int main() {     for(i=0 ; i<=row_max ; i++)     {         for(j=0 ; j<=col_max ; j++)         {             cin >> cost_matrix[i][j];         }     }      //initialize unit_matrix     for(i=0 ; i<=row_max ; i++)     {         for(j=0 ; j<=col_max; j++)         {             unit_matrix[i][j]=0;         }     }      float cost_minimal= 0.0;     float *supply_ptr;     float *require_ptr;     supply_ptr = &supply_array[4];     require_ptr = &require_array[4];      //initialize supply_array     for(i=0 ; i<=row_max ; i++)     {         cin >> supply_array[i];     }      //initialize require_array     for(i=0 ; i<=col_max; i++)     {         cin>>require_array[i];     }      float *matrix_ptr;     matrix_ptr = &cost_matrix[0][0];     int r=0,c=0,x=0,y=0;     while((x<= row_max) &&( y=col_max))     {         if(*require_ptr>*supply_ptr)         {             unit_matrix[x][y]=supply_array[x];             require_array[y]=require_array[y]-unit_matrix[x][y];             supply_array[x]=supply_array[x]-unit_matrix[x][y];             cost_minimal=cost_minimal+unit_matrix[x][y];             x=x+1;             supply_ptr=supply_ptr+1;             matrix_ptr=matrix_ptr+col_max;              continue;         }          if(*require_ptr<*supply_ptr)         {             unit_matrix[x][y]=require_array[y];             require_array[y]=require_array[y]-unit_matrix[x][y];             supply_array[x]=supply_array[x]-unit_matrix[x][y]*cost_matrix[x][y];             y=y+1;             x=x+1;             require_ptr=require_ptr+1;             supply_ptr=supply_ptr+1;             matrix_ptr=matrix_ptr+col_max;              continue;         }     }      //displaying unit matrix     for(i=0;i<=row_max;i++)     {         for(j=0;j<=col_max;j++)         {             cout<<unit_matrix[i][j];         }     }      //displaying minimal cost     cout<< "the minimal cost obtained : "<<cost_minimal;     return 0; } 

one apparent issue in code array index starts 0 , ends on size of array minus 1.

 for(i=0 ; i<=row_max ; i++)  {         //^^should <     for(j=0 ; j<=col_max ; j++)     {        //^^should <        cin >> cost_matrix[i][j];     }  } 

since row_max , col_max 4 in case, indices in interval[0,3], cannot access cost_matrix[4][4].

meanwhile, either use main or _tmain(depends kind of c++ project creating), should not have both in same source file.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -