0% found this document useful (0 votes)
57 views16 pages

Baze Program Are

The document contains C++ code snippets for solving various problems related to arrays, matrices, strings, and other data structures. The code snippets include functions to: 1) Sum the elements on the two diagonals of a square matrix. 2) Sum the prime elements in a matrix. 3) Transpose a square matrix. 4) Sum the maximum elements on each row of a matrix.

Uploaded by

blueber07
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views16 pages

Baze Program Are

The document contains C++ code snippets for solving various problems related to arrays, matrices, strings, and other data structures. The code snippets include functions to: 1) Sum the elements on the two diagonals of a square matrix. 2) Sum the prime elements in a matrix. 3) Transpose a square matrix. 4) Sum the maximum elements on each row of a matrix.

Uploaded by

blueber07
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

/* Suma elementelor de pe cele 2 diagonale unei matrici patratice */ #include <stdio.

h> using namespace std; int main() { int i, j, n; int x[50][50]; printf("Introduceti dimensiunea matricii: "); scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("Introduceti elementul de pe pozitia (%d,%d): ", i + 1, j + 1); scanf("%d", &x[i][j]); } } int s1 = 0, s2 = 0; for (i = 0; i < n; i++) { s1 += x[i][i]; s2 += x[i][n - i - 1]; } printf("Suma de pe diagonala principala = %d \n", s1); printf("Suma de pe diagonala secundara = %d \n", s2); return 0; } /* suma elementelor prime dintr-o matrice */ #include <stdio.h> #include <math.h> using namespace std; int main() { int i, j, m, n; int x[50][50]; printf("Introduceti numarul de linii si de coloane: "); scanf("%d %d", &m, &n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("Introduceti elementul de pe pozitia (%d,%d): ", i + 1, j + 1);

scanf("%d", &x[i][j]); } } int s = 0; int k, prim; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { prim = 1; for (k = 2; k <= ceil(x[i][j] / 2); k++) { if (x[i][j] % k == 0) prim = 0; } if (prim) s+= x[i][j]; } } printf("Suma elementelor prime = %d \n", s); return 0; } /* Transpura unei matrici patratice */ #include <stdio.h> using namespace std; int main() { int i, j, n; int x[50][50]; printf("Introduceti dimensiunea matricii: "); scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("Introduceti elementul de pe pozitia (%d,%d): ", i + 1, j + 1); scanf("%d", &x[i][j]); } } int t; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { t = x[i][j]; x[i][j] = x[j][i]; x[j][i] = t; }

} printf("Matricea transpusa este: \n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%d\t", x[i][j]); } printf("\n"); } return 0; } /* Suma elementelor maxime de pe fiecare linie (daca exista de cel putin 2 ori pe aceeasi linie, sa se adune toate maximurile) */ #include <stdio.h> using namespace std; int main() { int i, j, m, n; int x[50][50]; printf("Introduceti numarul de linii si de coloane: "); scanf("%d %d", &m, &n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("Introduceti elementul de pe pozitia (%d,%d): ", i + 1, j + 1); scanf("%d", &x[i][j]); } } int s = 0; int max; for (i = 0; i < m; i++) { max = x[i][0]; for (j = 1; j < n; j++) { if (x[i][j] > max) max = x[i][j]; } for (j = 0; j < n; j++) { s += (max == x[i][j]) ? max : 0; } } printf("Suma elementelor maxime de pe fiecare linie = %d \n", s);

return 0; } /* n factorial */ #include <stdio.h> using namespace std; int fact(int n) { if (n <= 1) return n; int k = n * fact(n - 1); return k; } int main() { int n; printf("Introduceti numarul n: "); scanf("%d", &n); printf("n factorial = %d", fact(n)); return 0; } /* gasirea primelor n numere prime */ #include <stdio.h> #include <math.h> using namespace std; int main() { int n; printf("Introduceti numarul n: "); scanf("%d", &n); int x[50], k = 0, p = 0, i = 0, prim; while (p < n) { i++; prim = 1; for (k = 2; k <= ceil(i / 2); k++) { if (i % k == 0) prim = 0; } if (prim) x[p++] = i;

} printf("Numerele prime gasite sunt: \n"); for (i = 0; i < n; i++) { printf("%d ", x[i]); } return 0; }

/* sa se ordoneze crescator elementele pare ale unui vector si sa se mute in fata toate aceste elemente */ #include <stdio.h> using namespace std; int main() { int i, j, n; int x[50]; printf("Introduceti dimensiunea vectorului: "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("Introduceti elementul de pe pozitia (%d): ", i + 1); scanf("%d", &x[i]); } int t; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (x[i] % 2 != 0 && x[j] % 2 == 0) { t = x[i]; x[i] = x[j]; x[j] = t; } } } i = 0; while (i < n - 1 && x[i] % 2 == 0) { j = i + 1; while (j < n && x[j] % 2 == 0) { if (x[i] > x[j]) { t = x[i]; x[i] = x[j];

