Заданы две матрицы А (6,6) и В (6,6). Для матрицы, которая отвечает заданному условию (1) выполните преобразования (2)
Условие (1) проверить в подпрограмме Function, преобразование выполнить в подпрограмме Procedure.
1-Количество нулевых элементов больше.
2-Найти сумму всех элементов данной диагонали.
Другие языки программирования и технологии
Помогите составить программу С++ "Процедуры и функции"
Матрицы заполняются случайными числами 0..99
Разница между процедурой и функций в С++ условна
"функция" возвращает "нечто", процедура "пустоту"))
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
/*global declarations*/
int mas1[6][6];
int mas2[6][6];
int i,j,sumZero1,sumZero2;
int GetZeroSum(int array[6][6])
{int summ=0;
for (int x=0;x<6;x++)
for (int y=0;y<6;y++){
if (array[x][y]==0) summ++;
}
return summ;
}
void diagonalProcedure(int array[6][6],int* result)
{
int summ=0;
for (int x=0;x<6;x++)
for (int y=0;y<6;y++){
if (x==y) summ=summ+array[x][y];
}
*result=summ;
}
void main()
{ /*m1:*/
clrscr();
int tmp;
/*Zapolneniye sly4ainimi 4islami*/
printf("Matrix1");
printf("\n");
for (int x=0;x<6;x++){
for (int y=0;y<6;y++)
{tmp=random(99);
mas1[x][y]=tmp;
printf("%i ",tmp);
}
printf("\n");
}
printf("\n");
printf("Matrix2");
printf("\n");
for (int x=0;x<6;x++){
for (int y=0;y<6;y++)
{tmp=random(99);
mas2[x][y]=tmp;
printf("%i ",tmp);
}
printf("\n");
}
sumZero1=GetZeroSum(mas1);
sumZero2=GetZeroSum(mas2);
if(sumZero1>sumZero1)
{diagonalProcedure(mas1,&tmp);
}
else
{diagonalProcedure(mas2,&tmp);
}
printf("\n Element's summ: %i",tmp);
getch();
}
Разница между процедурой и функций в С++ условна
"функция" возвращает "нечто", процедура "пустоту"))
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
/*global declarations*/
int mas1[6][6];
int mas2[6][6];
int i,j,sumZero1,sumZero2;
int GetZeroSum(int array[6][6])
{int summ=0;
for (int x=0;x<6;x++)
for (int y=0;y<6;y++){
if (array[x][y]==0) summ++;
}
return summ;
}
void diagonalProcedure(int array[6][6],int* result)
{
int summ=0;
for (int x=0;x<6;x++)
for (int y=0;y<6;y++){
if (x==y) summ=summ+array[x][y];
}
*result=summ;
}
void main()
{ /*m1:*/
clrscr();
int tmp;
/*Zapolneniye sly4ainimi 4islami*/
printf("Matrix1");
printf("\n");
for (int x=0;x<6;x++){
for (int y=0;y<6;y++)
{tmp=random(99);
mas1[x][y]=tmp;
printf("%i ",tmp);
}
printf("\n");
}
printf("\n");
printf("Matrix2");
printf("\n");
for (int x=0;x<6;x++){
for (int y=0;y<6;y++)
{tmp=random(99);
mas2[x][y]=tmp;
printf("%i ",tmp);
}
printf("\n");
}
sumZero1=GetZeroSum(mas1);
sumZero2=GetZeroSum(mas2);
if(sumZero1>sumZero1)
{diagonalProcedure(mas1,&tmp);
}
else
{diagonalProcedure(mas2,&tmp);
}
printf("\n Element's summ: %i",tmp);
getch();
}

#include <iostream>
#include <iomanip>
using namespace std;
bool Function(int matrix[][6], const size_t rows = 6, const size_t cols = 6);
void Procedure(int matrix[][6], const size_t rows = 6, const size_t cols = 6);
void Print(int matrix[][6], const size_t rows = 6, const size_t cols = 6);
int main() {
int A[6][6] = { { 0, 1, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1, 0 }, { 0, 1, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1, 0 }, { 0, 1, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1, 0 } };
int B[6][6] = { { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 0 } };
cout << "\n Matrix A:\n\n"; Print(A);
if (Function(A)) Procedure(A);
cout << " Matrix B:\n\n"; Print(B);
if (Function(B)) Procedure(B);
cin.get();
}
// Определяет количество нулевых элементов матрицы
bool Function(int matrix[][6], const size_t rows, const size_t cols) {
int count = 0;
for (size_t r = 0; r < rows; ++r) for (size_t c = 0; c < rows; ++c) count += matrix[r][c] ? 1 : -1;
return count >= 0? false : true;
}
// Выводит сумму элементов главной диагонали
void Procedure(int matrix[][6], const size_t rows, const size_t cols) {
long long sum = 0;
for (size_t n = 0; n < rows; ++n) sum += matrix[n][n];
cout << " Sum = " << sum << endl;
}
// Выводит матрицу на экран
void Print(int matrix[][6], const size_t rows, const size_t cols) {
for (size_t r = 0; r < rows; ++r, cout << endl) for (size_t c = 0; c < rows; ++c) cout << ' ' << matrix[r][c];
cout << endl;
}
P.S. Правильно заданный вопрос - половина ответа.
#include <iomanip>
using namespace std;
bool Function(int matrix[][6], const size_t rows = 6, const size_t cols = 6);
void Procedure(int matrix[][6], const size_t rows = 6, const size_t cols = 6);
void Print(int matrix[][6], const size_t rows = 6, const size_t cols = 6);
int main() {
int A[6][6] = { { 0, 1, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1, 0 }, { 0, 1, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1, 0 }, { 0, 1, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1, 0 } };
int B[6][6] = { { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 0 } };
cout << "\n Matrix A:\n\n"; Print(A);
if (Function(A)) Procedure(A);
cout << " Matrix B:\n\n"; Print(B);
if (Function(B)) Procedure(B);
cin.get();
}
// Определяет количество нулевых элементов матрицы
bool Function(int matrix[][6], const size_t rows, const size_t cols) {
int count = 0;
for (size_t r = 0; r < rows; ++r) for (size_t c = 0; c < rows; ++c) count += matrix[r][c] ? 1 : -1;
return count >= 0? false : true;
}
// Выводит сумму элементов главной диагонали
void Procedure(int matrix[][6], const size_t rows, const size_t cols) {
long long sum = 0;
for (size_t n = 0; n < rows; ++n) sum += matrix[n][n];
cout << " Sum = " << sum << endl;
}
// Выводит матрицу на экран
void Print(int matrix[][6], const size_t rows, const size_t cols) {
for (size_t r = 0; r < rows; ++r, cout << endl) for (size_t c = 0; c < rows; ++c) cout << ' ' << matrix[r][c];
cout << endl;
}
P.S. Правильно заданный вопрос - половина ответа.
в с++ нет разницы между процедурой и функцией.
Похожие вопросы
- Помогите составить программу. тема "Процедуры и функции" при помощи Паскаль
- Помогите составить программу через Pascal с использованием функции и процедуры.
- Помогите составить программу на паскале!
- Помогите составить программу на pascal
- помогите составить программу на языке Turbo pascal
- Помогите составить программу на языке Pascal
- Помогите составить программу на языке Pascal
- помогите составить программу в паскаль, плиз
- Помогите составить программу на Turbo Pascal?
- Помогите составить программу в Pascal ABC