Part I (1.5 points) Write a C++ program to compute the Fibonacci numbers. In mat
ID: 652320 • Letter: P
Question
Part I (1.5 points)
Write a C++ program to compute the Fibonacci numbers. In mathematics, the sequence of Fibonacci numbers is defined by the following recurrence relation.
Fn = Fn-1 + Fn-2, where F0 = 0 and F1 = 1
For example, if n = 3, then F3 = F2 + F1 = (F1 + F0) + F1 = (1 + 0) + 1 = 2.
Your program should contain a void function having two parameters, in which the first parameter (a value parameter) is n passed from the main function to it and the second parameter (a reference parameter) is the result passed back to the main function. The main function is to handle user input for n and pass it to the function described above. The main function is also responsible for displaying the result to the screen. The following show a sample run of the program.
Enter a nonnegative number: 3
The Fibonacci number for n = 3 is 2
Part II (2 points)
Write a C++ program that calculates the average of three test scores. The program should contain two value-returning functions (main and CalcAverage) and two void functions (GetTestScores and DisplayAverage). The main function should call the void GetTestScores function to get three test scores. The test scores may contain a decimal place. The main function should then call the value-returning CalcAverage function to calculate and return the average of the three test scores. When the CalcAverage function has completed its task, the main function should call the void DisplayAverage function to display the average of the three test scores on the screen. Display the average with one decimal place. The following shows a sample run of the program.
Enter three test scores: 78.5 91.6 83.4
The average of the three test scores is 84.5
Explanation / Answer
Part - 1
#include<iostream.h>
#include<conio.h>
rec_fibo(int);
void main()
{
clrscr();
int n,i;
cout<<"Enter the total elements in the series : ";
cin>>n;
cout<<" The Fibonacci series is: ";
for(i=0;i<n;i++)
{
cout<<rec_fibo(i)<<" ";
}
getch();
}
rec_fibo(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return rec_fibo(n-1)+rec_fibo(n-2);
}
Part -2
#include<iostream.h>
#include<conio.h>
class a
{
public:
float f1,f2,f3,avg;
void GetTestScores()
{
clrscr();
cout<<"Enter 3 test score";
cin>>f1>>f2>>f3;
}
void CalcAverage()
{
avg=(f1+f2+f3)/3;
}
void DisplayAverage()
{
cout<<"Agerage is"<<avg;
}
};
void main()
{
a o;
o.GetTestScores();
o.CalcAverage();
o.DisplayAverage();
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.