I have problem with while loop, as it seems. It does just one iteration. Is there any suggetsions?
My code:
#include <stdafx.h>
#include <iostream>
using namespace std;
bool notEmpty(int * array, int arraySize)
{
int sum = 0;
for (int i = 0; i < arraySize; ++i)
sum += array[i];
return sum == 0;
}
int main()
{
int resources[] = { 200, 180, 190 };
int needs[] = { 150, 130, 150, 140 };
int m = 3, n = 4;
int outgoings[3][4] = { { 7, 8, 1, 2 },
{ 4, 5, 9, 8 },
{ 9, 2, 3, 6 } };
int result[3][4] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
int i = 0, j = 0;
bool resultBool;
do
{
resultBool = false;
if (resources[i]>needs[j])
{
result[i][j] = needs[j];
resources[i] -= needs[j];
}
else if (resources[i]<needs[j])
{
result[i][j] = resources[j];
needs[j] -= resources[i];
}
else goto l;
l:resultBool = notEmpty(needs, 0);
}
while (!resultBool);
cout << "Path: " << endl;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
cout << result[i][j] << " ";
cout << endl;
}
double sum = 0;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
sum += result[i][j] * outgoings[i][j];
}
cout << "Resources: " << sum;
cout << endl;
system("pause");
}
Output:
Path:
150 0 0 0
0 0 0 0
0 0 0 0
Resources: 1050
But the output have to be like in the picture linked (bold text):
https://drive.google.com/file/d/0B2RsqtI-JTVxR3pMeTlzN2UyX28/view?usp=sharing[
^]
So it does first iteration well, and then does nothing...
Help, please!