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?

``````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;
};``````

## All 9 Replies

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.

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:

``````//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);
};``````

Then drop the [] from your function prototype, like so `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.

Hello,
I found that last loop has some problem, but couldn't figure out what exactly. I am trying this:

``````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;
}``````

What is wrong here?

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.

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.

``````//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;
}``````

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

thank you....

thank you

thank you
can you using the code

``````#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int A[100];         // Array A[ ] initial data to be sorted
int B[100];         // B[ ] using to final sorted
int size;            //Enter The Size Of Array
cout<<"\n\n Enter The Size of Array :";
cin>>size;

for(int i=0;i<=size;i++)
A[i]=(rand()%size)+1;           //using to insert random element in the array

cout<<"\n\n The Element Before Sorting: \n";
for(int i=0; i<=size;i++)
cout<<A[i]<<" , ";

int temp=A[0];
for(int i=0;i<=size;i++)         // using to find the max element in the array
{
if(A[i]>temp)
temp=A[i];
}
int K=temp;
cout<<"\n\n Press enter To Sorting Element";
getch();

int C[100];
for(int i=1;i<=K;i++)                         //using to initialization Array C
C[i]=0;

for(int j=1;j<=size;j++)
C[A[j]]=C[A[j]]+1;                        //using To Counting The Value In Array A Become Index In Array C

for(int i=2;i<=K;i++)
C[i]=C[i]+C[i-1];                        //Used To Collect All The Values With Each Other

for(int j=size;j>=0;j--)                       //Using To Final Aorted
{
B[C[A[j]]]=A[j];
C[A[j]]=C[A[j]]-1;
}

cout<<"\n\n Press Enter To Print The Array After Sorting :\n";
getch();
for(int i=0;i<=size;i++)
cout<<B[i]<<",";

getchar();
return 0;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.