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 C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
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...
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...
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”...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
An array of characters contains a few letters. Write a complete C program that will display...
An array of characters contains a few letters. Write a complete C program that will display the output as shown below. Lets assume the array contains =”abcde”. The program should be able to work with any array size, configurable in the program. Expected output Original array = [ a b c d e] a a b a b c a b c d a b c d e
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,...
Please provide answer in the format that I provided, thank you Write a program that prompts...
Please provide answer in the format that I provided, thank you Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets...
c++ 19.36 LAB: Output values below an amount - functions Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, and output all integers less than or equal to that value. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75 The 5 indicates that there are five integers...