Hello yhuong,
Welcome to the forum.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I will cover everything up to the while loop. I still have to figure out what is happening with the while loop.
I have made some changes to your original code to make it easier to read and to make it more understandable. I will be referring to the following code in my exploitation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <iostream>
#include <limits>
using namespace std;
int main()
{
constexpr int MAXSIZE{ 20 };
int i{ 0 }, j{ 0 }, min{ 0 }, row{ 0 }, col{ 0 }, b{ 0 }, d{ 0 }, c2{ 0 }, c1{ 0 }, p{ 0 }, q{ 0 }, sum = 0; // Should initialize all variables.
// Initialized these arrays to make testing easier. You can change the values to whatever you want.
int c[MAXSIZE][MAXSIZE]{ {100, 200 ,300}, {400, 500, 600}, {700, 800, 900} }; // <--- Change to { 0 } to initialize the arrays to zeros when the following code is uncommented.
int dem[MAXSIZE]{ 10, 20, 30 };
int sup[MAXSIZE]{ 40, 50, 60 };
int rf[MAXSIZE]{ 0 }, cf[MAXSIZE]{ 0 }; // Initialies entire array to zeros.
cout << "\n number of Row: ";
cin >> row;
cout << "\n number of Columns: ";
cin >> col;
// commented out these lines to make testing easier.
//cout << "\n Cost: \n"; //matrix
//for (i = 0; i < row; i++)
//{
// for (j = 0; j < col; j++)
// {
// cout << " Cost Matrix : " << (i + 1) << ", " << (j + 1) << ": "; // <--- Removed \n added space
// cin >> c[i][j];
// }
//}
//cout << "\n Demand: \n";
//for (i = 0; i < col; i++)
//{
// cout << " Demand: " << (i + 1) << ": ";
// cin >> dem[i];
//}
//cout << "\n Supply: \n";
//for (i = 0; i < row; i++)
//{
// cout << " Supply : " << (i + 1) << ": ";
// cin >> sup[i];
//}
cout << "\n Matrix: Supply\n";
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
cout << " | " << c[i][j];
cout << " " << sup[i]; // <--- This is part of the outer for loop.
cout << " \n";
}
std::cout << "Demand: ";
for (j = 0; j < col; j++)
cout << " " << dem[j];
for (i = 0; i < row; i++)
rf[i] = 0;
for (i = 0; i < col; i++)
cf[i] = 0;
b = row;
d = col;
|
Line 1 is the header file to work with "cin" and "cout"
line 2 is the header fie to work with tha last lines of code before the "return 0;". Not necessary to fully understand everything about the "limits" header file to use it.
Line 4 allows you to write "cin" ans "cout" with out the quailification of "std::". This line is not the best idea and will cause you problems in the future. It is better to learn "std::cin" and "std::cout" along with other things in the "std" namespace. "using" stateents are slightly better, but should be avaoided also.
Line 8 defines a constant variable "MAXSIZE" that I added to show a better way of defining arrays. This way you only have to change the value in one place. its just good practice and good coding.
Line 10 defines the simple variables. I changed "m" and "n" to "row" and "col" to help make the program easier to understand. I am thinking this is a program from a book and set up this way to make you think, but these variable names work better if they are real names to help describe what they do. Last note: all these variables should be initialized when they are defined and before they are used to avoid any problems.
Lines 12 - 15 defines all your arrays. I initialized the arrays for testing the program so I would not have to enter all those numbers.
Lines 18 - 21 Prompts the user to enter the size of the array to use. It would also be a good idea to tell the user not to exceed 20 or add code to check the number entered to be > zero or < 21.
Lines 24 - 32 Prompts the user to enter numbers into the 2D array. Note: Best to think of the array in the foramt of rows and coloumns.
Lines 35 - 47 Prompts the user to enter information into the demand and sypply arrays. Fairly straight forward here.
Lines 49 - 57 Prints out the 2D array with the amount of the sullpy array printed at the end of the row. I changed line 49 to give a better idea of what the numbers mean.
Lines 59 - 62 Prints the demand array.
Lines 66 - 69 Sets the "rf" and "cf" arrays to zero. A way of setting all the elements of the array if the array was not initialized.
Lines 71 and 72 set the varibles "b" and "d" to the values of "row" and "col". This way the values of "b" and "d" can be changed without changing the values of "row" and "col".
Notice that I put some comments in the code also.
Do not be afraid to use blank lines and spaces in your code. It makes it easier to read.
Now I will work on the rest of the program to figure out how it works and what it is doing.
Hope that helps for now,
Andy