Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*вариант 12
- Найти номер первой из строк, содержащих хотя бы один положительный элемент.
- Уплотнить заданную матрицу, удаляя из нее строки и столбцы, заполненные нулями.
- */
- #include <iomanip>
- #include <iostream>
- #include <time.h>
- #include <conio.h>
- #include <cmath>
- #include <memory>
- #include <stdio.h>
- using namespace std;
- void input(int N, int M, int **arr){
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- scanf_s("%d", &arr[i][j]);
- }
- }
- }
- void output(int N, int M, int **arr){
- for (int i = 0; i < N; ++i) {
- for (int j = 0; j < M; ++j)
- printf_s("%3d", arr[i][j]);
- printf_s("\n");
- }
- }
- void numer_line(int N, int M, int **arr){
- int ipos = 0, cnt = 0;
- for (int i = 0; i < N; ++i) {
- for (int j = 0; j < M; ++j)
- if (arr[i][j] > 0){
- ipos = i;
- cnt++;
- break;
- }
- if (cnt == 1) {
- cout << "number of line with positive item " << ipos + 1 << endl;
- break;
- }
- }
- if (cnt == 0)
- cout << "matrix hasn't these lines" << endl;
- }
- void condensation(int N, int M, int **arr){
- int i, j, m = 0, n = 0;
- for (j = 0; j < M; ++j) {
- for (i = 0; (i < N) && (arr[i][j] == 0); ++i);
- if (i == N)
- break;
- }
- for (m = j; j < M; ++j) {
- i = 0;
- while ((i < N) && (arr[i][j] == 0))
- ++i;
- if (i < N) {
- for (i = 0; i < N; ++i)
- arr[i][m] = arr[i][j];
- ++m;
- }
- }
- for (i = 0; i < N; ++i) {
- for (j = 0; (j < M) && (arr[i][j] == 0); ++j)
- ;
- if (j == M)
- break;
- }
- for (n = i; i < N; ++i) {
- j = 0;
- while ((j < M) && (arr[i][j] == 0))
- ++j;
- if (j < M) {
- for (j = 0; j < M; ++j)
- arr[n][j] = arr[i][j];
- ++n;
- }
- }
- cout << endl << "compacted array: " << endl;
- for (i = 0; i < n; ++i) {
- for (j = 0; j < m; ++j)
- cout << setw(3) << arr[i][j] << ' ';
- cout << endl;
- }
- }
- int main() {
- int N, M;
- cout << "size matrix:" << endl;
- cin >> N >> M;
- int** arr = new int *[N];
- for (int i = 0; i < N; i++)
- arr[i] = new int[M];
- cout << "enter elements:"<<endl;
- input(N, M, arr);
- cout << "\n";
- cout << endl << "original array " << N << "x" << M << ":" << endl;
- output(N, M, arr);
- cout << "\n";
- numer_line(N, M, arr);
- condensation(N, M, arr);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement