Help understand for block

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>
using namespace std;
int main()
{
	
const int numsToCalculate = 5;
	cout << "This program will calculate " << numsToCalculate \
		<< " Fibonacci Numbers at a time" << endl;

	int num1 = 0, num2 = 1;
	char wantMore = '\0';
	cout << num1 << " " << num2 << " ";

	do
	{
		for (int counter = 0; counter < numsToCalculate; ++counter)
		{
			cout << num1 + num2 << " ";

			int num2Temp = num2;
			num2 = num1 + num2;
			num1 = num2Temp;
		}

		cout << endl << "Do you want more numbers (y/n)? ";
		cin >> wantMore;
	} while (wantMore == 'y');

	cout << "Goodbye!" << endl;

	return 0;


I dont understand the for block (the code inside the for statement). Please Help!
You dont understand this part:

for (int counter = 0; counter < numsToCalculate; ++counter)

or this part:

1
2
3
4
5
6
7
{
        cout << num1 + num2 << " ";

        int num2Temp = num2;
        num2 = num1 + num2;
	num1 = num2Temp;
}


?
I take it that you've actually googled what " Fibonacci Numbers" are, and how they're calculated.

It's a cack-handed way of doing the code TBH.
The temp variable is completely unnecessary if the statements are ordered properly.
1
2
3
4
int nextFib = num1 + num2;
cout << nextFib << " ";
num1 = num2;  // F(n-1) becomes F(n-2)
num2 = nextFib; // the new F(n-1) 

You still used a "temp" variable, you just called it nextFib instead of num2temp :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
	const size_t numsToCalculate {10};

	std::cout << "This program will calculate " << numsToCalculate \
		<< " Fibonacci Numbers at a time\n";

	size_t num1 {}, num2 {1};
	char wantMore {};

	do {
		for (size_t counter {}; counter < numsToCalculate; ++counter) {
			std::cout << num1 << ' ';
			num2 += num1;
			num1 = num2 - num1;
		}

		std::cout << "\nDo you want more numbers (y/n)? ";
		std::cin >> wantMore;
	} while (wantMore == 'y');
}

I agree its not ideal. but that aside:

1
2
3
4
5
6
7
8
//lets say num 1 is 1 and num2 is 2 for example:
cout << num1 + num2 << " ";  //print the sum of num1 and num2, which is 3
int num2Temp = num2;   // its 2
num2 = num1 + num2;  //now num2 is 3
num1 = num2Temp;     //now num 1 is 2 from above.   this is similar to the classic 'swap' 
//algorithm with a twist that the sum is used.  so next itereation num1 is 2 and num2 is 3. 
// that produces sum 5 and so on.  


ok... so now, the interview question: how to swap 2 values without a temporary?
one way is addition:
x = x + y;
y = x - y;
x = x - y;

but, looky, x = x+y is sort of what we needed!
hmm so how about:

cout << num1 + num2 << " "; //print the sum of num1 and num2, which is 3
num2 = num1 + num2;
num1 = num2Temp-num1;

which is what they did above, but now you can see where it came from
Last edited on
How does it cycle through 5 numbers I still dont get it
Whats the purpose of

cout << num1 << " " << num2 << " ";
And why doesnt

num2 = num1 + num2;
num1 = num2Temp;

have "int" in front of them
Can I actually get help with the entire program? Ugh...I feel like giving up
Last edited on
int apple; declares a variable
int apple = 42; declares a variable and initializes it to 42.

apple = 100; is assigning 100 to an existing variable called apple.

If you tried to do something like:
1
2
int apple = 42;
int apple = 100 + apple;
Then you would just get an error, because you're re-declaring apple.
Last edited on
How does it cycle through 5 numbers I still dont get it

const int numsToCalculate = 5;
for (int counter = 0; counter < numsToCalculate; ++counter)
this loop does these values of counter: {0, 1, 2, 3, 4 } if you count them, there are 5 numbers in there. When counter reaches 5, it is no longer LESS THAN 5 (nums to calculate is 5) so it stops. ++ means add 1 every time through the loop. you can put anything in there, you can say += 3 and it would do 0, 3, and stop because 6 is not < 5 again.

int declares a variable. Creates may be a better word to get you started. You only need to 'create' something one time, after that, you have it available to use.

It seems like you jumped right into a fairly complex problem with no background. You may need to back up and do a few very simple programs first. First understand what a variable is and how to use it, and how to write a simple loop like printing 0-10, work your way up. And, can't say it enough times, the code you found is a slightly odd way of doing the problem anyway.
Last edited on
Whats the purpose of

cout << num1 << " " << num2 << " ";


It displays the first 2 numbers (0, 1)

And why doesnt

num2 = num1 + num2;
num1 = num2Temp;

have "int" in front of them


Because they are changing the values of existing variables - not defining new ones.

To learn about C++, have a look at https://www.learncpp.com/
Last edited on
int declares a variable. Creates may be a better word to get you started. You only need to 'create' something one time, after that, you have it available to use.

Available within that scope.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{ // scope A starts
   size_t num1 {}, num2 {1};

  for (int counter = 0; counter < 5; ++counter) // scope B starts
  { // scope C starts
    cout << num1 + num2 << " ";

    int num2Temp = num2;
    num2 = num1 + num2;
    num1 = num2Temp;
  } // scope C ends.  num2Temp disappears here on every iteration
  // scope B ends when loop ends. counter disappears

} // scope A ends. mun1 and num2 disappear 
It displays the first 2 numbers (0, 1)

Why does it need to do that. Isnt that what the for loop is for?
Lets run your program:
This program will calculate 5 Fibonacci Numbers at a time
0 1 1 2 3 5 8 
Do you want more numbers (y/n)? y
13 21 34 55 89 
Do you want more numbers (y/n)? n
Goodbye!

How many numbers are printed on the first line? 7
On the second? 5
What are the first 12 Fibonacci numbers?
0 1 1 2 3 5 8 13 21 34 55 89

Each for-loop appends five numbers to the sequence.

What is the very first number printed by the for-loop?
cout << num1 + num2 << " ";
That (num1+num2) is not the very first value of the serie (0, which is in num1 at start) nor the second (1, which is in num2 at start), but the next number after those.

An iteration of the loop does not print earlier numbers. It prints only the next number.
Topic archived. No new replies allowed.