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 minPrimeEvenSumOfColumn(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;
- if (minPrimeEvenSumOfColumn(array, M)) cout << minPrimeEvenSumOfColumn(array, M);
- else cout << "Няма сума по колона отгваряща на изискванията!" << endl;
- return 0;
- }
- // Н) да се намери колоната, която има сума на елементите нечетно, просто число, най-малко сред сума на елементите от останалите колони;
- int minPrimeEvenSumOfColumn(int array[][N], size_t rows){
- int sumOfColumn=0;
- int minPrimeSum = INT_MAX;
- int colIndex=INT_MAX;
- for (size_t j = 0; j < N; j++){
- for (size_t i = 0; i < M; i++){
- sumOfColumn += array[i][j];
- }
- if (sumOfColumn%2 !=0)
- {
- bool isPrime = true;
- for (size_t n = 2; n <= sumOfColumn / 2; n++)
- {
- if (sumOfColumn % n == 0)
- {
- isPrime = false;
- break;
- }
- }
- if (isPrime) {
- if (minPrimeSum > sumOfColumn){
- minPrimeSum = sumOfColumn;
- colIndex = j;
- }
- }
- }
- sumOfColumn = 0;
- }
- if (colIndex!=INT_MAX)return colIndex;
- else return 0;
- }
- 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() % 10 ;
- }
- }
- }
- //принтирам масив
- 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