-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab6.c
115 lines (94 loc) · 2.45 KB
/
lab6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
int ntrl(int num)
{
if (num > 9 && num < 100)
{
int j;
for (j = 2; j <= sqrt(num); j++)
{
if (num % j == 0)
return (1); // -1 prostoe
}
if (num % j != 0 && num != j)
return (-1);
}
else
return (0);
}
float arithmetick_mean(int *arr, int lot)
{
float m, sum = 0, k = 0;
int j = 0;
for (j = 0; j < lot; j++)
{
sum = sum + arr[j];
k++;
}
m = (sum / k);
if (k)
return (m);
else
return (-1);
}
int max(int *arr, int size)
{
int max = arr[0], j;
for (j = 0; j < size; j++)
if (arr[j] > max)
max = arr[j];
return (max);
}
void remove_last_item(int **array, int *size)
{
(*size)--;
*array = (int *)realloc(*array, *size * sizeof(int));
if (!(*array))
printf("error\n");
}
int main()
{
int *a;
int i, j, k = 0, m, n, max_1, max_2;
float arithmetick_mean_1, arithmetick_mean_2;
srand(time(NULL));
scanf("%d", &n);
printf("# Znach. (-1,0,1) \n");
a = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
{
a[i] = rand() % 100 + 1;
printf("%-2d%6d%10d\n", i, a[i], ntrl(a[i]));
}
max_1 = max(a, n);
arithmetick_mean_1 = arithmetick_mean(a, n);
printf("The maximum value = %d\n", max_1);
printf("The average value = %.1f\n\n\n\n", arithmetick_mean_1);
for (i = 0; i < n; i++)
{
if (ntrl(a[i - 1]) == -1)
i--;
if (ntrl(a[i]) == -1)
if (i != n - 1)
{
for (j = i; j < n - 1; j++)
a[j] = a[j + 1];
remove_last_item(&a, &n);
}
else
remove_last_item(&a, &n);
}
printf("# Znach. (0,1) \n");
for(i = 0; i < n; i++)
printf("%-2d%6d%10d\n", i, a[i], ntrl(a[i]));
max_2 = max(a, n);
arithmetick_mean_2 = arithmetick_mean(a, n);
printf("The maximum value = %d\n", max_2);
printf("The average value has changed = %.1f\n\n\n\n", arithmetick_mean_2);
max_1 == max_2 ? printf("The maximum value remains the same\n") : printf("The maximum value has changed\n");
arithmetick_mean_1 == arithmetick_mean_2 ? printf("The average value remains the same\n") : printf("The average value has changed\n");
system("PAUSE");
free(a);
}