Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
df971358
Commit
df971358
authored
Mar 01, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Now using array instead of structs.
parent
1dc110f0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
75 deletions
+13
-75
modsim/ass2/q7.c
modsim/ass2/q7.c
+13
-75
No files found.
modsim/ass2/q7.c
View file @
df971358
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <assert.h>
typedef
struct
rabbit
{
void
sequence
(
int
n
,
int
max_age
)
{
unsigned
short
age
;
int
i
,
mature
=
0
,
new
[
n
],
died
=
0
;
struct
rabbit
*
prev
,
*
next
;
}
rabbit
;
rabbit
*
rabbits
=
NULL
;
new
[
0
]
=
1
;
int
rabbit_count
=
0
;
rabbit
*
rabbit_born
()
{
rabbit
*
r
=
(
rabbit
*
)
malloc
(
sizeof
(
rabbit
));
if
(
(
r
->
next
=
rabbits
)
)
rabbits
->
prev
=
r
;
r
->
age
=
0
;
r
->
prev
=
NULL
;
rabbits
=
r
;
rabbit_count
++
;
return
r
;
}
void
rabbit_die
(
rabbit
**
ptr
)
{
rabbit
*
r
=
*
ptr
;
if
(
r
->
prev
!=
NULL
)
r
->
prev
->
next
=
r
->
next
;
if
(
r
->
next
!=
NULL
)
r
->
next
->
prev
=
r
->
prev
;
if
(
r
==
rabbits
)
rabbits
=
r
->
next
;
else
*
ptr
=
NULL
;
rabbit_count
--
;
free
(
r
);
}
void
sequence
(
int
generations
,
int
max_age
)
{
int
i
;
rabbit
*
r
,
*
next
=
NULL
;
rabbits
=
rabbit_born
();
printf
(
"1"
);
printf
(
"1"
);
for
(
i
=
1
;
i
<
generations
;
i
++
)
{
for
(
i
=
1
;
i
<
n
;
i
++
)
{
for
(
r
=
rabbits
;
r
!=
NULL
;
r
=
next
)
{
died
=
i
<
max_age
?
0
:
new
[
i
-
max_age
];
next
=
r
->
next
;
new
[
i
]
=
mature
;
mature
=
mature
+
new
[
i
-
1
]
-
died
;
if
(
r
->
age
++
)
{
rabbit_born
();
if
(
r
->
age
==
max_age
)
printf
(
" %d"
,
mature
+
new
[
i
]);
rabbit_die
(
&
r
);
}
}
printf
(
" %d"
,
rabbit_count
);
}
}
puts
(
""
);
puts
(
""
);
while
(
rabbits
)
rabbit_die
(
&
rabbits
);
}
void
sequence_old
(
int
n
)
{
int
i
,
new
=
1
,
new_new
,
mature
=
0
,
died
=
0
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
printf
(
"%d + %d = %d
\n
"
,
mature
,
new
,
new
+
mature
);
new_new
=
mature
;
mature
=
mature
+
new
-
died
;
new
=
new_new
;
}
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
main
(
int
argc
,
char
*
argv
[])
{
...
@@ -90,12 +26,14 @@ int main(int argc, char *argv[]) {
...
@@ -90,12 +26,14 @@ int main(int argc, char *argv[]) {
return
1
;
return
1
;
}
}
generations
=
atoi
(
argv
[
1
]);
max_age
=
generations
=
atoi
(
argv
[
1
]);
if
(
argc
>
2
&&
(
max_age
=
atoi
(
argv
[
2
]))
==
1
)
{
if
(
argc
>
2
&&
(
max_age
=
atoi
(
argv
[
2
]))
<
2
)
{
puts
(
"MAX_AGE should be at least 2
(or 0 for infinite)
."
);
puts
(
"MAX_AGE should be at least 2."
);
return
1
;
return
1
;
}
}
sequence
(
generations
,
max_age
);
sequence
(
generations
,
max_age
);
return
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment