Loop problem

Pages: 12
I find it hard to make a loop code.
Our instructor made us print out the following numbers using loop:
1,2,4,7,11,16,32,39,47,56,66

Any tips on making a loop code? Why am I finding it so difficult?
Are you supposed to calculate those values or can you store them in a container?
You should recognize the following pattern:
0 + 1 = 1
1 + 1 = 2
2 + 2 = 4
3 + 4 = 7
4 + 7 = 11
5 + 11 = 16
+10
6 + 26 = 32
7 + 32 = 39
8 + 39 = 47
9 + 47 = 56
10 + 56 = 66
Build your loop from here.
Last edited on
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;
}
Last edited on
@Async: Can't understand that code. We're using the old MS VS 6.0 version.

@Kyon: Yeah. I understand the pattern but the +10 thing makes it confusing for me.

@PanGalactic: Wat? Uhh.. we're just supposed to show those numbers.
Last edited on
the previous code I gave u is wrong,now I 've corrected it.
Can you convert it into a older syntax?
Hmm? Older syntax? Does this mean you want the code translated to C?

@Async: Please refrain from giving out full solutions.

-Albatross
Async, we usually don't hand out homework assignment solutions like that. But since we're at it, you can simplify that. This is how I'd do it:

1
2
3
4
5
6
7
8
int n = 1;
for(int i = 0; i < 11; ++i)
{
    n += i;
    std::cout << n;
    if(i == 5) n += 10;
    if(i != 10) std::cout << ", ";
}
Just give me the simple logic.

By the way, I made this code to print out fibonacci sequence:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream.h>
int main ()
{
    int a, b, c;
    a=0;
    b=1;   

    while (a<144)
    {
    c=a+b;
    a=b;
    b=c;
    cout << a <<", ";
    }
    system ("PAUSE");
return 0;
}


How can I remove the comma at the end?
This code won't compile because you forgot to state that cout is in the std namespace (you forgot a using namespace 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.

-Albatross
Last edited on
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?
1
2
3
4
for(initialization;condition;C.O.S.)
{
    //Larger changes of state. The C.O.S. is generally used for increasing a variable per iteration.
}


-Albatross
What C.O.S. are you talking about?
From my example up there:

1
2
3
4
5
6
7
8
9
10
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 << ", ";
}
Thanks.
What does that += thing does? I never encountered that before.
It's a compound assignment operator. Saying n += i; is equivalent to saying n = n + i;
it means n = n + 10;
kielmaru wrote:
@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.
kielmaru wrote:
@PanGalactic: Wat? Uhh.. we're just supposed to show those numbers.


You know the list of numbers. Don't bother computing them. Store them in an array and print them.

1
2
3
4
5
6
7
8
9
const int numbers[] = {1,2,4,7,11,16,32,39,47,56,66};
const size_t count = sizeof(numbers) / sizeof(int);  // computing the size of the array.

for (size_t i=0; i != count; i++)
{
    std::cout << numbers[i];
    if (i != count - 1) std::cout << ",";
}
std::cout << std::endl;


Here's another way to code a loop to print the numbers without calculating the values:

1
2
3
4
5
6
7
8
9
const int numbers[] = {1,2,4,7,11,16,32,39,47,56,66,-1};  // using a sentinel value.
size_t i = 0;

while (numbers[i] >=0)
{
    std::cout << numbers[i++];
    if (numbers[i] >= 0) std::cout << ",";
}
std::cout << std::endl;
Pages: 12