Răspuns:
#include <iostream>
using namespace std;
int n,m,i,j,num,a[20][20];
int main()
{
cout << "nr. linii, n= "; cin >> n;
cout << "nr. coloane, m= "; cin >> m;
for (i=1; i<=n; ++i)
{
if (i%2==1)
for (j=1; j<=m; ++j) a[i][j]=++num;
else for (j=m; j>0; --j) a[i][j]=++num;
}
cout << "matricea generata: "<< endl;
for (i=1; i<=n; ++i)
{
for (j=1; j<=m; ++j)
cout << a[i][j] << " ";
cout << endl;
}
return 0;
}
Explicație: