Дана целочисленная прямоугольная матрица. Определить номер первого из столбцов, содержащих хотя бы один нулевой элемент.
Характеристикой строки целочисленной матрицы назовем сумму ее отрицательных четных элементов. Представляя строки заданной матрицы, расположить их в соответствии с убыванием характеристик.
Первую часть я вроде как сделал, готов помочь вам с ней. Надо написать целиком программу, а не 2 программы.
Другие языки программирования и технологии
Программирование на c++. Непонятен ход решения, сама логика. Буду признателен за рабочий код.
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void swaprow(int * , int *, size_t);
int characteristic(int *, size_t);
int ** matrix( size_t);
void destroy(int **, size_t);
void random(int **, size_t);
void print(int **, size_t);
size_t firstzerocol(int **, size_t);
void sortmatrix(int **, size_t);
int main() {
size_t size = 18;
int ** mx = NULL;
if (mx = matrix(size)) {
random(mx, size);
print(mx, size);
size_t zero = firstzerocol(mx, size);
if (zero) cout << " Zero in column " << zero;
else cout << " Zero does not exist!";
cout << endl << endl;
sortmatrix(mx, size);
print(mx, size);
destroy(mx, size);
cin.get();
}
}
void sortmatrix(int ** _m, size_t _s) {
int * row = new (nothrow) int [_s];
if (row) for (size_t n = 1, m; n < _s; n++) for (m = 0; m < _s - n; m++) if (characteristic(_m[m], _s) > characteristic(_m[m + 1], _s)) swaprow(_m[m], _m[m + 1], _s);
}
size_t firstzerocol(int ** _m, size_t _s) {
for (size_t c = 0; c < _s; ++c) for (size_t r = 0; r < _s; ++r) if (!_m[r][c]) return c + 1;
return 0;
}
void print(int ** _m, size_t _s) {
for (size_t r = 0; r < _s; ++r, cout << endl) for (size_t c = 0; c < _s; ++c) cout << setw(4) << _m[r][c];
cout << endl;
}
void random(int ** _m, size_t _s) {
srand(static_cast<unsigned>(time(NULL)));
for (size_t r = 0; r < _s; ++r) for (size_t c = 0; c < _s; ++c) _m[r][c] = rand() % 199 - 99;
}
int ** matrix( size_t _s) {
int ** ptr = NULL;
if (ptr = new (nothrow) int * [_s]) {
for (size_t n = 0; n < _s; ++n) {
if (!(ptr[n] = new (nothrow) int [_s])) {
destroy(ptr, _s);
ptr = NULL;
break;
}
if (!ptr) break;
}
}
return ptr;
}
void destroy(int ** _m, size_t _s) {
if (_m) {
for (size_t n = 0; n < _s; ++n) if (_m[n]) delete[] _m[n];
delete[] _m;
}
}
void swaprow(int * _first, int * _second, size_t _s) { for (size_t n = 0; n < _s; ++n) swap(_first[n], _second[n]); }
int characteristic(int * _row, size_t _s) {
int sum = 0;
for (size_t n = 0; n < _s; ++n) {
if (_row[n] >= 0 || _row[n] & 1) continue;
sum += _row[n];
}
return sum;
}
#include <iomanip>
#include <ctime>
using namespace std;
void swaprow(int * , int *, size_t);
int characteristic(int *, size_t);
int ** matrix( size_t);
void destroy(int **, size_t);
void random(int **, size_t);
void print(int **, size_t);
size_t firstzerocol(int **, size_t);
void sortmatrix(int **, size_t);
int main() {
size_t size = 18;
int ** mx = NULL;
if (mx = matrix(size)) {
random(mx, size);
print(mx, size);
size_t zero = firstzerocol(mx, size);
if (zero) cout << " Zero in column " << zero;
else cout << " Zero does not exist!";
cout << endl << endl;
sortmatrix(mx, size);
print(mx, size);
destroy(mx, size);
cin.get();
}
}
void sortmatrix(int ** _m, size_t _s) {
int * row = new (nothrow) int [_s];
if (row) for (size_t n = 1, m; n < _s; n++) for (m = 0; m < _s - n; m++) if (characteristic(_m[m], _s) > characteristic(_m[m + 1], _s)) swaprow(_m[m], _m[m + 1], _s);
}
size_t firstzerocol(int ** _m, size_t _s) {
for (size_t c = 0; c < _s; ++c) for (size_t r = 0; r < _s; ++r) if (!_m[r][c]) return c + 1;
return 0;
}
void print(int ** _m, size_t _s) {
for (size_t r = 0; r < _s; ++r, cout << endl) for (size_t c = 0; c < _s; ++c) cout << setw(4) << _m[r][c];
cout << endl;
}
void random(int ** _m, size_t _s) {
srand(static_cast<unsigned>(time(NULL)));
for (size_t r = 0; r < _s; ++r) for (size_t c = 0; c < _s; ++c) _m[r][c] = rand() % 199 - 99;
}
int ** matrix( size_t _s) {
int ** ptr = NULL;
if (ptr = new (nothrow) int * [_s]) {
for (size_t n = 0; n < _s; ++n) {
if (!(ptr[n] = new (nothrow) int [_s])) {
destroy(ptr, _s);
ptr = NULL;
break;
}
if (!ptr) break;
}
}
return ptr;
}
void destroy(int ** _m, size_t _s) {
if (_m) {
for (size_t n = 0; n < _s; ++n) if (_m[n]) delete[] _m[n];
delete[] _m;
}
}
void swaprow(int * _first, int * _second, size_t _s) { for (size_t n = 0; n < _s; ++n) swap(_first[n], _second[n]); }
int characteristic(int * _row, size_t _s) {
int sum = 0;
for (size_t n = 0; n < _s; ++n) {
if (_row[n] >= 0 || _row[n] & 1) continue;
sum += _row[n];
}
return sum;
}
2-ую часть я сделал так бы:
Создать еще 1 массив размером - N;
int A[N][N], B[N];
/* B - вспомогательный массв, где i-ый элемент содержит характеристику i-ой строки */
for(int i = 0; i < N; i++)
{
int k = 0;
for(int j = 0; j < N; j++)
if(A[ i ][ j ] < 0 && A[ i ][ j ] % 2 == 0)
k+= A[ i ][ j ];
B[ i ] = k;
}
Ну а дальше остается сортировать строки в порядке убывания, операясь на вспомогательный массив.
Создать еще 1 массив размером - N;
int A[N][N], B[N];
/* B - вспомогательный массв, где i-ый элемент содержит характеристику i-ой строки */
for(int i = 0; i < N; i++)
{
int k = 0;
for(int j = 0; j < N; j++)
if(A[ i ][ j ] < 0 && A[ i ][ j ] % 2 == 0)
k+= A[ i ][ j ];
B[ i ] = k;
}
Ну а дальше остается сортировать строки в порядке убывания, операясь на вспомогательный массив.
#include <vcl.h>
#include <conio>
#include <stdio.h>
#include <iostream.h>
#include <time>
int main(){
const int m = 10, n = 10;
int mas[m][n] = {0}, newMas[m][n] = {0}, car[2][m] = {0};
int i,j,x;
srand (time(NULL));
for (i=0; i<m;>[j] = rand() % 20 - 10;
cout << mas[j] << '\t';
}
cout << endl;
}
cout << endl;
for (i=0; i<m;>[j]<0 && mas[j]%2==0)
sum += mas[j];
}
car[0] = sum;
car[1] = i;
}
for( i=0; i < m; i++) {
for( j = m-1; j > i; j-- ) {
if ( car[0][j-1] < car[0][j] ) {
x=car[0][j-1]; car[0][j-1]=car[0][j]; car[0][j]=x;
x=car[1][j-1]; car[1][j-1]=car[1][j]; car[1][j]=x;
}
}
}
for (i=0; i<m;>;
for (j=0; j<n;>[j] = mas[x][j];
}
}
for (i=0; i<m;>[j] << '\t';
}
cout << endl;
}
cout << endl;
getch();
return 0;
}
#include <conio>
#include <stdio.h>
#include <iostream.h>
#include <time>
int main(){
const int m = 10, n = 10;
int mas[m][n] = {0}, newMas[m][n] = {0}, car[2][m] = {0};
int i,j,x;
srand (time(NULL));
for (i=0; i<m;>[j] = rand() % 20 - 10;
cout << mas[j] << '\t';
}
cout << endl;
}
cout << endl;
for (i=0; i<m;>[j]<0 && mas[j]%2==0)
sum += mas[j];
}
car[0] = sum;
car[1] = i;
}
for( i=0; i < m; i++) {
for( j = m-1; j > i; j-- ) {
if ( car[0][j-1] < car[0][j] ) {
x=car[0][j-1]; car[0][j-1]=car[0][j]; car[0][j]=x;
x=car[1][j-1]; car[1][j-1]=car[1][j]; car[1][j]=x;
}
}
}
for (i=0; i<m;>;
for (j=0; j<n;>[j] = mas[x][j];
}
}
for (i=0; i<m;>[j] << '\t';
}
cout << endl;
}
cout << endl;
getch();
return 0;
}
Похожие вопросы
- Двоичный код быстрее языков программирования по типу c++? (Я знаю что эти языки тоже написаны на двоичном коде)
- Подскажите с чего лучше начать изучение программирования на C#?
- Какую выбрать среду программирования для C++/Visual C++ для учебных целей кроме CodeGear Delphi+C++ Builder?
- Задача по программированию на C++ C++ C++ C++, не на Pascal
- какой язык программирования выбрать C# или С++
- Помогите решить тут нужно умение строить цикл, чего я еще не умею. . Программирование на C#
- c++ Непонятная проблема
- Помогите в программирование на c++. Начало обучения
- Начинание программирования на C++
- Визуальное программирование в C++