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 findElement(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 (findElement(array, M)) cout << findElement(array, M)<< endl;
- else cout << "No such element!" << endl;
- return 0;
- }
- //И) намиране на елемент на матрицата, който има само нулеви съседи;
- int findElement(int array[][N], size_t rows){
- int prev;
- int next;
- for (size_t i = 0; i < M; i++)
- {
- for (size_t j = 0; j < N; j++){
- if (i == 0 && j == 0 && array[i][j + 1] == 0){
- return array[0][0];
- }
- else if (j == N - 1 && array[i][j - 1] == 0){
- return array[i][N-1];
- }
- else{
- prev = array[i][j - 1];
- next = array[i][j + 1];
- if (next == 0 && prev == 0){
- return array[i][j];
- }
- }
- }
- }
- 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() % 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