How to eliminate some items in a for loop

The code below will output

1.1, 1.2, 1.3, 1.4, 1.5,
2.1, 2.2, 2.3, 2.4, 2.5,
3.1, 3.2, 3.3, 3.4, 3.5,
4.1, 4.2, 4.3, 4.4, 4.5,
5.1, 5.2, 5.3, 5.4, 5.5,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
using namespace std;

int main ()


{

	int n = 0;
	int m = 1;  
	for (int i = 0; i < 5; i++ )
	{	
		for (int e = 0; e < 5; e++ )
		{	
			cout << m << "." << ++n <<  ", ";  
		}
			n = 0;
			m = m + 1;
			cout << endl;
	}  
}
.


I want it to show the numbers in a way that the next number should not be equal or bigger than the one before.

in other words, the output should be:

1.2, 1.3, 1.4, 1.5
2.3, 2.4, 2.5
3.4, 3.5
4.5

I hope someone helps me. I did not do programming for a long time. I want to come back.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
   const int max_num { 5 };

   for ( int i { 1 }; i <= max_num; ++i )
   {
      for ( int j { i }; j <= max_num; ++j )
      {
         if ( j == i ) continue;

         std::cout << i << '.' << j;

         if ( j != max_num ) std::cout << ", ";
      }

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

http://coliru.stacked-crooked.com/a/879d95e1a01aa852
I want it to show the numbers in a way that the next number should not be equal or bigger than the one before.

I believe you want the next number to not be smaller or equal, that is what your desired output shows.
An even more compact version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
   const 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;

         if ( j != max_num ) std::cout << ", ";
      }

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

http://coliru.stacked-crooked.com/a/43349676143df5bd
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
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
if it's fixed output
1
2
3
4
5
6
#include <iostream>

int main()
{
	std::cout<<"1.2, 1.3, 1.4, 1.5\n2.3, 2.4, 2.5\n3.4, 3.5\n4.5";
}


i know it feels like troll like, but i believe it's correct solution as problem stated
@anup30, the point of the exercise was to deal with dueling for loops.
Topic archived. No new replies allowed.