Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
progseq-dynamic_memory_allocation
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
dario.genga
progseq-dynamic_memory_allocation
Commits
5e54fe46
Commit
5e54fe46
authored
3 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Add shuffle method to create a random array
Also added a method to display the content of the array.
parent
4ad07717
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
histo.c
+5
-0
5 additions, 0 deletions
histo.c
unidimensional_array.c
+27
-7
27 additions, 7 deletions
unidimensional_array.c
unidimensional_array.h
+7
-3
7 additions, 3 deletions
unidimensional_array.h
with
39 additions
and
10 deletions
histo.c
+
5
−
0
View file @
5e54fe46
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
unidimensional_array.c
+
27
−
7
View file @
5e54fe46
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
unidimensional_array.h
+
7
−
3
View file @
5e54fe46
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment