Ok this is very strange and i cannot see where is the problem and i guess is really easy but i cannot see it so i will need your help :-O

My Declarations

int myGraph[4][4];
	int vertices[4];
	char path[4];

My initialize code

for(int i=0; i<=4;i++)
	{
		for(int z=0; z<=4; z++)
			myGraph[i][z] = -1;
	}
	for(int y=0; y<=4;y++)
	{
		vertices[y] = 9999;
		path[y] = 's';
	}
	

	myGraph[0][1] = 7;
	myGraph[0][3] = 2;
	myGraph[1][2] = 1;
	myGraph[1][3] = 3;
	myGraph[2][4] = 4;
	myGraph[3][1] = 2;
	myGraph[3][2] = 8;
	myGraph[3][4] = 5;
	myGraph[4][2] = 5;
	vertices[0] = 0;

Can anyone please tell me if i have done something wrong in this code??????

Because strangely the program is initialize myGraph to -1 and also the array vertices to -1 :S How is that possible?

How it confuse the two arrays? They have different name but same size is that a problem?
(I have seen the previous operations while debugging using a watch)
Screenshot:
[img]http://img254.imageshack.us/img254/2216/untitleg.jpg[/img]


Now because the vrtices are reinitialized after the initialization of myGraph the get the value 9999 correctly BUT when i reach line myGraph[3][4] = 5; then the vertice array change to 5 :S
Screenshot
[img]http://img262.imageshack.us/img262/9552/42388498.png[/img]


Is driving me crazy and if the solution is obvious i cannot see it ...:S

Edited 5 Years Ago by christos312: n/a

Your for loops perform 5 iterations, they should only perform 4. Your loop should end at (arraySize - 1), not array size.

This is incorrect:

const int arraySize = 5;

int myArray[arraySize] = {0};

for (int i = 0; i <= arraySize; ++i) {
  myArray[i] = i * 2;
}

This is the correct way:

const int arraySize = 5;

int myArray[arraySize] = {0};

for (int i = 0; i < arraySize; ++i) {
  myArray[i] = i * 2;
}

Notice the difference between the 2 versions on Line 5.

The behavior you describe occurs because Element 4 of myGraph that you are trying to access doesn't actually exist and vertices is directly adjacent to it in memory. As a result, your changes to those elements of myGraph overwrite the elements of vertices.

Edited 5 Years Ago by Fbody: n/a

:| I am really embarrassed :sad:

I knew it was something easy but i couldn't spot it

Thank you very much

This question has already been answered. Start a new discussion instead.