Ok, so I've been working on this code for a day now and i thought I had all the kinks worked out, but my program isn't working right. I'm writing this code to simulate a game (basically a simulation of a simulation) used to illustrate some topics we are trying to teach at work. I'll try to explain a few things then get to the code. As of now the game is running with 3 colors of cards (which i use rows in a dynamic 2d array to keep up with, ie row 0 of every array is the number of cards that are red), and the cards are moving from an assembly department to a kanban board to a paint department. the cards are used as a linkage between the assembly department and the paint department.

With this said, when I run the program without debugging, I think all the arrays are working properly except kanbanBoard, which is used to calculate some of the other arrays, and I think the errors in that array are causing the very wrong numbers in the other arrays. I have stepped through the code on paper and it seems like the logic is coded correctly but I'm not sure.
The updateKanbanBoard function is supposed to remove a card from the largest stack and repeat that process until it has done that step the same number of times are there are in the paint/assembly department. then if cardsRemoved!= the number of participants then it should remove 1 from each stack starting at the left until cardsRemoved==the number of participants.

Now that all of that is explained, when I run the code I get trash data from kanbanBoard propagating through the others. When I debug the code it runs until it hits line 122 then it gives me this message

'Kanban Simulation.exe': Loaded 'C:\Documents and Settings\jpettitt\My Documents\Visual Studio 2008\Projects\Kanban Simulation\Debug\Kanban Simulation.exe', Symbols loaded.
'Kanban Simulation.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'Kanban Simulation.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'Kanban Simulation.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll'
'Kanban Simulation.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll'
The program '[3704] Kanban Simulation.exe: Native' has exited with code 0 (0x0).

Which I'm pretty sure means the program exited. Why would it do this in the middle of a function when I hadn't told it to exit? Why does the program run to completion when I don't debug but with trash data?

The numbers I am inputting to test the program are: (just press the number then press enter and repeat.)
3,5,3 3,3,3 3,3,3 1,1,1 1,1,1 1,2,0 0,1,2 1,0,2 0,2,1 1,1,1

here is the code

I'm fairly sure that the problem is in updateKanbanBoard, but I posted the whole code because the problem might not be there, and it wasn't to terribly long.

#include <iostream>
#include <iomanip>
using namespace std;

void getStartState(char*, int**);
void printArray(char*, int**);
void updateWarehouse(int **, int **, int**);
void updateKanbanBoard(int**, int**, int**);
void updatePaint(int**, int**, int**);
int row=0;
int column=0;
int participants=0;

int main ()
{
	int roundNum=1;
	cout<<"How many colors will there be? ";
	cin>>row;
	cout<<"How many rounds? ";
	cin>>column;
	cout<<"How many assemblers/painters? ";
	cin>>participants;
	int** warehouse=new int*[row];
	for (int x=0;x<row;x++)
		warehouse[x]=new int[column];

	int** kanbanBoard=new int*[row];
	for (int x=0;x<row;x++)
		kanbanBoard[x]=new int[column];

	int** paint=new int*[row];
	for (int x=0;x<row;x++)
		paint[x]=new int[column];

	int** assembly=new int*[row];
	for (int x=0;x<row;x++)
		assembly[x]=new int[column];

	int** temp=new int*[row];
	for (int x=0;x<row;x++)
		temp[x]=new int[column];

	cout<<"Enter start state for game (make sure you put the\n";
	cout<<"colors into the program in the same order everytime.)\n";

	getStartState("Warehouse",warehouse);
	getStartState("Kanban Board",kanbanBoard);
	getStartState("Paint",paint);
	//getStartState two (special case for paint)
	cout<<"Paint round 2"<<endl;
	for (int x=0;x<row;x++)
		cin>>paint[x][1];
	getStartState("Assembly round 2",assembly);

	while(roundNum<column)
	{
		cout<<"Enter Assembly numbers for round "<<roundNum+2<<endl;
		for (int x=0;x<row;x++)
		{
			cin>>assembly[x][roundNum];
		}
		roundNum++;
	};

	updateWarehouse(warehouse, paint, assembly);
	updateKanbanBoard(kanbanBoard, assembly, temp);
	updatePaint(kanbanBoard, paint, temp);

	printArray("Warehouse",warehouse);
	printArray("Kanban Board",kanbanBoard);
	printArray("Paint",paint);
	printArray("Assembly",assembly);

	return 0;
}

