The fibonacci series went wrong?

we were asked to write a program to generate a number of fibonacci as homework. so I wrote a program that generates the first n Fibonacci numbers. Here is my frist code that works fine.

# include <stdio.h> void main() { int a = -1, b = 1, c = 0, i, n, sum = 0 ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThefibonacci series is : \n\n") ; for(i = 1 ; i <= n ; i++) { c = a + b ; printf("%d \t", c) ; b=c; a=b; } } 

so I tried various combinations and I found out that my code would work well if I changed the 12th and 13th lines. i.e

 # include <stdio.h> void main() { int a = -1, b = 1, c = 0, i, n, sum = 0 ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThefibonacci series is : \n\n") ; for(i = 1 ; i <= n ; i++) { c = a + b ; printf("%d \t", c) ; a=b; b=c; } } 

This is the same logical right. why does the first code give me the wrong output?

What are segmentation errors? (my compiler often tells me that there are segmentation errors in my code)

PS-i is a beggar. Only three weeks in c-language, and we learn about the cycles.

+4
source share
5 answers

Lines are executed in order, so in the first example, b becomes c until a becomes b , you assign c both a and b , creating some kind of exponential series (but zeros) instead of a fibonacci sequence.

A segmentation error means that your program accesses memory somewhere where it is not allowed to access memory, usually because you are looking for the wrong pointer or accessing the array outside of the bounds.

+1
source

Ordering operators matters.

 b = c; a = b; 

When this is done, b and a will both be equal to the original value of c , and the old value of b will be lost. This is probably not what you wanted.

 a = b; b = c; 

When this is done, a will correspond to the old value of b , and b will be equal to the original value of c .

+4
source

In the Fibonacci series, a new number is generated as the sum of the previous two numbers.

Suppose the previous two numbers were A and B , and the newly generated number C Now for the next iteration, you need to forget A , and B and C are your new previous numbers.

To make B and C you new A and B , you need to do:

 A = B // B becomes the new A B = C // C becomes the new B 

what are you doing:

 B = C // C becomes the new B, but you've not saved the old value of B!!! A = B // Old value of B gone..B is now C, which is assigned to A 
+2
source

Take a look at this separately:

  c = a + b ; printf("%d \t", c) ; b=c; a=b; 

The value of both a and b after doing this will be c .

  c = a + b ; printf("%d \t", c) ; a=b; b=c; 

If you reorder the instructions, a gets the old value of b , and b gets the new value of c .

+1
source

This is the same logical right. why does the first code give me the wrong output?

Have you ever wondered why

 printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThe fibonacci series is : \n\n") ; 

the first exits Enter the limit , then it waits for the number to be entered, then The fibonacci series is come out - in this particular order?

Why not the other way around or all at the same time?


What are segmentation errors?

A simple Google search would give you a ton of explanations. This means that you have access to memory that does not belong to you.

+1
source

Source: https://habr.com/ru/post/1445207/


All Articles