sorting array

Hi!
The goal of this program is to let the user enter a number, then create as many random numbers as the user entered (the random numbers must be between 1 - 1 000 000). And then the program has to sort those random numbers into an ascending order. Here's what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    int main()
    {
    const int MAX_SIZE = 100;
    int numbers[MAX_SIZE];
    int numElements;
    cout << "Enter number: \n";
    cin >> numElements;
    srand(static_cast<unsigned int>(time(0)));
    int array[numElements];
    cout << "Random numbers: \n";
    for(int i = 0; i < numElements; i++){
            array[i] = (rand()%1000000) + 1;
            cout << array[i] << ",";
            if(i == numElements){
                 cout << "i";
                 }
                 }
                 system("pause");
                 return 0;
                 }

I have no idea how to sort it into an ascending order. Any help?
Explain line 12? You already have "numbers" array declared on line 7. Also check to make sure "numElements" is less then or equal to "MAX_SIZE"
I don't know why I did that. Anyway, I removed the unnecessary line.
The code looks like this now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    int main()
    {
    const int MAX_SIZE = 100;
    int numbers[MAX_SIZE];
    int numElements;
    cout << "Enter number: \n";
    cin >> numElements;
    numElements <= MAX_SIZE;
    srand(static_cast<unsigned int>(time(0)));
    cout << "Random numbers: \n";
    for(int i = 0; i < numElements; i++){
            numbers[i] = (rand()%1000000) + 1;
            cout << numbers[i] << ",";
            if(i == numElements){
                 cout << "i";
                 }
                 }
                 system("pause");
                 return 0;
                 }
So do you know any sorting algorithms? Selection sort is easy to implement and understand, many people say the same for bubble sort.
Yeah, I've heard of bubble sort, but I don't really know how to implement it here. I basically know the algorithm, but don't exactly know where and how to put it.
Last edited on
Okay, my code is like this:
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
#include <stdlib.h>
    #include <iostream>
    using namespace std;
    int main()
    {
    const int MAX_SIZE = 100;
    int numbers[MAX_SIZE];
    int numElements;
    cout << "Enter number: \n";
    cin >> numElements;
    numElements <= MAX_SIZE;
    srand(static_cast<unsigned int>(time(0)));
    cout << "Random numbers: \n";
    for(int i = 0; i < numElements; i++){
            numbers[i] = (rand()%1000000) + 1;
            cout << numbers[i] << ",";
            if(i == numElements){
                 cout << "i";
                 }
                 }
    int exchanges;
    int temp;
    cout << "\nSorted numbers: \n";
    for(int i = 0; i < numElements; i++){
            if(numbers[i] > numbers[i + 1]){
                          temp = numbers[i];
                          numbers[i] = numbers[i +1];
                          numbers[i + 1] = temp;
                          exchanges++;
                          cout << temp << ",";
                          }
                          }
           
                                       
                 
                 system("pause");
                 return 0;
                 }

Can anyone help me?
Last edited on
Anybody?
there is a sort function in the algorithms library.
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
#include <stdlib.h>
#include <iostream>
#include <algorithm> 
#include <vector> 
using namespace std;
int main()
{
    //needed for sort function 
    vector<int> elements; 
    int numElements;
    
    cout << "Enter number: \n";
    cin >> numElements;
    srand(static_cast<unsigned int>(time(0)));
    
    cout << "Random numbers: \n"; 
    
    for(int i = 0; i < numElements; i++){
        
        elements.push_back((rand()%1000000) + 1);
        
        cout << elements[i] << ",";
        
        if(i == numElements){
        
            cout << "i";
        }
    }
    cout << '\n' << "the vector sorted is: "; 
    
    //sorts the vector
    sort(elements.begin(), elements.end()); 
    //output 
    for(int i = 0; i < elements.size(); i++){ cout << elements[i] << ",";} 
    cin.ignore(); // no system commands here 
    return 0;
}

there is also bubble sort which is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void floatingSort(int *array)
{//length is the length of the array you want sorted
    int i,j;
    for(i=0;i<length;i++)
    {
        for(j=0;j<length;j++)
        {
            if(array[i]>array[j])
            {
                int temp=array[i]; //swaps the value 
                array[i]=array[j];
                array[j]=temp;
            }
            
        }
        
    }
    
}
Last edited on
Well I tried bubble sorting (since we're not allowed to use the sort function) but I'm not good at this and it didn't work out.
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
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    int main()
    {
    const int MAX_SIZE = 100;
    int numbers[MAX_SIZE];
    int numElements;
    cout << "Enter number: \n";
    cin >> numElements;
    numElements <= MAX_SIZE;
    srand(static_cast<unsigned int>(time(0)));
    cout << "Random numbers: \n";
    for(int i = 0; i < numElements; i++){
            numbers[i] = (rand()%1000000) + 1;
            cout << numbers[i] << ",";
            if(i == numElements){
                 cout << "i";
                 }
                 }
    int exchanges;
    int temp;
    cout << "\nSorted numbers: \n";
    for(int i = 0; i < numElements; i++){
            for(int j = 0; j < numElements; j++)
            {
                    if(numbers[i] > numbers[j])
                    {
                                  temp = numbers[i];
                                  numbers[i] = numbers[j];
                                  numbers[j] = temp;
                                  cout << numbers[i] << ",";
                                  }
                                  }
                                  }
                                       
                 system("pause");
                 return 0;
                 }
Last edited on
Wait, I got the code a little bit better. It now shows the numbers in an ascending order, but also adds 1 random number at the end. How to get rid of it?
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
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    int main()
    {
    const int MAX_SIZE = 100;
    int numbers[MAX_SIZE];
    int numElements;
    cout << "Enter number: \n";
    cin >> numElements;
    numElements <= MAX_SIZE;
    srand(static_cast<unsigned int>(time(0)));
    cout << "Random numbers: \n";
    for(int i = 0; i < numElements; i++){
            numbers[i] = (rand()%1000000) + 1;
            cout << numbers[i] << ",";
            if(i == numElements){
                 cout << "i";
                 }
                 }
    int exchanges;
    int temp;
    int j;
    cout << "\nSorted numbers: \n";
    for(int i = 0; i < numElements; i++){
            for(int j = 0; j < numElements; j++)
            {
                    if(numbers[j] > numbers[j + 1])
                    {
                                  temp = numbers[j];
                                  numbers[j] = numbers[j + 1];
                                  numbers[j + 1] = temp;
                                  
                                  }
                                  }
                                  }
                for(int i = 0; i <= numElements; i++){
                      cout << numbers[i] << ",";
                      }                       
                 system("pause");
                 return 0;
                 }

And also how can I get a random number between 1 and 1 000 000? Right now it seems it wont get higher than 30 000 or so..
I'm really, really thankful for all the help!
The rand() function generates a random number from 0 to RAND_MAX, which is only 32767 for me.
So if you want a random number from 1 to 1 million, you're going to have to use a different pseudo-random number generator....

Also, what's the point of this line?
numElements <= MAX_SIZE;
I think you meant to check that numElements is less than or equal to MAX_SIZE, and do something (e.g. print an error or something) if it's not, but right now that line of code does absolutely nothing.

Anyways:
for(int i = 0; i <= numElements; i++){ // Should be < , not <=
that's your problem. (I think -- I haven't tested this myself, but I'm pretty sure about it)
Thank you so much! Now the program works fine :) And for the random number problem I simbly did this:
 
numbers[i] = (rand()%1000) * (rand()%1000) + 1;
Topic archived. No new replies allowed.