x[j] = t; } j++; } i++; } printf("Vectorul rezultat este: "); for (i = 0; i < n; i++) { printf("%d ", x[i]); } return 0; } /* sa se citeasca un string (numele unei persoane) sa se pastreze doar literele prima litera sa devina majuscula */ #include <stdio.h> #include <string.h> using namespace std; int main() { char s[100], t[100]; int i, k = 0, c; printf("Introduceti numele: "); scanf("%[^\n]", &s); for (i = 0; i < strlen(s); i++) { c = (int) s[i]; if ((c == 32 && k > 0) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) { if (k == 0) { t[k] = (c >= 65 && c <= 90) ? (char) c : (char) (c - 32); } else { t[k] = (char) c; } k++; } } t[k] = '\0'; printf("Numele resultat = %s \n", t); return 0; }

/* sa se inverseze un string si sa se transformele toate literele in litere mari */ #include <stdio.h> #include <string.h> #include <math.h> using namespace std; int main() { char s[100], t; int i, k = 0, c, n; printf("Introduceti string-ul: "); scanf("%[^\n]", &s); n = strlen(s); for (i = 0; i < ceil(n / 2); i++) { t = s[i]; s[i] = s[n - i - 1]; s[n - i - 1] = t; } for (i = 0; i < n; i++) { c = (int) s[i]; if (c >= 97 && c <= 122) { s[i] = (char) (c - 32); } } printf("Numele resultat = %s \n", s); return 0; }

/* numarul de cifre dintr-un numar si suma lor */ #include <stdio.h> #include <math.h> using namespace std; int main() { int n, i = 0, k, s = 0, t = 0; printf("Introduceti numarul n: "); scanf("%d", &n);

while (n > 0) { k = n % (int) pow10(t + 1); n -= k; s += ceil(k / pow10(t)); t++; } printf("Sunt %d cifre si suma lor este %d \n", t, s); return 0; }

/* media aritmetica a elementelor impare dintr-un vector */ #include <stdio.h> #include <math.h> using namespace std; int main() { int i, n, k = 0, s = 0; int x[50]; printf("Introduceti dimensiunea vectorului: "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("Introduceti elementul de pe pozitia (%d): ", i + 1); scanf("%d", &x[i]); } for (i = 0; i < n; i++) { if (x[i] % 2 == 1) { k++; s += x[i]; } } printf("Media aritmetica a return 0; } elementelor impare este: %.2f", round(s / k));

/* produsul cartezian a 2 vectori */ #include <stdio.h> using namespace std;

int main() { int i, j, n, s = 0; int x[50], y[50]; printf("Introduceti dimensiunea vectorilor: "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("Introduceti elementul de pe pozitia (%d) pentru primul vector: ", i + 1); scanf("%d", &x[i]); } for (i = 0; i < n; i++) { printf("Introduceti elementul de pe pozitia (%d) pentru al doilea vector: ", i + 1); scanf("%d", &y[i]); } for (i = 0; i < n; i++) { s += x[i] * y[i]; } printf("Produsul cartezian al celor 2 vectori este: %d ", s); return 0; }

/* Inversa unei matrici */ #include <stdio.h> using namespace std; int main() { int i, j, n; float x[50][50], y[50][50]; printf("Introduceti dimensiunea matricii: "); scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("Introduceti elementul de pe pozitia (%d,%d): ", i + 1, j + 1); scanf("%f", &x[i][j]); y[i][j] = (i == j) ? 1 : 0; } } int ok = 0, p = 0; while (!ok && p < n) {

for (i = 0; i < n; i++) { if (i != p) { for (j = 0; j < n; j++) { if (j != p) { x[i][j] = (x[i][j] * x[p][p] - x[i][p] * x[p][j]) / x[p][p]; } y[i][j] = (y[i][j] * x[p][p] - x[i][p] * y[p][j]) / x[p][p]; } x[i][p] = 0; } } for (i = 0; i < n; i++) { if (i != p) { x[p][i] = x[p][i] / x[p][p]; } y[p][i] = y[p][i] / x[p][p]; } x[p][p] = 1; p++; } int t; printf("Matricea inversa este: \n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%.2f\t", y[i][j]); } printf("\n"); } return 0; } /* suma n^2 recursiv doar a numerelor impare (1, 3, 5 ...) */ #include <stdio.h> using namespace std; float suma(int n) { if (n <= 1) return 1;

