I need help with a two-dimensional table

Its given the matrix T[n][n] with integer elements. It is considered that the two diagonals i divide the matrix into four areas: north, south, east, west (the elements on the diagonals are not part of no area). Compose a program that will count the elements located in the northern area. Can you make this tabel just with iostream please :*
you don't need the table, all you need is n.
the first part of the sum is
* 1 2 3 4 ... * (n-2)
the next row is
** 1 2 3 4 ... ** (n-4)
so is there a pattern that just gives you the answer using only N?
There may be 2 patterns, one for even N, one for odd N, but regardless, I believe you can do it without the table if you play with it a little.
Last edited on
The sums are:

n = 3; 1
n = 4; 2
n = 5; 3 + 1
n = 6; 4 + 2
n = 7; 5 + 3 + 1

So:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
	int n {}, cnt {};

	std::cout << "Enter n: ";
	std::cin >> n;

	for (int i = n - 2; i > 0; i -= 2)
		cnt += i;

	std::cout << "Number of elements are: " << cnt << '\n';
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
	const size_t max_size {20};

	for (size_t n = 1; n <= max_size; ++n) {
		size_t cnt {};

		for (int i = n - 2; i > 0; i -= 2)
			cnt += i;

		std::cout << n << "  " << cnt << '\n';
	}
}



1  0
2  0
3  1
4  2
5  4
6  6
7  9
8  12
9  16
10  20
11  25
12  30
13  36
14  42
15  49
16  56
17  64
18  72
19  81
20  90

terms1 wrote:
count the elements located in the northern area

Are you sure that you just want to count them?

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main()
{
   int n;
   cout << "Input n: ";   cin >> n;
   cout << "Number of northern elements = " << ( n * ( n - 2 ) + ( n % 2 ) ) / 4 << '\n';
}


Input n: 13
Number of northern elements = 36
Last edited on
yes
is this the right program?
You tell us. Does it produce the expected values?

NB as the matrix is square, the numbers in each of the 4 areas are the same.
yes its right, thanks guys
@seeplus can you explain how you did the first program?
Topic archived. No new replies allowed.