Help with 2d array

Apr 25, 2012 at 6:18pm
Hi,

I'm trying to sort a 2d array in increasing order and stores them in a one dimensional array (from low to high).

16 22 99 4 18
-258 4 101 5 98
105 6 15 2 45
33 88 72 16 3

Apr 25, 2012 at 6:28pm
closed account (S6k9GNh0)
Where's the code?
Apr 25, 2012 at 6:31pm
I really don't know where to start.
I know that I have to make outer loop (row) and inner loop (columns).
But that is all I can figure out.
Apr 25, 2012 at 6:40pm
Hey I3lue! Let's say your 2d matirx is called a with n lines and m columns and you want to store it in a vector called vect:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int a[100][100],n,m,vect[100],v=1;
void copy(int a[][100],int n,int m,int vect[]){
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
vect[v++]=a[i][j];
}
void sort(int vect[]){
int aux;
for(int i=1;i<v-1;i++)
for(int j=i+1;j<v;j++)
if(vect[i]>vect[j]){
aux=vect[i];
vect[i]=vect[j];
vect[j]=aux;
}
}
void sort_2d_array(int a[][100],int n, int m, int vect[]){
copy(a,n,m,vect);
sort(vect);
for(int i=1;i<v;i++)
cout<<vect[i]<<" ";
cout<<endl;
}

This should help! With these 3 functions you can sort any 2d array you want! If you have any questions feel free to ask me. Btw the code here was made simple and is not very fast so if you want a fast code then you can use merge sort as a sorting method!
Apr 25, 2012 at 6:50pm
Thanks uhraurhua,

But I haven't learned vector yet..... T.T
Apr 25, 2012 at 7:50pm
This is what I have I don't know why its not working ......


int main()
{
const int SKIP=-999999;
const int NUMROWS = 4;
const int NUMCOLS = 5;
int unsorted [NUMROWS][NUMCOLS] = { 16, 22, 99, 4, 18,
-258, 4, 101, 5, 98,
105, 6, 15, 2, 45,
33, 88, 72, 16, 3}
int NUMROWS, NUMCOLS;
int sorted[cols]
int max_value,
i,
i_max=0;
int i_s=0;
while (rows<=NUMROWS)
{
max_value = unsorted[0];
cols_max = 0;
i=0;
while (i<=NUMCOLS)
{
if (unsorted[i]> max_value)
{
max_value = unsorted[i];
i_max = i;
}
;
i=i+1;
}
sorted[i]= max_value;
cout<<" "<<sorted[i];
i_s=i_s+1;
}
cout<<endl;
Apr 26, 2012 at 9:29am
"I haven't learned vector yet..... T.T" Well ofcourse you did because "int sorted[cols] " is actually a vector. Vector or array is the same thing. Oh and btw shouldn't you use unsorted[rows][i]? If you have problems with arrays you can use this tutorial http://www.cplusplus.com/doc/tutorial/arrays/ . Also I am sorry if I can't explain myself because my english is quite bad. Well good luck at you program and feel free to ask me!
Apr 26, 2012 at 9:36am
When someone mentions vector it is often assumed to mean std::vector http://www.cplusplus.com/reference/stl/vector/
If you mean array why not just say it?
Apr 26, 2012 at 9:56am
In my language vector or array is the same thing. We call an array vector or "tablou" (it means array). So sorry for generating confusing on this topic. Next time I will say array not vector : )
Apr 26, 2012 at 1:09pm
closed account (S6k9GNh0)
A vector is NOT the same as an array. Hell, there's even std::array. Vector is a container for contiguous memory that changes size of the contained memory. std::array and a normal array is not. When used in other languages, it simply means that the construct used is built to easily change size (although know that changing the size of contiguous memory is generally slow!).
Last edited on Apr 26, 2012 at 1:14pm
Apr 26, 2012 at 6:07pm
I know that but in my language we use vector for both array and std::vector....
Topic archived. No new replies allowed.