void getStartState(char* a, int** b)
{
	cout<<a<<endl;
	for (int x=0;x<row;x++)
		cin>>b[x][0];
};
void printArray(char* a, int** b)
{
	cout<<a<<endl;
	for (int x=0;x<row;x++)
	{
		for (int i=0;i<column;i++)
			cout<<b[x][i]<<",";
		cout<<endl;
	}
	cout<<endl;
};

void updateWarehouse(int** warehouse, int** paint, int** assembly)
{
	for (int x=0;x<row;x++)
	{
		for (int i=1;i<column;i++)
		{
			warehouse[x][i]=(warehouse[x][i-1]+paint[x][i-1])-assembly[x][i-1];
		}
	}
};

void updateKanbanBoard(int** kanban, int** assembly, int**temp)
{
	for (int r=0;r<row;r++)
	{
		temp[r][0]=kanban[r][0];
	}
	for (int c=1;c<column;c++)
	{
		for(int r=0;r<row;r++)
		{
			kanban[r][c]=kanban[r][c-1]+assembly[r][c-1];
			temp[r][c]=kanban[r][c];
		}
		int cardsRemoved=0;
		for (int x=0;x<participants;x++)
		{
			int greatestNum=-1;
			for (int r=1;r<row;r++)
			{
				if(greatestNum!=-1)
				{
					if(kanban[greatestNum][c]<kanban[r][c])
						greatestNum=r;
				}
				else
				{
					if(kanban[r][c]>kanban[r-1][c])
						greatestNum=r;
					if(kanban[r-1][c]>kanban[r][c])
						greatestNum=r-1;
				}
			}
			if(greatestNum!=-1)
			{
				kanban[greatestNum]=kanban[greatestNum]-1;
				cardsRemoved++;
			}
		}
		while (cardsRemoved<participants)
		{
			int r=0;
			kanban[r][c]=kanban[r][c]-1;
			r++;
			cardsRemoved++;
		}
	}
};

void updatePaint(int** kanban, int** paint, int** temp)
{
	for(int c=2;c<column;c++)
	{
		for(int r=0;r<row;r++)
		{
			paint[r][c]=temp[r][c-1]-kanban[r][c-1];
		}
	}
};

Recommended Answers

All 10 Replies

Plu-cha... before I go diving into analyzing 2d arrays and such, I want to see if maybe the following easy fixes help anything:

-- Semi-colons.

while(roundNum<column)
	{
		cout<<"Enter Assembly numbers for round "<<roundNum+2<<endl;
		for (int x=0;x<row;x++)
		{
			cin>>assembly[x][roundNum];
		}
		roundNum++;
	};  // you dont need me  <--------------------------------- look
void getStartState(char* a, int** b)
{
...

};  < -------------------------------------------  me either
void printArray(char* a, int** b)
{

...

};  < --------------------------------------------------  me either

void updateWarehouse(int** warehouse, int** paint, int** assembly)
{

...

};  < ------------------------------------------------------  me either

void updateKanbanBoard(int** kanban, int** assembly, int**temp)
{

...

};  < -----------------------------------------  me either

void updatePaint(int** kanban, int** paint, int** temp)
{

...

};  <------------------------------------- me either

See if that helps any

I removed the semi-colons and I ran without debugging and with debugging, both gave same results as before.

(I would have sworn that you needed those though, guess I just remembered something wrong from intro years ago.)

I was prepared for the possibility that they would solve nothing. Alright I'm going to look at your code a little bit more and see if I can find anything else.

Awesome, I really appreciate the help, I've been banging my head against my desk all morning trying to fix this...

Also, i know that I never de-allocated the memory from all those pointers, but I wanted to get the program giving correct numbers before i worried about possible memory leaks, is this an ok strategy or could this possibly be the problem?

Also, since I didn't have line numbers on the posted code the line in updateKanbanBoard where greatestNum is declared and initiated is the line where the debugger exits the program I believe. It is line 122 though.

alrighty, so with those specific numbers the output looks like this:

