-2

I have an algorithm to sort a 2d array using the selection sort method. I have two variables to store the total number of permutations and the total number of comparisons after the matrix is sorted. Everything almost works well just that the I am getting false values for the variables (selection_count and selection_perm_count) I created. What am I doing wrong?

#include <iostream>
void sort(int **arr, int n, int m, int &selectionCount, int &spermuTation)
    {
        int min_idx;
        
        for (int i = 0; i < n; i++)
        {
            
            for (int j = 0; j < m - 1; j++)
            {
                min_idx = j;
                
                for (int k = j; k < m; k++)
                {

                    if (arr[i][k] > arr[i][min_idx])
                    {
                        min_idx = j;
                        spermuTation++;
                    }
                }
                if (min_idx != j)
                {
                    int tempVal = arr[i][min_idx];
                    arr[i][min_idx] = arr[i][j];
                    arr[i][j] = tempVal;
                    
                }
            }
        }
        
        cout << endl;
        cout << "Selection sort" << endl;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cout << "[" << i << "][" << j << "]: ";
                cout << arr[i][j] << " ";
            }
            cout << endl;
        }
        
    } 
int main()
{
int **matrix,N,M;
//some code for user to input matrix size and matrix allocation...etc.
int selection_count = 0;
int selection_perm_count = 0;
sort(matrix, N, M, selection_count, selection_perm_count);

cout << endl << "Number of comparisons: " << selection_count;
cout << endl << "Number of permutations: " << selection_perm_count << endl;
}
NIi
  • 1
  • 1
  • *Everything works well* -- If everything was working well, there would be no issues. -- *What am I doing wrong?* -- [What is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – PaulMcKenzie May 29 '22 at 07:43
  • 1
    `int **matrix` -- This is not a 2D Array. It is a pointer to a pointer. A real 2D array is something like this: `int matrix[20[20];`. Second, you picked up the `int **matrix;` syntax from somewhere -- wherever that place you picked up that line of code from, did you not also see where `new[]` was being used to allocate the memory used? Your code has no allocation of memory whatsoever. – PaulMcKenzie May 29 '22 at 07:46
  • @PaulMcKenzie I didn't add the code to allocate memory to the matrix because that is not the main focus here. – NIi May 29 '22 at 08:46
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 29 '22 at 09:27
  • Is the line `//some code for user to input matrix size and matrix allocation...etc.` supposed to be placeholder for code that you are not showing? If that is the case, then please note the following: Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. This allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel May 29 '22 at 09:31
  • @Andreas Wenzel Alright, I understand you but the program I'm working on involves many lines of code and files so I thought I'd just show the important aspect relating to my question – NIi May 29 '22 at 13:05
  • @NIi: Nobody is asking you to show your entire code. We are only asking for a [mre]. One way to create such an example would be to hard-code the input into the program. – Andreas Wenzel May 29 '22 at 13:09
  • @NIi *the program I'm working on involves many lines of code* -- You approached asking the question the wrong way. The first thing you should have done is *isolate* the program down to the `sort` function, and a `int main()` with hardcoded data that demonstrates the issue, and then post that program here. Then the next step would have been to debug the program. Let's assume the program contains "many lines" -- how would *you* debug such a program? – PaulMcKenzie May 29 '22 at 14:55
  • @NIi Right now, I am working on a program with many thousands of lines of code -- but if a function acts up, I can take that function, plug it into a simple program, and test it with various data. That's what you should strive to do. – PaulMcKenzie May 29 '22 at 14:58

0 Answers0