Question

In: Computer Science

void printList(double * A, int start, int end ) { if (start == end) //base case...

void printList(double * A, int start, int end )
{
        if (start == end) //base case
        {
                cout << A[start] << endl;
        }
        else //recursive case
        {
                cout << A[start] << endl;
                printList(A, start + 1, end);
        }
}

int main()
{
double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };
printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6
        cout << endl;
//HELP HERE: Using a recursive method on C++, reverse order of items in list
        //(No loops allowed)
        reverse(nums, 0, 12);
        printList(nums, 0, 12); //5.6 48 17 ... 2.14 13.8
        return 0;
}

Solutions

Expert Solution

#include <iostream>

using namespace std;
void printList(double * A, int start, int end )
{
        if (start == end) //base case
        {
                cout << A[start] << endl;
        }
        else //recursive case
        {
                cout << A[start] << endl;
                printList(A, start + 1, end);
        }
}

void reverse(double * A, int start, int end){
    if(start<end){
        double temp = A[start];
        A[start] = A[end];
        A[end] = temp;
        reverse(A,start+1,end-1);
    }
}
int main()
{
double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };
printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6
        cout << endl;
//HELP HERE: Using a recursive method on C++, reverse order of items in list
        //(No loops allowed)
        reverse(nums, 0, 12);
        printList(nums, 0, 12); //5.6 48 17 ... 2.14 13.8
        return 0;
}


Related Solutions

JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of...
binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of strings that have been sorted in order of increasing length, for ties in the string lengths they will be in alphabetical order. The method also takes in a search string and range of indexes (start, end). The method will return a String formatted with the path of search ranges followed by a decision (true or false), see the examples below. Example 1: binarySearchLengths({"a","aaa","aaaaa"},"bb",0,2) would...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT