Need some help! :')

How do i make nested loop to create these numbers?
10 11 13 16 20 25

Please help me, thanks!
Last edited on
1
2
3
4
5
6
7
#include <iostream>

int main()
{
   for (auto& itr : { 10, 11, 13, 16, 20, 25 }) { std::cout << itr << ' '; }
   std::cout << '\n';
}
What do you mean by "nested for"?

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
  int sum {10};
  for ( int add {0}; add < 6; ++add) { std::cout << (sum+=add) << ' '; }
  std::cout << '\n';
}
It was meant to be nested loop
How i can make nested loop to create those numbers?
Do you understand the pattern in that sequence of numbers? Do you understand the relationship each number has to the previous numbers? Do you understand how to generate the next number in that sequence?

You need to understand what it is you're trying to write code for, before you can write the code.

EDIT: And stop spamming the forums with multiple threads for the same question.
Last edited on
Writing nonsense is easy:
1
2
3
4
for ( int oh {0}; oh < 1; ++oh ) {
  for ( int my {0}; my < 1; ++my ) {
  }
}

The question is, what sensible nesting could you have in your problem?
When you have a series of numbers and are looking for some orderly pattern one way of finding a possible pattern is to get the (finite) difference.

10 11 13 16 20 25 gives
1 2 3 4 5

So we have a pattern, this time anyway, and if it all runs to plan the next number will be 25 + 6 = 31. Of course, the next number might be -1729, there's nothing to say it couldn't be, but people allocating marks generally can't cope with a smartarse (ass?) so best go for 31.

That's half the job done.

Edit: More than half actually because you don't have to predict the next number, just account for the one's you have.
Last edited on
Here's something that uses a nested for loop and outputs those values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
   int vals[] = {10, 11, 13, 16, 20, 25};

   
   for ( int x = 0; x < 10; x++)
   {
      for (int y = 0; y < 10; y++)
      {
         std::cout << vals[x+y] << ' ';
         if (x+y == 5) return 0;
      }
   }
}
Why do you need a NESTED loop to get those numbers?

an=10+n(n+1)/2 for n = 0,1,...,5
Loops are no longer nesting because they've flown south for the winter ;)
/runs
That's a post-Christmas-party joke ... but I did laugh!
Quite frankly I'm surprised I haven't read "42" in this thread yet haha :)
H00G0 wrote:
Quite frankly I'm surprised I haven't read "42" in this thread yet

This do you H00G0?

Nested loops - check
Required numbers - check
42 - check

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

int main()
{
   int p[] = { 11, -165, 935, -2475, 3374, -960, 7200 };

   for ( int x = 0; x < 7; x++ )
   {
      int y = 0;
      for ( int k = 0; k < 7; k++ )
      {
         y = x * y + p[k];
      }
      cout << y / 720 << " ";
   }
}
10 11 13 16 20 25 42 
For god's sake lastchance, you even took the time and effort x') Bless
Topic archived. No new replies allowed.