Home > Blockchain >  Generate a random amount of numbers and sort them with bubble sort
Generate a random amount of numbers and sort them with bubble sort

Time:08-20

I am new to programming and I am trying to generate an amount of numbers b and then sorting it with bubble sort without using the sort function of list (Because that would be too easy). Since arrays must have a constant value and I cannot put b as the value I'm trying to put the numbers into a list first and then converting the list into a dynamic array to later use bubble sort. How would I convert list1 into an array here?

#include "Log.h"
#include <array>
#include <list>
#include <ctime>
#include <memory>

using namespace std;
void Initlog();
void Log(const char* message);
int main();```



int main()
{
    Initlog();
    int a;
    int b;
    list<int> list1;
    

    cout << "Give how many numbers to generate: "; // Gibt an wie viele Zahlen generiert werden
    cin >> b;


    cout << "Give the biggest possible outcome: "; // Gibt an wie gro? die größte Zahl sein kann
    cin >> a;
    srand(time(0)); // Damit die Zahlen auch halbwegs random sind
    for (int i = 0; i < b; i  ) // Der Loop der solange macht, bis b erreicht ist
    {
        int x = rand()%a; // a steht für die größtmögliche Zahl, x ist was rauskommt
        cout << x << "\n"; // alle entstandenen Zahlen werden angezeigt
        list1.push_back(x); // Zahlen werden in eine Liste links nach rechts gesteckt
    }

    cout << "The list after inserting all elements is : ";
    for (list<int>::iterator i = list1.begin(); i != list1.end(); i  )
        cout << *i << " "; // Inhalt der Liste wird ausgelesen
    cout << endl;


    std::cin.get();
}

CodePudding user response:

Prefer to use std::vector<int>:

std::vector<int> data;  
for (int index = 0; index < b;   index)
{
    int x = rand() % a;
    std::cout << x << "\n";
    data.push_back(x);
}
// The std::vector can be treated as an array.  

To answer your question about converting std::list to an array:

std::list<int>::iterator iter = list1.begin();
std::list<int>::iterator end_iter = list1.end();
int * pointer_to_array = new int [list1.size()];

for (int *p = pointer_to_array;
     iter != end_iter;
       iter)
{
    *p   = *iter;
}

The code above is one of many techniques to convert a std::list to an array. The array must use dynamic memory because the quantity of data is not known at compile time.

Note: prefer std::vector because it handles memory allocation and deallocation for you.

CodePudding user response:

There are two ways, either use a 'genuine' array, or the std::vector container, which works much like an array in terms of access time and takes care of memory management for you.

Assuming C 11, converting list1 into a vector is as easy as:

std::vector<int> data;
data.reserve(list1.size());
for (auto i : list1)
    data.push_back(i);

After that, you can access data with the [] operator just like you would access an array and perform your bubble sort on constant time.

  • Related