float s = (n % 2 != 0 ? n * n : 0) + suma(n - 1); return s; } int main() { int n; float s = 0; printf("Introduceti n: "); scanf("%d", &n); s = suma(n); printf("Suma este: %.2f \n", s); } /* Determinantul unei matrici */ #include <stdio.h> using namespace std; int x[50][50]; int det(int cx, int cy, int size) { if (size == 1) { return x[cx][cy]; } else if (size == 2) { return x[cx][cy] * x[cx + 1][cy + 1] - x[cx][cy + 1] * x[cx + 1][cy]; } else if (size == 3) { return x[cx][cy] * x[cx + 1][cy + 1] * x[cx + 2][cy + 2] + x[cx + 1][cy] * x[cx + 2][cy + 1] * x[cx][cy + 2] + x[cx][cy + 1] * x[cx + 1][cy + 2] * x[cx + 2][cy] x[cx + 2][cy] * x[cx + 1][cy + 1] * x[cx][cy + 2] x[cx + 1][cy + 2] * x[cx + 2][cy + 1] * x[cx][cy] x[cx][cy + 1] * x[cx + 1][cy] * x[cx + 2][cy + 2]; } } int main() { int i, j, n; do { printf("Introduceti dimensiunea matricii (maxim 3): "); scanf("%d", &n);

} while (n < 1 || n > 3); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("Introduceti elementul de pe pozitia (%d,%d): ", i + 1, j + 1); scanf("%d", &x[i][j]); } } printf("Determinantul matricii = %d \n", det(0, 0, n)); return 0; } /* intersectia a doua multimi */ #include <stdio.h> using namespace std; int exists(int t[50], int size, int el) { for (int i = 0; i < size; i++) { if (t[i] == el) return 1; } return 0; } int main() { int i, n, m, k; int x[50], y[50], z[50]; printf("Introduceti dimensiunea primului vector: "); scanf("%d", &n); printf("Introduceti dimensiunea celui de-al doilea vector: "); scanf("%d", &m); for (i = 0; i < n; i++) { printf("Introduceti elementul de pe pozitia (%d) pentru primul vector: ", i + 1); scanf("%d", &x[i]); } for (i = 0; i < m; i++) { printf("Introduceti elementul de pe pozitia (%d) pentru al doilea vector: ", i + 1); scanf("%d", &y[i]); } k = 0; for (i = 0; i < n; i++) { if (!exists(z, k, x[i]) && exists(y, m, x[i]))

{ z[k++] = x[i]; } } printf("Intersectia este: "); for (i = 0; i < k; i++) { printf("%d ", z[i]); } return 0; }

/* reuniunea a doua multimi */ #include <stdio.h> using namespace std; int exists(int t[50], int size, int el) { for (int i = 0; i < size; i++) { if (t[i] == el) return 1; } return 0; } int main() { int i, n, m, k; int x[50], y[50], z[50]; printf("Introduceti dimensiunea primului vector: "); scanf("%d", &n); printf("Introduceti dimensiunea celui de-al doilea vector: "); printf("Introduceti dimensiunea celui de-al doilea vector: "); scanf("%d", &m); for (i = 0; i < n; i++) { printf("Introduceti elementul de pe pozitia (%d) pentru primul vector: ", i + 1); scanf("%d", &x[i]); } for (i = 0; i < m; i++) { printf("Introduceti elementul de pe pozitia (%d) pentru al doilea vector: ", i + 1); scanf("%d", &y[i]); } k = 0; for (i = 0; i < n; i++) {

if (!exists(z, k, x[i])) { z[k++] = x[i]; } } for (i = 0; i < m; i++) { if (!exists(z, k, y[i])) { z[k++] = y[i]; } } printf("Reuniunea este: "); for (i = 0; i < k; i++) { printf("%d ", z[i]); } return 0; } /* se citesc nr pana se intalneste 4 sa se calc media aritmetica */ #include <stdio.h> int main() { int k, n = 0, s = 0; printf("Introduceti numerele: "); while (n != 4) { scanf("%d", &n); k++; s += n; } printf("\nMedia este: %.3f \n", (float) s / k); } /* se citeste o matrice m*n sa se stearga toate coloanele care incep cu "1" */ #include <stdio.h> using namespace std; int main() { int i, j, n, m, k;

int x[50][50], y[50][50]; printf("Introduceti nr de linii: "); scanf("%d", &m); printf("Introduceti nr de coloane: "); scanf("%d", &n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("Introduceti elementul de pe pozitia (%d,%d): ", i + 1, j + 1); scanf("%d", &x[i][j]); } } k = -1; for (i = 0; i < n; i++) { if (x[0][i] != 1) { k++; for (j = 0; j < m; j++) { y[j][k] = x[j][i]; } } } printf("Matricea rezultata este: \n"); for (i = 0; i < m; i++) { for (j = 0; j <= k; j++) { printf("%d\t", y[i][j]); } printf("\n"); } return 0; }

/* sa se construiasca o matrice in spirala */ #include <stdio.h> int main() { int i = 0, j = -1, k, p, n, c = 0, dx, dy, count; int x[50][50]; printf("Introduceti dimensiunea matricii: "); scanf("%d", &n); count = n;

for (k { dx dy if

= 1; k <= 2 * n - 1; k++) = k % 4 == 1 ? 1 : (k % 4 == 3 ? -1 : 0); = k % 4 == 2 ? 1 : (k % 4 == 0 ? -1 : 0); (k % 2 == 0) count--;

for (p = 0; p < count; p++) { i += dy; j += dx; x[i][j] = ++c; } } printf("Matricea rezultata este: \n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%d\t", x[i][j]); } printf("\n"); } }

You might also like