Lab #7.1. Two-dimensional array
Given one dimensional array arr of size n2 (lab7.1.cpp).
#include <iostream>
using namespace std;
const int arr_size = 3;
int main ()
{
int arr[arr_size*arr_size]={11,22,33,44,55,66,77,88,99};
for (int i=0; i<arr_size; i++) cout << arr[i] << endl;
return 0;
}
Write all values of arr to a two-dimensional array of size nxn.
11 |
22 |
33 |
44 |
55 |
66 |
77 |
88 |
99 |
Print sums of the both diagonals (lab7.1a.cpp).
Both sums should be 165.
With small changes, the program should work for any sizes of arrays.
Lab #7.2 (advanced). The Snail task.
Given two-dimensional array n*m. Fill the array with numbers 1..nxm, starting with the outer elements, moving circularly clockwise, gradually approaching the centre. Start filling from the element (0,0).
1 |
2 |
3 |
10 |
11 |
4 |
9 |
12 |
5 |
8 |
7 |
6 |
Solution example see in lab7.2a.cpp. A compact solution example: lab7.2b.cpp. A solution borrowed from students: lab7.2c.cpp.
With small changes, the program should work for any sizes of arrays.