I have to display this number pattern:
-3 1 5 9
7 12 17
15 21
using:
int number = __________________; // assign correct initial value
int row, col;
for (row = 1; row <= 3; row++)
{
// adjust number so it displays the correct first value in the row
________________________________________________
for (col = 1; col <= ___5 - row___; col++)
{
cout << setw (5) << number;
// adjust number so it displays the next correct value in this row
______________________number + row + 3;_________________________________
} / /end inner for loop
cout << endl;
} // end outer for loop
I know number + row + 3 gets the correct numbers across but I cannot seem to get the correct start value.
Try this
With your restrictions, that would be:
Explanation:
(not a bullet) + [12 – (row-1)*2] is for moving between -3, 7 and 15
(not a bullet) – [ ((row-1) + 2) * (5 – (row – 1))] is for compensating growth of the number in the internal loop
Since the result of the right part with row = 1 is -3, initial number is chosen as 0.