jazykc
2013.3
upoljazykc
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Macros
Pages
misc
modus.c
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdarg.h>
4
5
//compare funkce pro qsort:
6
static
int
int_compare (
void
*a,
void
*b) {
return
*(
int
*)a - *(
int
*)b; }
7
8
int
modus (
int
pocet, ...) {
9
va_list args;
10
11
int
*cisla;
12
int
i;
13
int
modus, nmodus;
//cislo ktere je modus a pocet jeho vyskytu
14
int
n, predchozi;
//delka sekvence, predchozi cislo
15
16
cisla = malloc(
sizeof
(
int
) * pocet);
17
18
//kopie args ... do cisla[pocet]
19
va_start(args, pocet);
20
for
(i=0; i<pocet; i++) {
21
cisla[i] = va_arg(args,
int
);
22
}
23
va_end(args);
24
25
qsort(cisla, pocet,
sizeof
(
int
), int_compare);
26
//for (i=0; i<pocet; i++) { printf("%d,",cisla[i]); } printf("\n");//kontr vypis
27
28
//delka nejdelsi sekvence stejnych cisel je modus:
29
modus = predchozi = cisla[0];
//cisla[0] si predzpracujeme, zbytek for(i=1; ... !!!
30
nmodus = n = 1;
31
for
(i=1; i<pocet; i++) {
32
if
(predchozi != cisla[i]) {
//zmena cisla?
33
//je predchozi novy modus?
34
if
(n>nmodus) {
35
nmodus = n;
36
modus = predchozi;
37
}
38
predchozi = cisla[i];
39
n = 1;
40
}
else
{
//sekvence pokracuje, zapocteme
41
n++;
42
}
43
}
44
45
free(cisla);
//jen pomocne!!!
46
return
modus;
47
}
48
49
int
main(
void
) {
50
printf(
"modus je %d\n"
, modus(5, 1,2,2,3,4));
51
52
return
0;
53
}
Generated on Sat Dec 7 2013 22:57:26 for jazykc by
1.8.5