Please support our C++ advertiser: Programming Forums
Views: 1127 | Replies: 6
![]() |
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Hello,
I want to sort a 2dim array by columns, but there is an error in my code. It doesn't accept value of element from 2dim array into C[x]. Can anybody help please?
I want to sort a 2dim array by columns, but there is an error in my code. It doesn't accept value of element from 2dim array into C[x]. Can anybody help please?
C++ Syntax (Toggle Plain Text)
static int counting_sortx( int** A[], int** B[], int k, int rows, int col){ /*Array A[ ] stores the initial data to be sorted. Array B[ ] is used to store the final, sorted, list. Array C[ ] is used to count the occurrences of the data values */ int i,j,C[k], T[rows]; //The first for loop initialises C[] to zero. for (i=0; i<k; i++) C[i] = 0; //The second for loop increments the values in C[], according to their //frequencies in the data. for (j = 0; j < rows; j++) int x=[j][col]; C[x] = C[x] + 1; //The third for loop adds all previous values, making C[] contain a cumulative //total. for (i = 1; i < k; i++) C[i] = C[i] + C[i-1]; //The fourth for loop writes out the sorted data into array B[]. for (j = 0; j < rows; j++){ B[C[A[j][col]]] = A[j][col]; C[A[j][col]] = C[A[j][col]] - 1;} return 0; };
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
What kind of arrays are you passing to the function to begin with?
I'm pretty sure your A and B parameters will need to be modified.
I am passing dynamically allocated arrays:
C++ Syntax (Toggle Plain Text)
//create an array int** ARR = new int*[data_rows]; int** A = new int*[data_rows]; int** B = new int*[data_rows]; for(i=0;i<data_rows;i++){ ARR[i]=new int[data_columns]; A[i]=new int[data_columns]; B[i]=new int[data_columns]; } //then while (j<data_columns) { for (i=0; i<data_rows; i++){ A[i][data_columns]=ARR[i][data_columns]; } k=fRow[data_columns]+1; //give k a value of max element in current column counting_sort::counting_sortx(ARR, B, k, data_rows, data_columns); };
Last edited by WaltP : Oct 28th, 2007 at 5:26 pm. Reason: Fixed code tags -- use the PREVIEW button
Then drop the [] from your function prototype, like so
Use "post preview" to make sure you've got the code tags right before pressing submit.
static int counting_sortx( int** A, int** B, int k, int rows, int col)Use "post preview" to make sure you've got the code tags right before pressing submit.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Hello,
I found that last loop has some problem, but couldn't figure out what exactly. I am trying this:
What is wrong here?
I found that last loop has some problem, but couldn't figure out what exactly. I am trying this:
C++ Syntax (Toggle Plain Text)
for (int i=0; i<rows; i++) for (int j=0; j<col; j++){ B[C[A[i][col]]][col] = A[i][col]; C[A[i][col]] = C[A[i][col]] - 1; }
Do you have a clear idea of what a "before" and "after" array is supposed to look like?
Draw out on paper what the actions of your algorithm are supposed to do, then use a debugger to single-step through your code one statement at a time. When your code and your paper disagree, then you've found a bug.
Whether that bug is in your code or on your paper is for you to decide.
Draw out on paper what the actions of your algorithm are supposed to do, then use a debugger to single-step through your code one statement at a time. When your code and your paper disagree, then you've found a bug.
Whether that bug is in your code or on your paper is for you to decide.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
•
•
Join Date: Oct 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
I have it working now. The only thing is hard for me, in step 4, it doesn't print all rows' values, only sorted column's values. I've tried to play with col instead of scol in B[][] and A[][], but it doesn't work.
I am printing array A[][] from counting_sort file:
4 2 3 3
2 1 1 4
1 7 1 2
7 1 10 1
8 1 8 2
7 1 4 3
2 1 7 1
7 2 2 3
8 4 2 2
4 9 9 1
array C[] after step 1: 0 0 0 0 0 0 0 0 0 0 0
array C[] after step 2: 0 1 2 0 2 0 0 3 2 0 0
array C[] after step 3: 1 3 3 5 5 5 8 10 10 10
array B[][] after step 4:
1 0 0 0
2 0 0 0
2 0 0 0
4 0 0 0
4 0 0 0
7 0 0 0
7 0 0 0
7 0 0 0
8 0 0 0
8 0 0 0
C++ Syntax (Toggle Plain Text)
//scol - column by which I sort, col - total number of colums for (int j=0; j<rows; j++){ B[(C[A[j][scol]]-1)][scol] = A[j][scol]; C[A[j][scol]] = C[A[j][scol]] - 1; }
4 2 3 3
2 1 1 4
1 7 1 2
7 1 10 1
8 1 8 2
7 1 4 3
2 1 7 1
7 2 2 3
8 4 2 2
4 9 9 1
array C[] after step 1: 0 0 0 0 0 0 0 0 0 0 0
array C[] after step 2: 0 1 2 0 2 0 0 3 2 0 0
array C[] after step 3: 1 3 3 5 5 5 8 10 10 10
array B[][] after step 4:
1 0 0 0
2 0 0 0
2 0 0 0
4 0 0 0
4 0 0 0
7 0 0 0
7 0 0 0
7 0 0 0
8 0 0 0
8 0 0 0
Last edited by Malaisary : Oct 30th, 2007 at 6:21 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode