Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
progseq-controle_continue_2
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-controle_continue_2
Commits
4b810f05
Commit
4b810f05
authored
3 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Add finished ex3
parent
7e6af7a0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ex3/ex3.c
+21
-15
21 additions, 15 deletions
ex3/ex3.c
with
21 additions
and
15 deletions
ex3/ex3.c
+
21
−
15
View file @
4b810f05
...
@@ -6,7 +6,6 @@
...
@@ -6,7 +6,6 @@
#include
<stdio.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdlib.h>
#include
<string.h>
#include
<string.h>
#include
<stdbool.h>
typedef
struct
list_t
{
typedef
struct
list_t
{
int
*
value
;
int
*
value
;
...
@@ -40,17 +39,31 @@ list *push(list *li, int *value, int *index) {
...
@@ -40,17 +39,31 @@ list *push(list *li, int *value, int *index) {
return
new
;
return
new
;
}
}
void
print_pascal
(
list
*
li
,
int
pascal_size
)
{
list
*
current
=
li
;
for
(
int
i
=
0
;
i
<
pascal_size
;
i
++
)
{
if
(
current
!=
NULL
)
{
printf
(
"%d "
,
*
current
->
value
);
current
=
current
->
next
;
}
}
printf
(
"
\n
"
);
}
list
*
create_pascal_triangle
(
list
*
li
,
int
pascal_size
,
int
size_to_compute
,
int
remaining_step
)
{
list
*
create_pascal_triangle
(
list
*
li
,
int
pascal_size
,
int
size_to_compute
,
int
remaining_step
)
{
if
(
remaining_step
==
0
&&
size_to_compute
==
pascal_size
)
{
if
(
remaining_step
==
0
&&
size_to_compute
==
pascal_size
)
{
// End of the recursion
print_pascal
(
li
,
pascal_size
);
return
li
;
return
li
;
}
}
if
(
remaining_step
==
0
&&
size_to_compute
!=
pascal_size
)
{
if
(
remaining_step
==
0
&&
size_to_compute
!=
pascal_size
)
{
create_pascal_triangle
(
li
,
pascal_size
,
size_to_compute
+
1
,
size_to_compute
+
1
);
// Start calculating the next row
return
li
;
return
create_pascal_triangle
(
li
,
pascal_size
,
size_to_compute
+
1
,
size_to_compute
+
1
)
;
}
}
int
index
=
*
li
->
index
+
1
;
int
index
=
*
li
->
index
+
1
;
if
(
remaining_step
==
1
||
remaining_step
==
size_to_compute
)
{
if
(
remaining_step
==
1
||
remaining_step
==
size_to_compute
)
{
// Add the default value and the start and end of each rows
int
default_value
=
1
;
int
default_value
=
1
;
li
=
push
(
li
,
&
default_value
,
&
index
);
li
=
push
(
li
,
&
default_value
,
&
index
);
}
else
{
}
else
{
...
@@ -58,6 +71,7 @@ list *create_pascal_triangle(list *li, int pascal_size, int size_to_compute, int
...
@@ -58,6 +71,7 @@ list *create_pascal_triangle(list *li, int pascal_size, int size_to_compute, int
list
*
current
=
li
;
list
*
current
=
li
;
int
current_index
=
0
;
int
current_index
=
0
;
// Compute the value by searching the numbers at top and top left
while
(
*
current
->
index
>
*
li
->
index
-
size_to_compute
)
{
while
(
*
current
->
index
>
*
li
->
index
-
size_to_compute
)
{
if
(
*
current
->
index
==
*
li
->
index
-
(
size_to_compute
-
1
)
||
if
(
*
current
->
index
==
*
li
->
index
-
(
size_to_compute
-
1
)
||
*
current
->
index
==
*
li
->
index
-
(
size_to_compute
-
2
))
{
*
current
->
index
==
*
li
->
index
-
(
size_to_compute
-
2
))
{
...
@@ -71,18 +85,8 @@ list *create_pascal_triangle(list *li, int pascal_size, int size_to_compute, int
...
@@ -71,18 +85,8 @@ list *create_pascal_triangle(list *li, int pascal_size, int size_to_compute, int
return
create_pascal_triangle
(
li
,
pascal_size
,
size_to_compute
,
remaining_step
-
1
);
return
create_pascal_triangle
(
li
,
pascal_size
,
size_to_compute
,
remaining_step
-
1
);
}
}
void
print_pascal
(
list
*
li
,
int
pascal_size
)
{
list
*
current
=
li
;
for
(
int
i
=
0
;
i
<
pascal_size
;
i
++
)
{
if
(
current
!=
NULL
)
{
printf
(
"%d "
,
*
current
->
value
);
current
=
current
->
next
;
}
}
printf
(
"
\n
"
);
}
int
main
()
{
int
main
()
{
// Initialize the program
int
default_value
=
1
;
int
default_value
=
1
;
int
default_index
=
0
;
int
default_index
=
0
;
...
@@ -90,14 +94,16 @@ int main() {
...
@@ -90,14 +94,16 @@ int main() {
li
->
value
=
&
default_value
;
li
->
value
=
&
default_value
;
li
->
index
=
&
default_index
;
li
->
index
=
&
default_index
;
// Get the pascal size from user
int
pascal_size
;
int
pascal_size
;
scanf
(
"%d"
,
&
pascal_size
);
scanf
(
"%d"
,
&
pascal_size
);
// Create the triangle
if
(
pascal_size
>
1
)
{
if
(
pascal_size
>
1
)
{
li
=
create_pascal_triangle
(
li
,
pascal_size
,
2
,
2
);
li
=
create_pascal_triangle
(
li
,
pascal_size
,
2
,
2
);
}
}
print_pascal
(
li
,
pascal_size
);
// Free the memory and exit the program
destroy
(
li
);
destroy
(
li
);
return
EXIT_SUCCESS
;
return
EXIT_SUCCESS
;
}
}
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