Література
1. Г.И. Светозарова, Е.В. Сигитов, А.В. Козловский. Практикум по программированию на алгоритмических языках. Краткое теоретическое введение. – стр. 35.
2. С/С++. Программирование на языке высокого уровня / Т.А. Павловская. – СПб.: Питер, 2001. – 464 с.: ил.
Додаток 1
//ф. Matr_pav.CPP
//Примеры из Павловской на передачу двумерных массивов "произвольной размерности"
//в функции Sum() и Sum1() (через int * и int ** – по-разному будет потом доступ к элементам!)
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int sum(const int * a, const int nrow, const int ncol);
int sum1(const int ** a, const int nrow, const int ncol);
int main(void)
{ int b[2][2]={{2,2},{4,3}};
int i,j, nrow, ncol, *a;
cout << "\n 0) Sum matr b: "<< sum(&b[0][0],2,2);
//cout << "Sum matr b: "<< sum(b,2,2); - Bad!
cout << "\n 1) Input nrow and ncol:";
cin >> nrow >> ncol;
cout << "Input matr (" <<nrow<<"x"<< ncol <<")\n";
a= (int *)malloc(nrow*ncol*sizeof(int));
for (i=0;i<nrow;i++)
for (j=0;j<ncol;j++)
// scanf("%d", &a[i*ncol+j]); //OK
// cin>> &a[i*ncol+j]; //Bad! - "Illegal structure operation"
cin>> *(a+i*ncol+j);
cout<<"Sum matr a: "<< sum(a,nrow,ncol);
//For using two indexes - f. sum1 (a'la Pavlovskaya):
int **pc;
cout << "\n 2) Input nrow and ncol (for sum1): ";
cin >> nrow >> ncol;
cout << "Input matr (" <<nrow<<"x"<< ncol <<")\n";
//pc=new int* [nrow]; - 'OK
pc=(int **)malloc(nrow*sizeof(int*));
for (i=0;i<nrow;i++)
// pc[i]=new int [ncol]; - 'OK
pc[i]=(int*)malloc(ncol*sizeof(int));
for (i=0;i<nrow;i++)
for (j=0; j<ncol;j++) cin>> pc[i][j];
cout<<"Summa matr c (from sum1):"<< sum1(pc,nrow,ncol);
// --- Deleting matrix ---
for (i=0; i<nrow; i++) delete pc[i];
delete pc;
free(a);
getch();
return 0;
}
int sum(const int *a, const int nstr,const int nstb)
{ int i,j, s=0;
cout<<"Matr from sum:\n";
for (i=0;i<nstr;i++)
{ for (j=0;j<nstb;j++)
cout<< *(a+i*nstb+j)<<" ";
cout<<"\n";
}
for (i=0;i<nstr;i++)
for (j=0;j<nstb;j++) s+=a[i*nstb+j];
return s;
}
int sum1(const int ** a, const int nstr, const int nstb)
{ int i,j,s=0;
cout<<"Matr from sum1:\n";
for (i=0;i<nstr;i++)
{ for (j=0;j<nstb;j++)
cout<< a[i][j] <<" ";
cout<<"\n";
}
for (i=0;i<nstr;i++)
for (j=0;j<nstb; j++) s+=a[i][j];
return s;
}
Додаток 2
/* *******************************
* Artem V. Chirkov? * */
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
// ----------------------------------------------
void action(double**,int, int&, int&);
void action(double*,int, int&, int&);
// ==============================================
int main()
{
int N, i, j, nW=0, nH=0, nW1=0, nH1=0;
do // ---------------------
{
clrscr();
// --- Entering N ---
cout << "Enter N (matrix size): "; cin >> N;
// --- Initializing and entering Matrix ---
double **A = new double*[N];
for (i=0; i<N; i++) A[i] = new double[N];
double *B = (double*) new double[N*N];
cout << "Enter Matrix "<<N<<"x"<<N<<":\n";
for (i=0; i<N; i++)
for (j=0; j<N; j++) cin >> A[i][j];
for (i=0; i<N; i++)
for (j=0; j<N; j++) B[i*N+j] = A[i][j];
// --- Output Matrix and results ---
clrscr();
cout << "===== Old matrix: =========\n\n";
for (i=0; i<N; i++)
{
for (j=0; j<N; j++) cout << setiosflags(ios::left) << setw(6) << setprecision(2) << A[i][j];
cout << endl;
}
action(A, N, nW, nH);
action(B, N, nW1, nH1);
cout << "\n===== New matrix A "<<nW<<"x"<<nH<<": =====\n\n";
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
if (A[i][j] != 0) cout << setiosflags(ios::left) << setw(6) << setprecision(2) << A[i][j];
if (A[i][N-1] != 0) cout << endl;
}
cout << "\n===== New matrix B "<<nW1<<"x"<<nH1<<": =====\n\n";
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
if (B[i*N+j] != 0) cout << setiosflags(ios::left) << setw(6) << setprecision(2) << B[i*N+j];
if (B[i*N+N-1] != 0) cout << endl;
}
// --- Deleting matrix ---
for (i=0; i<N; i++) delete A[i];
delete A;
delete B;
getch();
cout << "\n=========================================\n";
cout << "\nPress 'n' to exit the program or any other key to continue...";
}
while ( getch() != 'n' );
// ------------------------
return 0;
}
// ----------------------------------------------
/* Returns new size of matrix (WxH)
Also changes matrix A */
void action(double **A, int N, int & W, int & H)
{
W = N; H = N;
int *zCols = new int[N], *zRows = new int[N];
for (int i=0; i<N; i++)
{ zCols[i] = 0; zRows[i] = 0; }
for (i=0; i<N; i++)
for (int j=0; j<N; j++)
if (A[i][j] == 0) { zCols[i] = 1; zRows[j] = 1; }
for (i=0; i<N; i++)
{
if (zCols[i] == 1)
{
W--;
for (int j=0; j<N; j++) A[i][j] = 0;
}
if (zRows[i] == 1)
{
H--;
for (int j=0; j<N; j++) A[j][i] = 0;
}
}
}
void action(double *A, int N, int & W, int & H)
{
W = N; H = N;
int *zCols = new int[N], *zRows = new int[N];
for (int i=0; i<N; i++)
{ zCols[i] = 0; zRows[i] = 0; }
for (i=0; i<N; i++)
for (int j=0; j<N; j++)
if (A[i*N+j] == 0) { zCols[i] = 1; zRows[j] = 1; }
for (i=0; i<N; i++)
{
if (zCols[i] == 1)
{
W--;
for (int j=0; j<N; j++) A[i*N+j] = 0;
}
if (zRows[i] == 1)
{
H--;
for (int j=0; j<N; j++) A[j*N+i] = 0;
}
}
}
