generated from Me-Phew/programowanie-zaawansowane-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add random array generation, shuffle and reverse
- Loading branch information
Showing
7 changed files
with
136 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <vector> | ||
#include <random> | ||
#include <algorithm> | ||
#include <iostream> | ||
|
||
void displayArray(std::vector<int> arr) { | ||
std::cout << "["; | ||
|
||
for (auto it = arr.begin(); it != arr.end(); ++it) { | ||
std::cout << *it; | ||
|
||
if (it != arr.end() - 1) { | ||
std::cout << ", "; | ||
} | ||
} | ||
|
||
std::cout << "]\n"; | ||
} | ||
|
||
void randomlyShuffleArray(std::vector<int>& arr) { | ||
std::random_device randomDevice; | ||
std::mt19937 generator(randomDevice()); | ||
|
||
std::shuffle(arr.begin(), arr.end(), generator); | ||
} | ||
|
||
void reverseArray(std::vector<int>& arr) { | ||
for (int i = 0; i < arr.size() / 2; i++) { | ||
//std::swap(arr[i], arr[arrSize - i - 1]); | ||
int temp = arr[i]; | ||
|
||
int swappedElementIndex = arr.size() - i - 1; | ||
arr[i] = arr[swappedElementIndex]; | ||
arr[swappedElementIndex] = temp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef ARRAY_UTILS_HPP | ||
#define ARRAY_UTILS_HPP | ||
|
||
#include <vector> | ||
|
||
void displayArray(std::vector<int>); | ||
|
||
void randomlyShuffleArray(std::vector<int>&); | ||
|
||
void reverseArray(std::vector<int>&); | ||
|
||
#endif /* ARRAY_UTILS_HPP */ |
20 changes: 20 additions & 0 deletions
20
src/utils/random_number_generator/random_number_generator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include<cstdlib> | ||
#include<ctime> | ||
|
||
#include <random_number_generator/random_number_generator.hpp> | ||
|
||
bool RandomNumberGenerator::isSeeded = false; | ||
|
||
void RandomNumberGenerator::seed() { | ||
if (!isSeeded) { | ||
std::srand(std::time(NULL)); | ||
isSeeded = true; | ||
} | ||
} | ||
|
||
int RandomNumberGenerator::getRandomNumber(int min, int max) { | ||
RandomNumberGenerator::seed(); | ||
|
||
int range = max - min; | ||
return std::rand() % range; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/utils/random_number_generator/random_number_generator.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef RANDOM_NUMBER_GENERATOR_HPP | ||
#define RANDOM_NUMBER_GENERATOR_HPP | ||
|
||
class RandomNumberGenerator { | ||
private: | ||
static bool isSeeded; | ||
|
||
/// @brief Inicjalizuje generator liczb losowych. | ||
static void seed(); | ||
|
||
public: | ||
static int getRandomNumber(int min, int max); | ||
}; | ||
|
||
#endif /* RANDOM_NUMBER_GENERATOR_HPP */ |