Question

In: Computer Science

This C++ program will use arrays, recursion, and pass by reference to calculate 02, 12, 22,...

This C++ program will use arrays, recursion, and pass by reference to calculate 02, 12, 22, 32, 42 and then print a message.


Write a driver program (main) that includes the following:


An array class (not a built-in-array) of 5 integers initialized to 0,1,2,3,4 the bases


An integer initialized to 2 to hold the exponent


A string initialized to “Good Job!\n”;


A for loop that calls the recursive pow function with all of the bases and the exponent.


Call the print function with the string using pass by reference by passing the address of s.


You must use the array class (not built-in-arrays) for full credit.


Write a pow function that implements raising a base to a power. The function takes an int base and an int exp. It returns an int which is baseexp. You must use recursion for full credit.


Base case: base1 = base


Recursive case: baseexp = base * baseexp-1


Write a print function that takes a string pointer and uses the indirection operator (*) to print the contents of the string to the screen. This function does not return anything. This function must use pass by reference with pointers for full credit.


Solutions

Expert Solution

Code in C++ with comments (code to copy)

// This C++ program will use arrays, recursion, and pass by reference to calculate 0^2, 1^2, 2^2, 3^2, 4^2 and then print a message.
#include <bits/stdc++.h>
using namespace std;
// Write a pow function that implements raising a base to a power. The function takes an int base and an int exp. It returns an int which is baseexp. You must use recursion for full credit.
int pow(int base, int exp){
        if(exp==1)
                // Base case: base1 = base
                return base;
        else{
                // Recursive case: baseexp = base * baseexp-1
                return base*pow(base, exp-1);
        }
}
// Write a print function that takes a string pointer and uses the indirection operator (*) 
//to print the contents of the string to the screen. This function does not return anything. 
//This function must use pass by reference with pointers for full credit.
void print(string *msg){
        cout<<*msg;
}

// Write a driver program (main) that includes the following:
int main(){
        // An array class (not a built-in-array) of 5 integers 
        // initialized to 0,1,2,3,4 the bases
        // You must use the array class (not built-in-arrays) for full credit.
        array<int,6> arr = {0, 1, 2, 3, 4}; 
        //  An integer initialized to 2 to hold the exponent
        int exp = 2;
        //  A string initialized to “Good Job!\n”;
        string greet = "Good Job!\n";
        //  A for loop that calls the recursive pow function with all of the bases and the exponent.
        for(int i=0;i<5;i++){
                arr[i]=pow(arr[i], exp);
        }
        //  Call the print function with the string using pass by reference by passing the address of s.
        print(&greet);
}

Code Screenshot

Console Output Screenshot

Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.


Related Solutions

Write a c program to calculate the factorial of a number using recursion    [8] Question five...
Write a c program to calculate the factorial of a number using recursion    [8] Question five Write a stack algorithm to POP an item                                                         [6] What does FRONT and REAR signify in a queue?                                                                 [6] Write an algorithm for a dequeue operation                                                                       [8]
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
A local instructor wants you to write a c++ program using arrays to calculate the average...
A local instructor wants you to write a c++ program using arrays to calculate the average score made on exams by her students. For simplicity, she always has only 12 students in each course she teaches. She teaches multiple subjects so she would like to enter the name of the exam. She wants the program to also determine the highest and lowest scores and the number of students who passed and failed the exam. A score of 60 or above...
1.Write a C++ program using control structures, arrays, functions to calculate the number of dollars, quarters,...
1.Write a C++ program using control structures, arrays, functions to calculate the number of dollars, quarters, dimes, nickels, and pennies in a given amount of money. The input should be a floating-point value representing a decimal value. Example: 63.87 à 63 [dollars and 87 cents] should output 63 dollars, 3 quarters, 1 dime, 0 nickels, and 2 pennies. 2. In trigonometry the cosine function can be calculated using cos(x) = 1 – x 2 /2! + x 4 /4!- x...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP...
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP ( 0.00 - 1.49 -> fail 1.50 - 4.00 -> pass ) 1b) Write a program in C to display month in Islamic Calendar.
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which will be used in parallel. Create a program that keeps track of the sales of BBQ sauces for a company. The company makes several different types of sauces, Original, Sticky Sweet, Spicy, Sweet Heat, Hickory Bourbon and Smokey Mesquite. One array will contain the names of the different BBQ sauces. This array will be initialized from a text file with the 6 different names....
An excellent example of a program that can be greatly simplified by the use of recursion...
An excellent example of a program that can be greatly simplified by the use of recursion is the Chapter 4 case study, escaping a maze. As already explained, in each maze cell the mouse stores on the maze stack up to four cells neighboring the cell in which it is currently located. The cells put on the stack are the ones that should be investigated after reaching a dead end. It does the same for each visited cell. Write a...
For C program, convert the 1-D stencil program from lab03 to use array reference (B[I]) to...
For C program, convert the 1-D stencil program from lab03 to use array reference (B[I]) to access array element instead of using pointers. The C program follows these steps: 1) declare two arrays, each has 100 elements; 2) use a for loop to randomly generate 100 integers and store them in one array; 3) use another for loop to do the 1-D stencil and store the result in the other array;
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR...
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR PROGRAM INSTRUCTIONS PLEASE SEE BELOW) PROGRAM 3 Adjust program II to make use of functions. All the same rules from the previous program specifications still apply, for example input gathering, output formatting and breaking on -1 still apply. Additional requirements. Write a function that prompts the user for hours worked, rate and name. Use parameter passing, and pass by reference. Write a function that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT