•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,577 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,634 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1088 | 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
•
•
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.
•
•
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- infile into array and sort (C++)
- What Is The Name Of This Sort Algorithm?? (C)
- Index array & sort algorithm??? (Java)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Can you guys help me? about Quick Sort Algorithm (C)
- sorting an array of string (C)
Other Threads in the C++ Forum
- Previous Thread: c++ temperature conversion
- Next Thread: Read string and use in switch statement



Linear Mode