Question

# WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...

WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question,

DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER

QUESTION:

1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers.
For example:
For n=10
Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

2):
Write a C++ program that declares and initializes a float array dynamically and finds the index of the first occurrence of the second largest element in the array.

For Example:

Input:
7.8
3.2
9.0
7.1

Output:
Second Largest element is: 7.8
Index of second largest element is: 1

3):
Write a C++ program that keeps taking character input from the user until user enters q and displays the data in reverse order.
Your program should save the input in a dynamically allocated array. Initially create a dynamic array of five characters. Each time the array gets filled your program should double the size of array and continue taking the input. After receiving q (i.e. end of data input) your program should print the characters in the reverse order as entered by the user.
You have to make use of the following functions for this task:
void Input (char * & carr, int & size);    //why is size passed by reference for this?
void reverse (char * carr, int size);
void Output (char * carr, int size);

4): WRITE C++ PROGRAM BY
Take size input from the user and create an array of that size. Now populate the array as well by taking input from the user.
First Implement void copyArray(int* arr, int *&arr1, int size) that copies arr into arr1.
Now implement another function int reduceArray(int *arr, int *&arr1, int size) that asks user to enter size to reduce the array. To reduce the array remove the elements of the arr from the start and copy remaining into arr1. Use copyArray function to copy.

For Example:

Input:
5
3
40
7
8
12
642
Please enter the reduced size of array: 5

Output:
Array after reduction is: 40
7
8
12
642

I have implemented questions (1-4) as per the description.
PLEASE FIND THE FOLLOWING CODE SCREENSHOT, OUTPUT, AND CODE FOR EACH QUESTION.

QUESTION 1:

1.CODE SCREENSHOT:

2.OUTPUT:

3.CODE:

``````#include<iostream>
using namespace std;
int main(){
int n,i;
cout<<"Enter a value for 'n' : ";
cin>>n;
//DYNAMICALLY ALLOCATE THE ARRAY
int *a=new int[n];
//SAVE THE FIRST TWO Fibonacci NUMBERS
a[0]=1;
a[1]=1;
//COMPUTE THE SUBSEQUENT NUMBERS INTO ARRAY
for(i=2;i<n;i++)
a[i]=a[i-1]+a[i-2];
//DISPLAY THE RESULT
cout<<"Fibonacci Numbers : "<<a[0];
for(i=1;i<n;i++)
cout<<","<<a[i];
}``````

QUESTION 2:

1.CODE SCREENSHOT:

2.OUTPUT:

3.CODE

``````#include <iostream>
using namespace std;
int main(){
int n;
float largest,second;
int largestIndex=0,secondLargestIndex=0;
cin>>n;
// declares a float array dynamically
float *num=new float[n];
//
for(int i=0; i<n; i++){
cin>>num[i];
}
/*Compare the first two element and find the
largest,second(largest) and their indeces
*/
if(num[0]<num[1]){
largest = num[1];
largestIndex=1;
second = num[0];
secondLargestIndex=0;
}
else{
largest = num[0];
largestIndex=0;
second = num[1];
secondLargestIndex=1;
}
for (int i = 2; i< n ; i ++) {
/* If the current array element is greater than largest
* then the largest is copied to "second" and the element
* is copied to the "largest" variable.
*/
if (num[i] > largest) {
second = largest;
secondLargestIndex=largestIndex;
largest = num[i];
largestIndex=i;
}
/* If current array element is less than largest but greater
* then second largest ("second" variable) then copy the
* element to "second"
*/
else if (num[i] > second && num[i] != largest) {
second = num[i];
secondLargestIndex=i;
}
}
cout<<"Second Largest Element is: "<<second<<endl;
cout<<"Index of second largest element is"<<secondLargestIndex;
return 0;
}``````

QUESTION 3:

1.CODE SCREENSHOT:

2.OUTPUT:

3.CODE

``````#include <iostream>
using namespace std;
void Input (char * & carr, int & size){
int i=0,n=size,j;
char *temp;
char ch;
//read a character and ignore enter or any other character
cin.get(ch);
cin.ignore();
//loop until we enter 'q'

while(ch!='q'){
//if the array is full
//then reallocate the array
if(i==n){
n=n*2;
temp=carr;
carr=new char[n];
for(j=0;j<i;j++)
carr[j]=temp[j];
}
//save the char into array
carr[i++]=ch;
//cout<<ch<<endl;
cin.get(ch);
cin.ignore();
}
size=n;
}
void reverse (char * carr, int size){
char ch;
int i,j;
//swap the first and last elements
//and increment i,decrement j
for(i=0,j=size-1;i<=j;i++,j--)
{
ch=carr[i];
carr[i]=carr[j];
carr[j]=ch;
}
}
void Output (char * carr, int size){
//Display using the for loop
for(int i=0;i<size;i++)
cout<<carr[i]<<" ";
}
int main(){
//create a dynamic array of 5 characters
char *carr = new char[5];
//to hold number of elements variables
int size=5;
Input(carr,size);
cout<<"Given Input : "<<endl;
//display text before reverse
Output(carr,size);
//reverse the array
reverse(carr,size);
cout<<endl<<"Array After reverse :"<<endl;
//display the array after reverse
Output(carr,size);
}``````

QUESTION 4:

1.CODE SCREENSHOT:

2.OUTPUT:

3.CODE

``````#include<iostream>
using namespace std;
void copyArray(int* arr, int *&arr1, int size){
int i,j;
//Declare the array of required size
arr1=new int[size];
//copy the array
for(i=0,j=size-1;i<size;i++,j--)
arr1[i]=arr[j];
}
//the reduce array
int reduceArray(int *arr, int *&arr1, int size){
int size1,i;
cout<<"Please enter the reduced size of array : ";
cin>>size1;
//call the copy method
copyArray(arr,arr1,size1);
//print the array
cout<<"Array after reduction is:";
for(i=0;i<size1;i++)
cout<<arr1[i]<<endl;

}
int main(){
int size,i;
cin>>size;
//DYNAMICALLY ALLOCATE THE ARRAY
int *arr=new int[size];
for(i=size-1;i>=0;i--)
cin>>arr[i];
int *arr1;
reduceArray(arr,arr1,size);

}``````