For (A), the first inner loop won't run, because j = i = 0, and j is not < i.
If it were "j <= i", then you'd get 6 inner loop runs that you seem to expect.
(B) Honestly, if you're not sure, then just start counting it. You'll notice a pattern sooner or later.
The following modification to your program might make the pattern easier to see.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
int main()
{
const int SIZE = 6; // change this to test
for(int i{0}; i<SIZE; i++)
{
for(int j{0}; j < i; j++)
{
std::cout << ".";
}
std::cout << "\n";
}
}
| |
padding out to a 6x5 rectangle:
xxxxx
.xxxx
..xxx
...xx
....x
..... |
You'll see that the x's are symmetric to the dots, and since the area of the rectangle is (n)(n-1), the number of dots must be half that, or (n)(n-1)/2.
In other words, for a given size of
n, the number of dots is the
(n-1)th triangular number in your code.
Given T(n) = (n)(n+1)/2, it means T(n-1) = (n-1)(n)/2.
https://en.wikipedia.org/wiki/Triangular_number
SIZE = 6 ---> produces 5th triangle number --> (5)(6)/2 = 15.