Question
c++

page 2 ]. Write a C++ program that uses a recursive function to compute factorial (n!) Include comments in your code. dawoe l
0 0
Add a comment Improve this question Transcribed image text
Answer #1

QUESTION 7:

C++ Code:

#include <iostream> using namespace std 11 / Declaration of factorial function 12 long factorial(int); 10 13 int main() 14 15

27 /Function to calculate factorial using long factorial(int n){ if(n==0) Base case of factorial 28 recursion */ 29 30 31 ret

Output Snapshot:

Enter Number :5 Factorial of 5 is 120

Text Code:

#include <iostream>
using namespace std;
// Declaration of factorial function
long factorial(int);

int main()
{
// Integer variable to store value from the user
int num;
// Prompting user to enter the number
cout << "Enter Number :";
// Storing user input to num variable
cin >> num;
// Calculating the factorial and displaying the output using factorial function call
cout << "Factorial of " << num << " is " << factorial(num) << endl;

return 0;
}

/* Function to calculate factorial using recursion */
long factorial(int n){
if(n==0) // Base case of factorial   
return 1;
else
return n*factorial(n-1); // calling the factorial function recursively
}

Explanation:

  • Please refer code comments for the understanding purpose.

QUESTION 8 :

Function templates are generic type of function which can be operated. The template feature allows us in code reusability. It helps use to create a template of function that could be adapted to multiple type, which can be done without repeating the whole code of different datatypes :

Class templates are also generic type of class which can be operated by different datatypes. The programmer can create a template of the class which is adapted to multiple datatypes without writing the whole code of class again.

QUESTION 10

C++ Code:

#include <iostream> 10 using namespace std; 11 int main() 12 13 //Declaring and Initial ization of integer array of size 10 i

21 //Sorting the array 22 for(int i-0; i<(10-1); i++){ 23 for (int j-0; j<(10-i-1); j++){ 24 if(array0fInt[j] > arrayOfInt[j+

Output Snapshot:

Array Before Bubble Sorting 10 505 14 18 100 9 1 5 50 Array After Bubble Sorting : 9-5 1 5 10 14 18 50 50 100

Text Code:

#include <iostream>
using namespace std;

int main()
{
// Declaring and Initialization of integer array of size 10
int arrayOfInt[10] = {10, 50, -5, 14, 18, 100, -9, 1, 5, 50};
// Displaying Array values to user before applying bubble sort
cout << "Array Before Bubble Sorting :" << endl;
for(int i=0; i<10; i++)
cout << arrayOfInt[i] << " ";
cout << endl;
  
// Sorting the array
for(int i=0; i<(10-1); i++){
for(int j=0; j<(10-i-1); j++){
if(arrayOfInt[j] > arrayOfInt[j+1]){
int swap = arrayOfInt[j];
arrayOfInt[j] = arrayOfInt[j+1];
arrayOfInt[j+1] = swap;
}
}
}
// Displaying the array values after applying the bubble sort algorithm
cout << "Array After Bubble Sorting :" << endl;
for(int i=0; i<10; i++)
cout << arrayOfInt[i] << " ";
cout << endl;
  
return 0;
}

Explanation:

  • The program uses the Bubble Sort algorithm for sorting the integer array in ascending order.
  • Please refer code comments for the understanding purpose

Advantages of Bubble Sort:

  • The bubble sort algorithm is understandable very easily.
  • Implementation of the bubble sort is very simple and easy.
  • This sorting algorithm does not requires external memory to store the data for sorting purpose.
  • The performance of this sorting algorithm is great when array is almost sorted.

QUESTION 11:

When cirPtr is a pointer to a structure member element named radius, the expression a (*cirPtr.radius = 10) is invalid and expression b ((*cirPtr).radius=10) and c (cirPtr->radius = 10). In fact both, expression b and c are equal to each other.

As cirPtr is a pointer to the structure and (*cirPtr) represents the value at the pointer addressing by cirPtr, hence (*cirPtr).radius represents the value of radius member which is pointed by cirPtr pointer. The alternate method to write it is, (cirPtr -> radius). Hence

(*cirPtr).radius is equivalent to cirPtr->radius.

Add a comment
Know the answer?
Add Answer to:
c++ page 2 ]. Write a C++ program that uses a recursive function to compute factorial...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ //Write prototype for function factorial that accepts an int num and returns an int /*...

    C++ //Write prototype for function factorial that accepts an int num and returns an int /* WITH LOOP OF YOUR CHOICE: Write code for function factorial that accepts an int num and returns the num's factorial factorial(5); 1*2*3*4*5 returns 120 DON'T FORGET TO WRITE TEST CASE. */ //write includes statements //write using statements for cin and cout /* Use a do while loop to prompt the user for a number, call the factorial function, and display the number's factorial. Also,...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • C Programming 6 1 point Which function(s) can be used to open a file? fread fopen...

    C Programming 6 1 point Which function(s) can be used to open a file? fread fopen fscanf fstream file.open fopens 7 1 point Which function(s), is/are used to write text to a file? fprintf printf write fseek fwrite 8 1 point Which preprocessor directive is used to make a macro? #define #ifdef #include #macro #directive 1 point You cannot write the field values of a structure variable to a file. True False 10 1 point You can use the standard...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • In the language c using the isspace() function: Write a program to count the number of...

    In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters. Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Run your program using the following three sets of input:             1. You're traveling through ​               another...

  • PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

    PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). : Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • 1. [2 points] Write a MIPS assembly language program of the following C function and the...

    1. [2 points] Write a MIPS assembly language program of the following C function and the code to call the function: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; مهه Arguments g, h, i, and j are passed to the function in registers $a0, $al, Şa2, and $a3, respectively while f in $50 (hence, need to save $50 on stack), and the result is to be stored...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT