-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtp_lab02_ex1.c
185 lines (156 loc) · 5.12 KB
/
tp_lab02_ex1.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void afisare_matrice(int **matrice, int SIZE){
printf("\n");
for (int i = 0; i < SIZE; i++){
for (int j = 0; j < SIZE; j++)
printf("%d ", matrice[i][j]);
printf("\n");
}
printf("\n");
}
int **citire_matrice(int **matrice, int SIZE){
if ((matrice = (int **) malloc (SIZE * sizeof(int*))) == NULL){
fprintf(stderr, "Eroare la dimensiunea matricei.\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < SIZE; i++)
if ((matrice[i] = (int *) malloc (SIZE * sizeof(int))) == NULL){
fprintf(stderr, "Eroare la dimensiunea matricei pe linii.\n");
exit(EXIT_FAILURE);
}
srand(time(NULL));
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
matrice[i][j] = rand () % 10;
return matrice;
}
void suma_cea_mai_mica(int **matrice, int SIZE){
int maxim = 0, suma = 0;
for (int i = 0; i < SIZE; i++){
maxim = matrice[i][0];
suma = 0;
for (int j = 0; j < SIZE; j++){
if (maxim < matrice[i][j])
maxim = matrice[i][j];
suma += matrice[i][j];
}
printf("Suma minima pe linia %d este: %d\n", i, suma - maxim);
}
printf("\n");
}
int este_prim(int element){
for (int i = 2; i <= element / 2; i++)
if (element % i == 0)
return 0;
return 1;
}
void afla_nr_prime_pe_pozitii_pare (int **matrice, int SIZE){
int count = 0;
for (int i = 0; i < SIZE; i+=2)
for (int j = 0; j < SIZE; j+=2)
if (este_prim(matrice[i][j]) == 1){
count++;
printf("%d : matrice[%d][%d]\n", matrice[i][j], i, j);
}
printf("\n");
printf("Numere prime pe pozitii pare: %d\n", count);
}
int **permuta_coloana_la_stanga(int **matrice, int SIZE){
int col1[SIZE];
for (int i = 0; i < SIZE; i++)
col1[i] = matrice[i][0];
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE - 1; j++)
matrice[i][j] = matrice[i][j+1];
for (int i = 0; i < SIZE; i++)
matrice[i][SIZE-1] = col1[i];
return matrice;
}
int **interschimba_min1c_maxnc(int **matrice, int SIZE){
int minim = matrice[0][0], maxim = matrice[0][SIZE-1];
for (int i = 0; i < SIZE; i++){
if (minim > matrice[i][0])
minim = matrice[i][0];
if (maxim < matrice[i][SIZE-1])
maxim = matrice[i][SIZE-1];
}
printf("Minim: %d\nMaxim: %d\n", minim, maxim);
for (int i = 0; i < SIZE; i++){
if(matrice[i][SIZE-1] == maxim)
matrice[i][SIZE-1] = minim;
if (matrice[i][0] == minim)
matrice[i][0] = maxim;
}
return matrice;
}
void afla_nr_linii_elem_egale(int **matrice, int SIZE){
int count = 0, check = 0;
for (int i = 0; i < SIZE; i++){
check = 1;
for (int j = 0; j < SIZE; j++)
if (matrice[i][j] != matrice[i][0]){
check = 0;
break;
}
if (check == 1)
count++;
}
printf("\nNumar de linii cu toate elementele egale: %d\n", count);
}
int main (void){
int SIZE = 0, optiune = 0;
printf("Dimensiunea matricei: ");
if (scanf("%d", &SIZE) != 1){
fprintf(stderr, "Eroare la introducerea dimensiunii matricei.\n");
exit(EXIT_FAILURE);
}
int **matrice = NULL;
matrice = citire_matrice(matrice, SIZE);
afisare_matrice(matrice, SIZE);
do {
printf("\n");
printf("== ALEGE OPTIUNE ==\n");
printf("1. Determina suma pe linii eliminand maximul\n");
printf("2. Afla numerele prime de pe pozitii pare\n");
printf("3. Permuta matricea cu o coloana la stanga\n");
printf("4. Interschimba minimul de pe prima coloana cu maximul de pe ultima coloana\n");
printf("5. Afla numarul de linii cu toate elementele egale\n");
printf("6. Opreste procesul\n\n");
printf("Alegerea ta: ");
if (scanf("%d", &optiune) != 1){
fprintf(stderr, "Eroare la introducerea optiunii.\n");
exit(EXIT_FAILURE);
}
switch (optiune) {
case 1:
suma_cea_mai_mica(matrice, SIZE);
break;
case 2:
afla_nr_prime_pe_pozitii_pare(matrice, SIZE);
break;
case 3:
matrice = permuta_coloana_la_stanga(matrice, SIZE);
afisare_matrice(matrice, SIZE);
break;
case 4:
matrice = interschimba_min1c_maxnc(matrice, SIZE);
afisare_matrice(matrice, SIZE);
break;
case 5:
afla_nr_linii_elem_egale(matrice, SIZE);
break;
case 6:
printf("Operatiune finalizata.\n");
break;
default:
printf("Valoare nedefinita pentru optiune.");
break;
}
} while (optiune != 6);
for (int i = 0; i < SIZE; i++)
free(matrice[i]);
free(matrice);
return 0;
}