Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

E COMP 1224 Sec. 1 Spring 2017 Lab 1. I need the solution for question 2 only pl

ID: 3789195 • Letter: E

Question

E COMP 1224 Sec. 1 Spring 2017 Lab 1. I need the solution for question 2 only please. Thank you

I need the solution for question 2 only please. Thank you

COMP 1224 Sec. 1 Spring 2017 Lab 1 Your first lab will be similar to the examples discussed in class, covering programming hanction, and recursion. It includes the following tasks Using CdC write loop(s) to control and output a star pattern as follows: giving a integer N (use cin to assign N), first, number in each row equals the row you need to print a triangle with N rows. the star stars). Then number (e.g the 1" row has 1 star, the N row has triangle output a reverse triangle with (N-1) row, the first row of the reverse center has (N-1) stars, and the last row of the reverse triangle has 1 star. All stars are and the pattern is a diamond shape. Eg when N is 3, you will print the following star pattern Hint nested loop is needed to finish this 2. a. In main declare a variable N. input an integer (positive and less than 30) from standard input (keyboard): based on the value of a to the N Fibonacci number and print the result. b. Define a recursive function to calculate Fibonacci numbers. In main0, assign N with an integer (positive and than 20) from standard input. Based on the value of call the recursive function and output the Nun Fibonacci number: Fibonnacci (N) Fibonacci (N-1) Fibonacci (N-2) Note: The first two Fibonacci numbers are given 1 and as 1. Each Fibonacci number is the sum of its preceding two numbers, see above equation. The following specifications will be expected for each of your projects in this class: 1. An empty project file must be created first, and then you will create new source file and header files. 2. Add comments at the beginning of the program, and add description for each function, loop, etc. urn in both the hardcopy and softcopy of your source code, including all .cpp and .h files. Your name, your ID number and HW number should be written on the upper-right corner of the first sheet of the hardcopy. Note that you are require to submit your softcopy to assignment 1 (a1, a2) in Taskstream. Due: Wednesday, Feb. 8, 2017

Explanation / Answer

2 .a) without recursion

#include<iostream> // header file
using namespace std;
int main()
{
int n; //declaring variable
cout<<"enter the value of n"<<" "; //taking the input from user
cin>>n;
int value1=1,value2=1,value3; //initialising first two values to "1"
if(n==1||n==2)
{
cout<<1;
}
else
{
for(int i=2;i<=n;i++) // calcuating the nth fibbonaci number
{
value3=value1+value2;
value1=value2;
value2=value3;
}
cout<<value3; //printing out the value
}
}

=========================================================================================

2. b) with recursion


#include<iostream> // header file
using namespace std;
int n1=1,n2=1,n3=0;// initalising first two values to "1" globally
void printFibonacci(int count)
{
if(count>=0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;

printFibonacci(count-1); // recursive function call
}
}
int main()
{
int n; //declaring variable to store user input
cout<<"enter the value of n"<<" "; //taking the input from user
cin>>n;
if(n==1||n==2)
{
cout<<1;
}
else
{
printFibonacci(n-2);// calculating the nth number
cout<<n3; // printing the output
}