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
|
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int A[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20 }; // define A array's elements.
int B[5][4] = { 0 }, k = 1; // define B array and k counter.
for (int i = 0; i < 5; i++){ // for statement: rows.
for (int j = 0; j < 4; j++){ // for statement: columns.
(j % 2 == 0) ? // check column.
(B[i][j] = A[k + 10 - j / 2 + 2 * i], k += 2) : // column 0 and 2.
(B[i][j] = A[k + (9 + j) / 2 - 2 * i], k -= 2); // column 1 and 3.
} // end of for statement: columns.
} // end of for statement: rows.
for (int p = 0; p < 5; p++){ // first (rows) for statement to print B array.
for (int q = 0; q < 4; q++) // second (columns) for statement to print B array.
cout << setw(6) << B[p][q]; // print B array's elements.
cout << endl; // new line after each row.
} cout << endl; // new line after the array.
return 1; // main function successfully ended.
}
|