Skip to content
Snippets Groups Projects
Commit 5e54fe46 authored by dario.genga's avatar dario.genga
Browse files

Add shuffle method to create a random array

Also added a method to display the content of the array.
parent 4ad07717
Branches
No related tags found
No related merge requests found
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
* Description : Manipulate an unidimensional array with dynamic memory allocation * Description : Manipulate an unidimensional array with dynamic memory allocation
*/ */
#include "unidimensional_array.h" #include "unidimensional_array.h"
#include "time.h"
int main() { int main() {
srand(time(NULL));
// Ask the user the size of the array // Ask the user the size of the array
size_t array_size = ask_array_size(); size_t array_size = ask_array_size();
int *array = malloc(array_size * sizeof(int)); int *array = malloc(array_size * sizeof(int));
...@@ -12,6 +14,9 @@ int main() { ...@@ -12,6 +14,9 @@ int main() {
// Fill the array with random values // Fill the array with random values
fill_array_with_random_values(array, array_size); fill_array_with_random_values(array, array_size);
// Print the array
print_array(array, array_size);
// Find the lowest value in the array // Find the lowest value in the array
int lowest_value = find_lowest_value_in_array(array, array_size); int lowest_value = find_lowest_value_in_array(array, array_size);
printf("Lowest value : %d\n", lowest_value); printf("Lowest value : %d\n", lowest_value);
......
...@@ -14,19 +14,39 @@ size_t ask_array_size() { ...@@ -14,19 +14,39 @@ size_t ask_array_size() {
return array_size; return array_size;
} }
void fill_array_with_random_values(int array[], size_t array_size) { void shuffle_array(int* array, size_t array_size) {
size_t max_value = array_size - 1; for (size_t i = 0; i < array_size; i++)
{
int index1 = rand() % (int)array_size;
int index2 = rand() % (int)array_size;
swap(&array[index1], &array[index2]);
}
}
void fill_array_with_random_values(int* array, size_t array_size) {
// Fill the array with all values from 0 to its size - 1
for (size_t i = 0; i < array_size; i++)
{
array[i] = i;
}
srand(time(0)); // Then shuffle the array
shuffle_array(array, array_size);
}
void print_array(int* array, size_t array_size) {
printf("Array :\n[");
for (size_t i = 0; i < array_size; i++) for (size_t i = 0; i < array_size; i++)
{ {
int random_value = (rand() % (max_value + 1)); printf("%d", array[i]);
array[i] = random_value; if (i + 1 < array_size) {
printf(" , ");
}
} }
printf("]\n");
} }
int find_lowest_value_in_array(int array[], size_t array_size) { int find_lowest_value_in_array(int* array, size_t array_size) {
int lowest_value; int lowest_value;
for (size_t i = 0; i < array_size; i++) { for (size_t i = 0; i < array_size; i++) {
...@@ -39,7 +59,7 @@ int find_lowest_value_in_array(int array[], size_t array_size) { ...@@ -39,7 +59,7 @@ int find_lowest_value_in_array(int array[], size_t array_size) {
return lowest_value; return lowest_value;
} }
int find_index_highest_value_in_array(int array[], size_t array_size) { int find_index_highest_value_in_array(int* array, size_t array_size) {
int highest_value; int highest_value;
size_t index_highest_value = 0; size_t index_highest_value = 0;
......
...@@ -9,11 +9,15 @@ ...@@ -9,11 +9,15 @@
size_t ask_array_size(); size_t ask_array_size();
void fill_array_with_random_values(int array[], size_t array_size); void fill_array_with_random_values(int* array, size_t array_size);
int find_lowest_value_in_array(int array[], size_t array_size); int find_lowest_value_in_array(int* array, size_t array_size);
int find_index_highest_value_in_array(int array[], size_t array_size); int find_index_highest_value_in_array(int* array, size_t array_size);
void shuffle_array(int* array, size_t array_size);
void print_array(int* array, size_t array_size);
void swap(int *x, int *y); void swap(int *x, int *y);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment