Please answer the queston (in terms of C programming) and explain answer End of
ID: 3905889 • Letter: P
Question
Please answer the queston (in terms of C programming) and explain answer
End of Semester 1, 2016 COMP1004 Engineering Programming l(b) The following C program contains three (3) logical errors that will not cause errors on compilation but will not produce the right answer. Identify and correct these errors by stating the line number where they occur [3 marks each, 6 marks in total] 10 20 #include / In Mathematics, the Eibonacci number series have the following form: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 40 By definition, the i:st Fibonacci numbers are 0 and 1, and each 60 subsequent number is the sum of the previous two numbers. This 0 program computes che iist n Fibonacci sequence where n is inputted 90 100 int main (voial 110f 120 130 140 150 160 170 180 190 200 210 220 230 ord fibonacci (int n) 240 250 260 int r prinHow many numbers of the sequence would you like? " . Here xe cail the fibonacci function "/ :n:a0: 1/ initialise first number of the fibonacci sequence in: E # 0; // initialise second number of the fibonacci sequence 280 290 300 3:0 32 printe("sdin",a); suma bi 0Explanation / Answer
The errors are in lines 20,260 and 340
20: Declare function fibonacci(int n)
260: Initialise b=1, First two numbers in fibonacci sequence is 0 and 1. Hence a=0 and b=1
340: Assign b=sum. In fibonacci sequence subsequent number is the sum of previous two numbers.
Corrected Program
#include<stdio.h>
void fibonacci(int n);
int main(void) {
int n;
printf("How many numbers of the sequence would you like?");
scanf("%d",&n);
fibonacci(n);
return 0;
}
void fibonacci(int n)
{
int a=0;
int b=1;
int sum=0;
int i;
for(i=0;i<n;i++)
{
printf(" %d",a);
sum=a+b;
a=b;
b=sum;
}
}