Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
algoprog-etu-22-23
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
Show more breadcrumbs
aliya.myaz
algoprog-etu-22-23
Commits
8278228d
Commit
8278228d
authored
2 years ago
by
Pierre Kunzli
Browse files
Options
Downloads
Patches
Plain Diff
ajout corrige serie 8
parent
05f26c44
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
Programmation/Exercices/serie_08_ex2_corr.c
+104
-0
104 additions, 0 deletions
Programmation/Exercices/serie_08_ex2_corr.c
with
104 additions
and
0 deletions
Programmation/Exercices/serie_08_ex2_corr.c
0 → 100644
+
104
−
0
View file @
8278228d
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
void
swap
(
int
*
a
,
int
*
b
){
int
tmp
=
*
a
;
*
a
=
*
b
;
*
b
=
tmp
;
}
void
fill
(
int
*
tab
,
int
size
){
for
(
int
i
=
0
;
i
<
size
;
i
++
){
tab
[
i
]
=
i
;
}
}
void
permut_random
(
int
*
tab
,
int
size
){
for
(
int
i
=
0
;
i
<
size
;
i
++
){
swap
(
tab
+
i
,
tab
+
(
rand
()
%
size
));
}
}
void
permut_cyclic
(
int
*
tab
,
int
size
,
int
n
){
int
*
new
=
malloc
(
size
*
sizeof
(
int
));
for
(
int
i
=
0
;
i
<
size
;
i
++
){
new
[(
i
+
n
)
%
size
]
=
tab
[
i
];
}
for
(
int
i
=
0
;
i
<
size
;
i
++
){
tab
[
i
]
=
new
[
i
];
}
free
(
new
);
}
void
place_smaller_at_end
(
int
*
tab
,
int
size
){
int
ind_min
=
0
;
for
(
int
i
=
1
;
i
<
size
;
i
++
){
if
(
tab
[
i
]
<
tab
[
ind_min
])
ind_min
=
i
;
}
swap
(
tab
+
ind_min
,
tab
+
size
-
1
);
}
void
print
(
int
*
tab
,
int
size
){
for
(
int
i
=
0
;
i
<
size
;
i
++
){
printf
(
"%d, "
,
tab
[
i
]);
}
}
void
sum
(
int
*
tab
,
int
*
tab_two
,
int
*
tab_three
,
int
size
){
for
(
int
i
=
0
;
i
<
size
;
i
++
){
tab_three
[
i
]
=
tab
[
i
]
+
tab_two
[
i
];
}
}
void
main
(){
int
size
;
srand
(
time
(
NULL
));
printf
(
"Entrez la taille du tableau : "
);
scanf
(
"%d"
,
&
size
);
int
*
tab
=
malloc
(
size
*
sizeof
(
int
));
fill
(
tab
,
size
);
printf
(
"fill : "
);
print
(
tab
,
size
);
printf
(
"
\n
"
);
printf
(
"permut random : "
);
permut_random
(
tab
,
size
);
print
(
tab
,
size
);
printf
(
"
\n
"
);
printf
(
"permut cyclic 3 : "
);
permut_cyclic
(
tab
,
size
,
3
);
print
(
tab
,
size
);
printf
(
"
\n
"
);
printf
(
"place smaller at the end: "
);
place_smaller_at_end
(
tab
,
size
);
print
(
tab
,
size
);
printf
(
"
\n
"
);
int
*
tab_two
=
malloc
(
size
*
sizeof
(
int
));
fill
(
tab_two
,
size
);
permut_random
(
tab_two
,
size
);
permut_cyclic
(
tab_two
,
size
,
3
);
place_smaller_at_end
(
tab_two
,
size
);
printf
(
"tab_two : "
);
print
(
tab_two
,
size
);
printf
(
"
\n
"
);
int
*
tab_three
=
malloc
(
size
*
sizeof
(
int
));
sum
(
tab
,
tab_two
,
tab_three
,
size
);
printf
(
"sum : "
);
print
(
tab_three
,
size
);
printf
(
"
\n
"
);
free
(
tab
);
free
(
tab_two
);
free
(
tab_three
);
}
\ No newline at end of file
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