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:
Please enter size: 5
Please enter elements: 1.5
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:
Please enter size: 8
Please enter elements: 91
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

ANSWER ALL PARTS OTHERWISE DONT ANSWER PLEASE AND DO ADD COMMENTS

Homework Answers

Answer #1

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

ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT

QUESTION 1:

1.CODE SCREENSHOT:

2.OUTPUT:

3.CODE:

#include<iostream>
using namespace std;
int main(){
        int n,i;
        //READ THE NUMBER OF ELEMENTS 
        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;
   cout<<"Please enter size : ";
   cin>>n;
   // declares a float array dynamically 
   float *num=new float[n];
   cout<<"Please enter elements : ";
   //
   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;
        cout<<"Please Enter Charactrers(q-to Quit):"<<endl; 
        //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;
        //read the input
        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;
        //read the array size
        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;
        //READ THE NUMBER OF ELEMENTS 
        cout<<"Please enter size:  : ";
        cin>>size;
        //DYNAMICALLY ALLOCATE THE ARRAY
        int *arr=new int[size];
        cout<<"Please enter elements:";
        for(i=size-1;i>=0;i--)
                cin>>arr[i];
        int *arr1;
        reduceArray(arr,arr1,size);
        
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
Write a C program to combine two arrays of the same size arranged in order descendant....
Write a C program to combine two arrays of the same size arranged in order descendant. Test data : Enter the number of elements to be stored in the first array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Enter the number of elements to be stored in the second array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Expected output: The combined array...
Write code in JAVA Write a program that will output a right triangle based on user...
Write code in JAVA Write a program that will output a right triangle based on user specified input height (int) and specified input symbol (char).The first line will have one user-specified character, such as % or *. Each subsequent line will have one additional user-specified character until the number in the triangle's base reaches specified input height. Output a space after each user-specified character, including after the line's last user-specified character. Hint: Use a nested for loop. Ex:If the input...
Write a c++ program that evaluates the first 50 numbers of the Fibonacci sequence and store...
Write a c++ program that evaluates the first 50 numbers of the Fibonacci sequence and store the numbers in an array. The c++ program asks the user for an input to enter a positive integer 'n' and display the nth fibonacci number "FibN" that is stored inside of the array
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h andRun.cpp to separate class header and implementation. In this program, we are going to create a small scale Telivision Management System. A) Create a class Episode with following variables: char* episode_name, char* episode_venue, char episode_date[22] and char episode_time[18]. Input format for episode_date: dd-mm-yyyy Input format for episode_time: hh:mm am/pm B) Implement default constructor and overloaded constructor. Print “Default Constructor Called” and “Overloaded Constructor Called”...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT(...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT( TRUE/ FALSE) QUIZ 8 Array Challenge Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the string true if any two numbers can be multiplied so that the answer is greater than double the sum of all the elements in the array. If not, return the string false. For example: if arr is [2, 5, 6, -6, 16, 2,...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...