How many colors will there be? 3
How many rounds? 5
How many assemblers/painters? 3
Enter start state for game (make sure you put the
colors into the program in the same order everytime.)
Warehouse
3
3
3
Kanban Board
3
3
3
Paint
1
1
1
Paint round 2
1
1
1
Assembly round 2
1
2
0
Enter Assembly numbers for round 3
0
1
2
Enter Assembly numbers for round 4
1
0
2
Enter Assembly numbers for round 5
0
2
1
Enter Assembly numbers for round 6
1
1
1
Warehouse
3,3,4,3,3,
3,2,2,2,0,
3,4,3,1,0,

Kanban Board
134525044,134525076,25,2,2,
2,3,3,0,2,
0,0,25,3,3,

Paint
1,1,-134525072,-23,1,
1,1,2,1,25,
1,1,3,-20,2,

Assembly
1,0,1,0,1,
2,1,0,2,1,
0,2,2,1,1,

So, as you said before KanbanBoard needs some attention. More than likely (obviously even) the Kanban Board is either not getting the correct value or is referencing the wrong array item.

Digging in now.. bbl

What comes up when I run mine looks a little different. I guess its just because of the way pointers work with pointing to memory and such.

How many colors will there be? 3
How many rounds? 5
How many assemblers/painters? 3
Enter start state for game (make sure you put the
colors into the program in the same order everytime.)
Warehouse
3
3
3
Kanban Board
3
3
3
Paint
1
1
1
Paint round 2
1
1
1
Assembly round 2
1
2
0
Enter Assembly numbers for round 3
0
1
2
Enter Assembly numbers for round 4
1
0
2
Enter Assembly numbers for round 5
0
2
1
Enter Assembly numbers for round 6
1
1
1
Warehouse
3,3,4,-842150448,-1684300899,
3,2,2,-842150449,-1684300902,
3,4,3,-842150450,-1684300902,

Kanban Board
1,143,-33686019,2,2,
1,144,-33686019,-33686019,-33686017,
0,20,1,145,-33686019,

Paint
1,1,-139,33686021,1,
1,1,-139,33686023,0,
1,1,-17,4,-140,

Assembly
1,0,1,0,1,
2,1,0,2,1,
0,2,2,1,1,

Press any key to continue . . .

A rather strange loop, see what happens:

while (cardsRemoved<participants)
{
    int r = 0; // r created on every loop, initialized 0
    kanban[r][c]=kanban[r][c]-1; // always [0][c] = [0][c]
    r++; // incremented, but r shall die soon
    cardsRemoved++; // automatic r ended
}

Probably you forgot that the loop body is a block. The r variable is declared in this block. It's created anew with value 0 on every loop. Result: kanban[0][c] decremented while cardsRemoved<participants . Obviously it's not a desired effect.

Regrettably, I have no time to check up more codes at the moment. Correct (trivial correction) this fragment and continue...

ArkM thanks for pointing that out, I hadn't paid enough attention to that particular loop to notice that it wasn't really doing what I intended. After the change I still get the garbage data and the debugger exiting, but that is one problem that would have kept the program from working correctly so I appreciate it.

new code (can be pasted over the previous while loop, and I used z since I hadn't used it previously in the program)

int z=0;
		while (cardsRemoved<participants)
		{
			kanban[z][c]=kanban[z][c]-1;
			z++;
			cardsRemoved++;
		}

Ok, first thanks very much for the help. I took the code home with me and continued working on it when I got home from work. I am fairly sure that the program is working correctly now, but I'm still not sure why my work computer was exiting during the debugging, because my personal laptop ran the debugger without exiting.

Here were the problems

if(greatestNum!=-1)
			{
				kanban[greatestNum][c]=kanban[greatestNum][c]-1;
				cardsRemoved++;
			}

In my original code for this section I only told it what row to access and didn't specify which column, so it kept re-writing over column one and not initializing the rest of the columns, hence the trash data.

updateKanbanBoard(kanbanBoard, assembly, temp);
	updatePaint(kanbanBoard, paint, temp);
	updateWarehouse(warehouse, paint, assembly);

I was also getting trash data from this section of the original code because I was calling updateWarehouse which accessed data from paint, which wasn't initialized yet because I hadn't called updatePaint and caused trash data.

again, thanks for all the help. sorry for the trouble

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.