Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- void printMatrix(int a[], int n);
- int main()
- {
- const int SIZE = 10;
- int a[SIZE];
- int n;
- do
- {
- cout << "Enter n: ";
- cin >> n;
- }
- while (n < 4 || n > SIZE);
- cout << "Enter main diagonal sequence: ";
- for (int i = 0; i < n; i++)
- {
- cin >> a[i];
- }
- printMatrix(a, n);
- return 0;
- }
- void printMatrix(int a[], int n)
- {
- double matrix[n][n];
- // main diagonal
- for (int i = 0; i < n; i++)
- {
- matrix[i][i] = a[i];
- }
- // below main diagonal
- for (int k = 1; k < n; k++)
- {
- for (int i = 0; i + k < n; i++)
- {
- // row index + above element
- matrix[i + k][i] = i + k + matrix[i + k - 1][i];
- }
- }
- // above main diagonal
- for (int k = 1; k < n; k++)
- {
- for (int j = 0; j + k < n; j++)
- {
- // below element + left element
- matrix[j][j + k] = (matrix[j + 1][j + k] + matrix[j][j + k - 1]) / 2.0;
- }
- }
- // print matrix
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- cout << setw(5) << matrix[i][j] << " ";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement