Matrix9. Дана матрица размера M × N. Вывести ее элементы, расположенные в строках с четными номерами (2, 4, …). Вывод элементов производить по строкам, условный оператор не использовать.
>Решение Pascal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | program Matrix9; var a:array [1..10,1..10] of integer; M, N, i, j:Integer; begin Write('N: '); Readln(N); Write('M: '); Readln(M); for i:=1 to M do begin writeln(i,': '); for j:=1 to N do begin Write(j,' : '); Read(a[i,j]); end; end; i:=0; while (i<M) do begin i:=i+2; for j:=1 to N do Write(' : ',a[i,j]); writeln(' : '); end; end. |
c++
int main()//matrix9
{
int const n = 8;
const int m = 8;
int k = 5;
int arr[10][10];
srand(time(0));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
arr[i][j] = rand() % 5 + 1;
cout << arr[i][j]<< " ";
}
cout << endl;
}
cout << endl << endl;
for (int i = 0; i < n; i++)
{
if ( (i+1)%2 == 0)
for (int j = 0; j < m; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
выводится несуществующая строка, решение на паскале не верное
C++
#include
#include
#include
using namespace std;
main(){
int M, N, i, j;
srand(time(NULL));
do {cout<>M;} while (M<1);
do {cout<>N;} while (N<1);
int A[M][N]{};
for (i=0;i<M;i++){
for (j=0;j<N;j++){A[i][j]=rand();}}
for (i=2;i<M;i+=2){
for (j=0;j<N;j++){cout<<" "<<A[i][j];} cout<<endl;}
return 0;}