can you help me explanation for all this code???

this code about least cost method. Thank you!!!
#include<iostream>
using namespace std;
int main()
{

int c[20][20],
i,j,
min, m, n, b, d, c2, c1, p, q;
int dem[20],sup[20],rf[20],cf[20],sum=0;


cout<<"\n number of Row: ";
cin>>m;
cout<<"\n number of Columns: ";
cin>>n;


cout<<"\n Cost: \n"; //matrix
for(i=0;i<m;i++)
{
for(j=0; j<n; j++)
{
cout<<" Cost Matrix : \n"<< (i+1) << (j+1) ;
cin>> c[i][j];
}
}


cout<<"\n Demand: \n";
for(i=0; i<n; i++){
cout<<" Demand [ : \n"<< (i+1) ;
cin>> dem[i];
}

cout<<"\n Supply: \n";
for(i=0; i<m; i++)
{
cout<<" Supply : "<<(i+1);
cin>> sup[i];
}

cout<<"\n Matrix:\n";
for(i=0; i<m; i++){
for(j=0; j<n; j++)
cout<<" | "<<c[i][j];
cout<<" "<<sup[i];
cout<<" \n";
}
for(j=0; j<n; j++)
cout<<" "<<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;
}
}
}
}
}
}
if(sup[p]<dem[q])
{ 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--;
}
}
cout<<"\n\n total cost \n\n"<<sum;
}
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
Last edited on
Hello yhuong,

Part two:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#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;


	while (b > 0 && d > 0)
	{
		min = 1000;
		for (i = 0; i < row; i++)
		{
			if (rf[i] != 1)
			{
				for (j = 0; j < col; 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 < row; i++)
		{
			if (rf[i] != 1)
			{
				for (j = 0; j < col; 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;
							}
						}
					}
				}
			}
		}

		if (sup[p] < dem[q])
		{
			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--;
				}
	}
	cout << "\n\n total cost = " << sum;

	//  These lines added to keep the console window from closing to soon.
	//  See Console Closing Down http://www.cplusplus.com/forum/beginner/1988/
	std::cout << "\n\n\n\n Press enter to continue";
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Used to remove the "\n" from the input buffer.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- wait for enter to be pressed.

	return 0;  // <--- Added needs to be here for proper coding since main returns an int, but not necessary.
}


Line 77 - 95 will determine the lowest value of the 2D array. Not sure what the if statements at lines 80 and 84 are for yet. Lines 89 and 90 set the variables "p" and "q" for later use because "i" and "j" go out of scope when th for loops end.

Lines 97 - 100 determine a value for "c1", but I do not see what it is used for yet.

Lines 102 - 128 determine values for "c1" and "c2", but I am not sure yet what all this is good for because "c1" and "c2" are not used after this for loop. It may have some future use though.

The if/else statements starting at line 130 determines what value is added to sum based on the subscripts "p" and "q". Here is where "b" and "d" are used so that "row" and "col" keep their original values.

Finally line 155 prints the value of sum. I changed the output so it looks better.

Lines 159 - 161 is what I added to keep the program from ending and keep the console window open to to see what has been printed.

Line 160 will remove the "\n" from the input buffer so that line 161 will wait for the enter key. This is a quirk of "cin" leaving the "\n" in the input buffer.

Line 163 Added. The program will run with out this line, but it is good practice and good coding to put it there.

If there is anything you do not understand let me know;

Hope that helps,

Andy
Topic archived. No new replies allowed.