Need help with two dimensional arrays

Hello,
I am currently using a two dimensional dynamic array that is

1
2
3
4
5
6
7
    int** matrix = new int*[N];
    for(int i = 0; i < N; i++)
      matrix[i]=new int[N];

    for (int i=0;i<N;i++){
      for (int j=0;j<N;j++)
      infile >> matrix[i][j];}


It reads from the infile to fill itself up.
Which will basically give me a matrix of N*N size filled up by the file that I have specified. There is no problem up until now.

Somewhere down the program i have stuff like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 while (next_permutation(x, x + N)){
   
       for (int i = 0; i < N; i++){
       	 cout << matrix[j][x[i]-1] << " ";
	 j++;
	 	 current_cost=current_cost + (matrix[j][x[i]-1]);
	    	    dummyarray[i]=x[i];
}

          if (optimal_cost=0)
	    	 optimal_cost=current_cost;

          else if (current_cost<optimal_cost){

	 	 optimal_cost=current_cost;
	 	 optimal_combo = dummyarray;
		 }
       cout << endl; j=0;current_cost=0;

       }

This basically creates a permutation of numbers that go up to N (ex. 1, 2 , 3 ,4 ,5) and then adds the cost (stored by the code above) of one element from one row chosen by the permutation.
Say that the current permutation is 5, 4, 3, 2, 1, then the program should be adding up the 5th element of the first row, 4th element of the second row, 3rd element of the second row and so on.

As you may have guessed right now, this is a cost matrix which finds the lowest cost possible to cover all the locations once.

The problem that I am having here is that, I can get the permutation part to work just fine, i can make it print just fine and it is indeed printing the cost that was stored in the matrix at the location [j][x[i]-1].

What I cannot get to work here is that it keeps giving me segmentation error when all I want to do is add the said element to the current_cost, it compiles fine and just crashed with segmentation error when I try to do the current_cost=current_cost + (matrix[j][x[i]-1]).

Current cost is an integer and so is matrix, is it illegal to add an element of a two dimensional array to a single integer variable?

Edit: Two dimensional array was something I rolled back to when my vector of vector matrix behaved the same way, so in case you are wondering, yes I have already tried out the vectors.
Last edited on
1
2
3
       	 cout << matrix[j][x[i]-1] << " ";
	 j++;
	 	 current_cost=current_cost + (matrix[j][x[i]-1])
j might get out of bounds here. Output values after you increase it or increase it after you added cost.
I see two possible things "j" is leftover from some previous operation before the while or is uninitialized, thats first

second the [x[i]-1] is going out of range. Since your vector solution was failing similarly there is problem with the array/vector going out of range or the "subscript out of range" problem

Go through you indexes to locate exact problem
Last edited on
Thanks guys,
Placing j at the bottom of the for loop fixed my problem.
I am slamming my head against the wall for not figuring this out.
Topic archived. No new replies allowed.