Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <clocale>
- #include <ctime>
- #include <cmath>
- using namespace std;
- #define M 5
- #define N 5
- int sumOfPrime(int array[][N], size_t rows);
- void fillTheArray(int array[][N], size_t rows);
- void printTheArray(int array[][N], size_t rows);
- int main(){
- setlocale(LC_ALL, "Bulgarian");
- srand(time(NULL));
- int array[M][N];
- fillTheArray(array, M);
- printTheArray(array, M);
- cout << endl;
- cout << sumOfPrime(array, M);
- return 0;
- }
- //К) намиране на сума на онези елементи от матрицата, които са прости числа;
- int sumOfPrime(int array[][N], size_t rows){
- int sumOfPrime = 0;
- for (size_t i = 0; i < M; i++)
- {
- for (size_t j = 0; j < N; j++){
- if (array[i][j] == 1 || array[i][j] == 0){
- sumOfPrime += array[i][j];
- }
- else{
- bool isPrime = true;
- for (size_t n = 2; n <= array[i][j] / 2; n++)
- {
- if (array[i][j] % n == 0)
- {
- isPrime = false;
- break;
- }
- }
- if (isPrime) sumOfPrime += array[i][j];
- }
- }
- }
- return sumOfPrime;
- }
- void fillTheArray(int array[][N], size_t rows){
- for (size_t i = 0; i < M; i++)
- {
- for (size_t j = 0; j < N; j++){
- array[i][j] = rand() % 5;
- }
- }
- }
- //принтирам масив
- void printTheArray(int array[][N], size_t rows){
- cout << "Оригиналната матрица е това : " << endl;
- for (size_t i = 0; i < M; i++)
- {
- for (size_t j = 0; j < N; j++){
- cout << array[i][j] << "\t";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement