Mar 1, 2023 at 9:16am Mar 1, 2023 at 9:16am UTC
A different look at the goal:
1.2, 1.3, 1.4, 1.5
2.3, 2.4, 2.5
3.4, 3.5
4.5
Lets add metadata:
row=1 1.2, 1.3, 1.4, 1.5
row=2 2.3, 2.4, 2.5
row=3 3.4, 3.5
row=4 4.5
Seems true. Can we use that value? Yes:
row=1 row.2, row.3, row.4, row.5
row=2 row.3, row.4, row.5
row=3 row.4, row.5
row=4 row.5
Is there any other bit that could be based on the row? Yes:
row=1 row.(row+1), row.3, row.4, row.5
row=2 row.(row+1), row.4, row.5
row=3 row.(row+1), row.5
row=4 row.(row+1)
What else? How many columns do we have?
row=1 cols=4 row.(row+1), row.3, row.4, row.5
row=2 cols=3 row.(row+1), row.4, row.5
row=3 cols=2 row.(row+1), row.5
row=4 cols=1 row.(row+1)
The number of columns seems to be (max_num-row) in every case and there are (max_num-1) rows in total.
A different (but not better) approach to the delimiter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
int main()
{
const int max_num { 5 };
for ( int row { 1 }; row < max_num; ++row )
{
std::cout << row << '.' << row+1;
for ( int j { row + 2 }; j <= max_num; ++j )
{
std::cout << ", " << row << '.' << j;
}
std::cout << '\n' ;
}
}
Last edited on Mar 1, 2023 at 9:18am Mar 1, 2023 at 9:18am UTC
Mar 1, 2023 at 10:17am Mar 1, 2023 at 10:17am UTC
1 2 3 4 5 6 7 8 9
#include <iostream>
int main() {
constexpr int max_num { 5 };
for (int i { 1 }; i < max_num; ++i)
for (int j { i + 1 }; j <= max_num; ++j)
std::cout << i << '.' << j << (j != max_num ? ", " : "\n" );
}
1.2, 1.3, 1.4, 1.5
2.3, 2.4, 2.5
3.4, 3.5
4.5
or using std::format:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <iterator>
#include <format>
int main() {
constexpr int max_num { 5 };
for (int i { 1 }; i < max_num; ++i)
for (int j { i + 1 }; j <= max_num; ++j)
std::format_to(std::ostream_iterator<char >(std::cout), "{}.{}{}" , i, j, j != max_num ? ", " : "\n" );
}
Last edited on Mar 2, 2023 at 9:54am Mar 2, 2023 at 9:54am UTC
Mar 2, 2023 at 3:03pm Mar 2, 2023 at 3:03pm UTC
@anup30, the point of the exercise was to deal with dueling for loops.