Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have problem with while loop, as it seems. It does just one iteration. Is there any suggetsions?

My code:
C#
//libraries
#include <stdafx.h>
#include <iostream>

//standard namespace
using namespace std;

//function to check if array is not empty
bool notEmpty(int * array, int arraySize)
{
	int sum = 0;
	for (int i = 0; i < arraySize; ++i)
		sum += array[i];

	return sum == 0;	// if sum==0, true returned, else false
}


int main()
{
	//setting resources array
	int resources[] = { 200, 180, 190 };
	//setting needs array
	int needs[] = { 150, 130, 150, 140 };

	int m = 3, n = 4; //m - resources fields, n - needs fields

	//setting outgoings
	int outgoings[3][4] = { { 7, 8, 1, 2 },
	{ 4, 5, 9, 8 },
	{ 9, 2, 3, 6 } };

	//setting result array to write progress
	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; //check for zero equal elements

		if (resources[i]>needs[j]) //if there's more resources then needss
		{
			result[i][j] = needs[j]; //needs satisfied
			resources[i] -= needs[j]; //resources left
			
			
		}
		else if (resources[i]<needs[j]) //if not enough resources
		{
			result[i][j] = resources[j]; //giving as many resources as possible
			needs[j] -= resources[i]; //how much is still needed?
		}
		
		
		else goto l; //go to check nulling
	l:resultBool = notEmpty(needs, 0);	//check nulling
	}

	//outputing data
	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; //path price
		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:
C#
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!
Posted
Comments
phil.o 16-Dec-15 16:40pm    
Semantically, I find quite disturbing the fact that your "notEmpty" method returns true when the sum is zero. According to what it does, it should be named "empty".
Maybe it's the problem? Maybe you are expecting the opposite value of the one that is actually returned?

1 solution

You really need to clean up your code indentation. It'll make your code much easier to read and debug.

Also, using the debugger and stepping through the code and examining variables greatly helps to solve problems like this. It's your logic and we have no idea what you're trying to do with it or what it should be doing.

I really question why you even need the goto statement in that code, because you don't. Removing that statement (and the else right above it) doesn't change the behavior or the code at all.

This appears to be homework. We're not going to do it for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900