Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ALP4-Tutorials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mactavish96
ALP4-Tutorials
Commits
708b94ac
Commit
708b94ac
authored
2 years ago
by
Mactavish
Browse files
Options
Downloads
Patches
Plain Diff
add solutions to exercises
parent
8b23c414
Branches
tutorial-2
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
exercises/higher-oder-func/higher_order_func.c
+48
-3
48 additions, 3 deletions
exercises/higher-oder-func/higher_order_func.c
exercises/process/mysystem.c
+13
-3
13 additions, 3 deletions
exercises/process/mysystem.c
with
61 additions
and
6 deletions
exercises/higher-oder-func/higher_order_func.c
+
48
−
3
View file @
708b94ac
...
...
@@ -3,15 +3,60 @@
#include
<string.h>
void
*
map
(
void
*
array
,
size_t
n
,
size_t
size
,
void
(
*
fn
)(
void
*
elem
))
{
// TODO: implement me!
if
(
n
==
0
)
{
return
NULL
;
}
void
*
result
=
malloc
(
n
*
size
);
// copy original array to result byte by byte
memcpy
((
char
*
)
result
,
(
char
*
)
array
,
n
*
size
);
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
// cast it to char* to make it byte addressable
fn
((
char
*
)
result
+
i
*
size
);
}
return
result
;
}
void
*
filter
(
void
*
array
,
size_t
n
,
size_t
size
,
size_t
*
new_size
,
bool
(
*
pred
)(
void
*
elem
))
{
// TODO: implement me!
*
new_size
=
0
;
size_t
count
=
0
;
if
(
n
==
0
)
{
return
NULL
;
}
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
void
*
element
=
(
char
*
)
array
+
i
*
size
;
if
(
pred
(
element
))
{
count
++
;
}
}
if
(
count
==
0
)
{
return
NULL
;
}
void
*
result
=
malloc
(
count
*
size
);
count
=
0
;
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
void
*
element
=
(
char
*
)
array
+
i
*
size
;
if
(
pred
(
element
))
{
void
*
result_element
=
(
char
*
)
result
+
count
*
size
;
memcpy
(
result_element
,
element
,
size
);
count
++
;
}
}
*
new_size
=
count
;
return
result
;
}
void
*
reduce
(
void
*
array
,
size_t
n
,
size_t
size
,
void
*
initial_value
,
void
*
(
*
reducer
)(
void
*
acc
,
void
*
elem
))
{
// TODO: implement me!
if
(
n
==
0
)
{
return
initial_value
;
}
void
*
accumulator
=
initial_value
;
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
void
*
element
=
(
char
*
)
array
+
i
*
size
;
accumulator
=
reducer
(
accumulator
,
element
);
}
return
accumulator
;
}
This diff is collapsed.
Click to expand it.
exercises/process/mysystem.c
+
13
−
3
View file @
708b94ac
...
...
@@ -7,13 +7,13 @@
* a given shell command.
*/
#include
<errno.h>
#include
<stdbool.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<sys/wait.h>
#include
<unistd.h>
#include
<errno.h>
// The max-length shell command the user can enter
static
const
size_t
COMMAND_MAX_LEN
=
2048
;
...
...
@@ -34,7 +34,17 @@ static const size_t COMMAND_MAX_LEN = 2048;
*/
// Uncomment when implemented
static
int
mysystem
(
char
*
command
)
{
// TODO: implement this function
pid_t
pidOrZero
=
fork
();
if
(
pidOrZero
==
0
)
{
char
*
arguments
[]
=
{
"/bin/sh"
,
"-c"
,
command
,
NULL
};
execvp
(
arguments
[
0
],
arguments
);
// If the child gets here, there was an error
exit
(
errno
);
}
// If we are the parent, wait for the child
int
status
;
waitpid
(
pidOrZero
,
&
status
,
0
);
return
WIFEXITED
(
status
)
?
WEXITSTATUS
(
status
)
:
-
WTERMSIG
(
status
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
...
...
@@ -52,7 +62,7 @@ int main(int argc, char *argv[]) {
command
[
strlen
(
command
)
-
1
]
=
'\0'
;
// TODO: use mysystem instead of system
int
commandReturnCode
=
system
(
command
);
int
commandReturnCode
=
my
system
(
command
);
printf
(
"return code = %d
\n
"
,
commandReturnCode
);
}
...
...
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