2D array

Hi guys,
In the following code, the variable adjmatrix doesn't take the value I want it to take. 'Procedure' is the name of a struc t and procedure[].process_node1 is of type char. procedure[].process_node2 is also of type char. procedure[].process_consomation is of type float.'Node' is the name of a struct and node[].milestone_nom is of type char.

For example: procedure[0].process_node1 = 'Start' and node[0].milestone_nom ='Start'. procedure[0].process_node2 = 'A' and node[1].milestone_nom.
As the value of procedure[0].process_consomation is equal to -3.88 ,
the values of adjmatrix[0][1] and adjmatrix[1][0] should be equal to -3,88. But this code outputs 0 as value for adjmatrix[0][1] and adjmatrix[1][0]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
float adjmatrix [12][12];
	for (int a=0; a<16; a++){
		for (int b=0; b<12; b++){
			if (procedure[a].process_node1 == node[b].milestone_nom){
					for (int c=0;c<12;c++){
							if (procedure[a].process_node2 == node[c].milestone_nom)	{
								adjmatrix[b][c]= procedure[a].process_consomation;
								adjmatrix[c][b]= procedure[a].process_consomation;
							}

					}
		}
	}
	}
process_node1 is a char you say? But you're trying to assign it the value 'Start'.
You need to turn on warnings, because you're probably getting a warning like
warning: character constant too long for its type
warning: overflow in implicit constant conversion [-Woverflow]


A character literal containing more than one character is implementation-defined behavior, which in this case means "don't do that".

If you need to store a word like "Start", use a string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example program
#include <iostream>
#include <string>

int main()
{
    std::string arr[5];
    
    arr[3] = "Start"; // notice: double-quotes
    
    if (arr[3] == "Start")
    {
        std::cout << "Start!\n";
    }
}
Last edited on
> For example: procedure[0].process_node1 = 'Start' and node[0].milestone_nom ='Start'.
What type are those variables containing strings?

If you want to use ==, then they need to be std::string.

If this is C, or you're using plain-old char arrays, then you need to use strcmp()

Of course, you should be using the debugger and putting a breakpoint on line #7 to see if that code is ever executed.

Or put a breakpoint on #4 and start single-stepping the code until it does something you didn't expect.

Debuggers are marvellous tools.
I defined both structures as :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct milestone
				{
					int milestone_x;
					int milestone_y;
					char milestone_nom[6];
				};

struct process
			{
			  char process_nom[20];
			  float process_consomation;
			  char process_node1[6];
			  char process_node2[6];
						};
		process procedure[16]


So 'start' should fit in char milestone_nom[6] , isn't it?

Last edited on
process_node1 is not a char, it's an array of chars. This is an important difference.

With this information, follow what salem c said and either use std::strings, or use strcmp to compare "c-strings" (null-terminated character arrays).
http://www.cplusplus.com/reference/cstring/strcmp/
Last edited on
> So 'start' should fit in char milestone_nom[6] , isn't it?
as ganado said, 'start' is «implementation-defined behavior, which in this case means "don't do that".»
what you want is "start"
then I wonder how did you set the value, ¿strcpy()?

http://www.cplusplus.com/forum/general/259503/
following that, I doubt that you even need a char array, may simply translate "Start" and "End" to 1 and 2.


1
2
3
4
5
6
float adjmatrix [12][12];
for (int a=0; a<16; a++){
	int b = find_index(node, procedure[a].process_node1);
	int c = find_index(node, procedure[a].process_node2);
	adjmatrix[b][c] = adjmatrix[c][b] = procedure[a].process_consomation;
}
find_index() should be easier to debug.
Last edited on
Topic archived. No new replies allowed.