Sorting random numbers?

So I've written this code which lets you decide how many rows you want with seven numbers in it. But I want them sorted and the same number can't appear again on the same row. I know about bubbleSort but I can't seem to get it work... Can someone explain step by step so I can understand? Here's the code for the rows:

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
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;

int main()

{
setlocale(LC_ALL, "");
int i;
int avsluta = 1;
int rader;
string svar;
	while(avsluta==1)
		{
		cout << "Välkommen till lotteriet!" << endl;	
		cout << "Hur många rader har du betalat för? ";
		cin >> rader;
		srand (time (NULL) );
		if(rader < 13)
		{
			cout << "Okej! Här är din spelbricka!" << endl;
		while(rader > 0)
		{
		i = 1;
		while(i <= 7)
		{
			cout << (rand()%35) + 1 << " ";
	        	i++;
                }
		cout << endl;
		rader--;
                }
		}
		else
	        {
	        cout << "Endast 1-12 rader är tillåtna!" << endl;
	        }
		cout << "Vill du fortsätta? (Ja/Nej) ";
		cin >> svar;
		if(svar == "JA" || svar == "ja" || svar == "jA" || svar == "Ja")
	        {
	        cout << "Okej!" << endl;
		}
		else if(svar != "NEJ" || svar == "NEj" || svar == "Nej" || svar == "nej" || svar == "nEJ" || svar == "nEJ" || svar == "nej")
		{
		avsluta = 0;
		}

	        }
	return 0;
}
Last edited on
Bump, anyone?
closed account (o1vk4iN6)
Well firstly, why post in english then code in another language, makes it harder to read as variable and comments are meaningless and you did not use [code] tags.

Somethings I can see:

1. Make a new function and name it accordingly to what you want (bubble sort in this case).
2. Convert your string to a standard case (all lower case for example) then compare if it the desired word, no need for 7 comparisons in your if statement.
3. Use code tags.
@xerzi

Fixed the code tag. Didn't know that one because I'm new here.
Why I wrote it in another language? Because I'm from another country and I did a copy paste. Editing the variables and text takes its time. What do you mean by string?

Why not make an array and make it compare the seven numbers in the array?

Does that work?
Last edited on
You may translate your string & variable names in your whole source then replace the old string with the translated string.
Where are you from? :)

Last edited on
@Jackson Marie

Okay. I'll try that. I'm from Sweden. Why?
1
2
3
4
5
6
7
8
9
10
cout << "Vill du fortsätta? (Ja/Nej) ";
		cin >> svar;
		if(svar == "JA" || svar == "ja" || svar == "jA" || svar == "Ja")
	        {
	        cout << "Okej!" << endl;
		}
		else if(svar != "NEJ" || svar == "NEj" || svar == "Nej" || svar == "nej" || svar == "nEJ" || svar == "nEJ" || svar == "nej")
		{
		avsluta = 0;
		}

"Something I can see" is you're using too many comparisons in your if condition.
Try to apply the tip :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << "Vill du fortsätta? (Ja/Nej) ";

		char temp[256];
		cin >> temp;
		strupr(temp); //Make upper
	svar = temp;

		if(svar == "JA")
		{
		cout << "Okej!" << endl;
		}
		else if (svar == "NEJ")
		{
		avsluta = 0;
		}

I used "Make string upper" (strupr) function.
Last edited on
Can someone explain step by step so I can understand?
It's easy, really.

Look at this:

http://en.wikipedia.org/wiki/Bubble_sort

you need 2 nested loops. both iterate over the size of the array. The inner loop swaps the values of the array if necessary (a[j] > a[j + 1]) and set a bool variable (is_swapped or whatever) to true. the outer loop sets is_swapped before the inner loop to false. After the inner loop is done and is_swapped remains false you may break the outer loop (the sort is done)
@coder777

Yeah I read that yesterday. I forgot to create an array. That's why it didn't work...
Topic archived. No new replies allowed.