SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdlib.h> | |
2 | #include <stdio.h> | |
3 | //funkcja *dodatnie czyta liczby z klawiatury, dodatnie dodaje do tablicy a z ujemnych liczy srednia | |
4 | void czytaj_n(int*n); | |
5 | void drukuj_tab(int *t, int n); | |
6 | int *dodatnie(int *n, double *srednia_u); | |
7 | int main() | |
8 | { | |
9 | int *tab = NULL, n=0,i; | |
10 | double srednia_u=0; | |
11 | tab = dodatnie(&n, &srednia_u); | |
12 | if (tab == NULL) | |
13 | printf("Nie podano dodatnich wartosci\n"); | |
14 | else | |
15 | { | |
16 | printf("tablica %i dodatnich wartosci:\n", n); | |
17 | drukuj_tab(tab, n); | |
18 | } | |
19 | if (srednia_u == 0) | |
20 | printf("Nie podano ujemnych wartosci\n"); | |
21 | else | |
22 | printf("srednia z ujemnych wartosci wynosi: %lf\n", srednia_u); | |
23 | system("pause"); | |
24 | } | |
25 | void czytaj_n(int*n) | |
26 | { | |
27 | scanf_s("%d", n); | |
28 | ||
29 | } | |
30 | void drukuj_tab(int *t, int n) | |
31 | { | |
32 | int i; | |
33 | for (i = 0; i<n; i++) | |
34 | { | |
35 | printf("[%d]=%d\n", i, *(t + i)); | |
36 | } | |
37 | } | |
38 | int *dodatnie(int *n, double *srednia_u) | |
39 | { | |
40 | int *tab = NULL; | |
41 | int suma_u = 0; | |
42 | int n_u = 0, i = 0; | |
43 | int l; | |
44 | while (1) | |
45 | { | |
46 | czytaj_n(&l); | |
47 | if (l == 0) | |
48 | break; | |
49 | else | |
50 | if (l > 0) | |
51 | { | |
52 | (*n)++; | |
53 | tab = (int*)realloc(tab, *n * sizeof(int)); | |
54 | *(tab + *n - 1) = l; | |
55 | } | |
56 | else | |
57 | { | |
58 | n_u++; | |
59 | suma_u += l; | |
60 | } | |
61 | } | |
62 | if (n_u != 0) | |
63 | *srednia_u = (double)suma_u / n_u; | |
64 | else *srednia_u = 0; | |
65 | return tab; | |
66 | } |