Sorting MD Array by column

Hello, this is my first post, I have tried searching for this but I am unable to find a proper resolution.

I am trying to sort my Multi-dimensional array by column. like this:

input:

45 15
37 48
44 50
14 33
8 12
39 22

When sorted it should look like this:

8 12
14 15
37 22
39 33
44 48
45 50

When I run my code now I get this:

0 0
0 0
0 0
0 0
44 48
45 50

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	int r;
	int c;
	int n;
        
	for (c = 0; c < 2; c++)
	{
		for (n = 0; n < 6; n++)
		{
			for (r = 0; r < n; r++)
			{
				if (tickets[n][c] > tickets[n+1][c])
				{
					int temp = tickets[n][c]; 
					tickets[n][c] = tickets[n+1][c];
					tickets[n+1][c] = temp;
				}
			}

		}

	}


Could anyone point me in the right direction on what I am doing wrong?
Last edited on
I think that instead of
1
2
3
4
5
6
if (tickets[n][c] > tickets[n+1][c])
				{
					int temp = tickets[n][c]; 
					tickets[n][c] = tickets[n+1][c];
					tickets[n+1][c] = temp;
				}

you have to write
1
2
3
4
5
6
if (tickets[r][c] > tickets[r+1][c])
				{
					int temp = tickets[r][c]; 
					tickets[r][c] = tickets[r+1][c];
					tickets[r+1][c] = temp;
				}
Thanks but,

I tried that as well but it is still not working correctly.
Last edited on
What is your output right now?
It runs like this now:

0
0
0
0
0
45

I don't get it, this simple bubble sort works on a SD array but I just cant get it to work on a MD array.
These 0's mean that you go outside the boundaries. Can you show part of the code with declaration of tickets?
I was working on one of the projects I saw posted in the forums here. Here is what I have so far.

I have already switched one of the arrays to a vector because it was giving me a lot of problems with deleting the elements. But I would like to try and keep the rest of the arrays.

I know MD arrays are not optimal, but I would like to try and finish it like this. This is all for learning anyways.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <time.h>
#include <new>
#include <vector>

using namespace std;

// array that holds the lotto tickets
int tickets[6][10];

// array to hold wining numbers
int lottowinnum[6];

// var for the powerball 
int powerball;

void lottosort (int *array,int length)
{
	int i,j;

	for (i = 0; i < 6; i++)
	{
		for (j = 0; j < i; j++)
		{

			if (array[i] < array[j])
			{
				int temp=array[i]; 
                array[i]=array[j];
                array[j]=temp;
			}
		}
	}
}



using namespace std; 
int main()
{

	cout << "Welcom to Sam's LOTTO!" << endl << endl;
	cout << "How many tickets would you like, you may choose up to 10." << endl << endl;
	int numplrtick;
	cin >> numplrtick;

	cout << "Please Choose 6 numbers from 1-50." << endl << endl;

	int t = 0;
	for (t=0; t <= (numplrtick - 1); t++)
	{
		int n = 0;
		vector<unsigned int> tempnumchk (6);        // vector to hold the numbers to check.
		for (n=0 ; n<6; n++)
		{
			int plrnumcho;
			bool lottonum2t = false;
			do
			{
			    // Set the bool back to false so that loop can continue.
				lottonum2t = false;
				cout << "please choose your " << n + 1 <<" number." << endl << endl;
				cin >> plrnumcho;

				// check if same number picked twice.
				int lnc;    // Counter for loop

				for (lnc = 0; lnc <= 5; lnc++)
				{
					
					if (tempnumchk[lnc] == plrnumcho) 
					{
						lottonum2t = true;            // Bool to see if the number has been choosen before.
						cout << "Each number must be diffrent, please choose again" << endl << endl;
						break;
					}
				}

				// add the player number to the temp vector
				tempnumchk [n] = plrnumcho;			

			} while ((plrnumcho > 50 || plrnumcho < 1) || lottonum2t);

			tickets[t][n] = plrnumcho;

			//int results = tickets[t][n];
			//cout << results << endl << endl;
		}


	// delete/unassign the temp array.
	tempnumchk.erase (tempnumchk.begin(),tempnumchk.begin()+6);

	if (numplrtick > 1)
	{
		cout << "Now choose the 6 numbers for your " << t + 1 << " ticket" << endl << endl;
	}
	}

	cout << "Great, now its time for the lotto drawing!" << endl << endl;
	srand(time(0));
	int ln;
	for (ln = 0; ln <= 6 ; ln++)
	{
		int temprand = rand() % 50 + 1;
		lottowinnum[ln] = temprand;
	}


	// sort array of winning lotto numbers

	lottosort(lottowinnum,6);

	// sort player tickets

	int r;
	int c;
	int n;


	for (c = 0; c < numplrtick; c++)
	{
		for (n = 0; n < 6; n++)
		{
			for (r = 0; r < n; r++)
			{
				if (tickets[r][c] > tickets[r+1][c])
				{
					int temp = tickets[r][c]; 
					tickets[r][c] = tickets[r+1][c];
					tickets[r+1][c] = temp;
				}
			}

		}

	}



	// print the winning lotto numbers

	int wn;
	for (wn = 0; wn <= 5; wn++)
	{
		int winnum = lottowinnum[wn];
		cout << winnum << endl << endl;
	}

	// print player tickets

	int pt;

	for (pt = 0; pt < numplrtick; pt++)
	{
		int n;

		for (n = 0; n < 6; n++)
		{
			cout << tickets [n][pt] << endl << endl;
		}

	}

	int pause;
	cout << "Press any key to continue...";
	cin >> pause;

	return 0;

}
Anyone have any ideas on this?
Topic archived. No new replies allowed.