the increment of the number sequence is a Arithmetic sequence
so the program could be like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
using std::cout;
int main()
{
int increment = 0;
int num = 1;
for(int i=1;i<=11;i++)
{
if(i==6)
num += 10;
num += increment;
cout<<num;
if(i!=11)
cout<<",";
increment++;
}
return 0;
}
This code won't compile because you forgot to state that cout is in the std namespace (you forgot a usingnamespace std; or a std:: on line 13). Sorry to be like that.
Also, consider having an variable that per-iteration steps up one, and have an if statement that when true (will be false on the last iteration of the loop; determine what value that variable will have then and use <) prints that comma.
Also, I'm having troubles converting that code into FOR loop.
In for loop, the syntax is:
for (initialization;condition;change of state)
Where can I add the formula or the process? It cant be added in the change of state right?
int n = 1;
for(int i = 0; i < 11; ++i) /* usually, you initialize a counter variable to 0,
set a limit and increment it with each iteration */
{
// this is the loop's body; it's where we do the computations that will be iterated
n += i;
std::cout << n;
if(i == 5) n += 10;
if(i != 10) std::cout << ", ";
}
@Kyon: Yeah. I understand the pattern but the +10 thing makes it confusing for me.
Since the sequence keeps incrementing by a number (which keeps increasing with 1), we add them together all the time, however, this logic is broken on the 7th number, since 16 + 6 does not equal 32. We add 10 beforehand, so that the row is printed as desired.