sorting numbers in ascending order

Nov 8, 2013 at 10:45am
Hello
So I need some help with sorting numbers in ascending order.
I'm making a program which generates lottery numbers, but I don't know how to sort the in ascending order after generating them.

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
  #include <iostream>
#include <time.h>
using namespace std;
int main()
{
	int antal;
	cout << "How many rows? (1-12)" << endl;
	cin >> antal;
	
	srand( time(0) );
	while(antal >= 1 && antal <= 12)
	{

	for (int rad=antal ; rad != 0 ; rad--)
	{
		for( int ramd=0 ; ramd < 7 ; ramd++){
	int slump;			
	slump		= rand();		
	slump		= slump % 35;	// 0 - 35
	slump++;					// 1 - 35
	
	cout << slump << "  ";
		}
		cout << endl;
	}
	
	break;
	}
	system("PAUSE");
    return 0;
}
Nov 8, 2013 at 10:49am
store ur numbers in an array, and google bubble sort :)
Nov 8, 2013 at 5:35pm
And here you can read about simple variant of the bubble sort:

http://codeabbey.com/index/task_view/bubble-sort

And also get test data and check your program results.
Nov 8, 2013 at 6:00pm
you could also use a recursive function.

There is a problem with your question though: define ascending order. A list can be visualized many different ways, and it's generally a good thing to just think of it as an abstract object.

ex:

1 2 3 4 5 6 7 8
could also be represented as
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8


or even in reverse! It depends on how you want to program the 'read' functions, depending on what you're using it for.

I assume you mean from least to greatest, though.

I'm not giving you the code, but I will write up some psuedo for you. I, personally, templated this.

void sort stuff(your list&)
{
    -if the lists size is less than two, return
    -go through the list and store the smallest value in a temp variable
    -resize the list to 1 less than it's current size (you can swap the smallest value with the last to prevent data loss)
    -call this function
    -add the temp variable you stored the smallest value in, back into the list.
}


if it's in the wrong order (fro greatest to least instead of least to greatest) then simply flip the sign to take out the largest value.
Last edited on Nov 8, 2013 at 6:02pm
Topic archived. No new replies allowed.