The recFun() prints the digits of the number that is passed. So whatever number is passed, it prints the same number .
Answer a) The call recFun(268) prints 268
Answer b) To print the digits one on each line, we can
modify the function as follows-
int recFun(int x)
{
int sum = 0;
if(x > 0)
{
int sum = recFun(x/10) +
x%10;
}
else
sum = x;
cout << sum << endl;
}
Answer c) To print the sum of digits, the code can be
modified as follows-
int recFun(int x, int sum = 0)
{
if(x <= 9)
cout << x+sum <<
endl;
else
recFun(x / 10, sum+x%10);
}
2. Consider the following recursive function (Chapter 17, #9, modified) void recFun (int x) { if...
This is for C in Linux: Problem 1: Given the following recursive function: void recursiveFunction( int m ) printf("%d", m); if( m <= 0 ) return; if( n + 2 == 0 ) recursiveFunction( m - 1); else recursiveFunction( m - 2); What is the output when the following functions are called with these parameters? recursiveFunction( 5 ); recursiveFunction( 10 ); recursiveFunction( 0);
XCORE Question 1 Consider the following program void Elint x, int y Y = y + 1 cout<<x<<"*«y << endl; void nain) 1 int i, a13): all) = 15; a 2) - 203 a13) = 25; cout <i«"" <all) <<"" << a12) << ""« a[3] << endl; cout <i<** <all) << "" << a12) <<""« a[3] << endl; What values of the variable and array A are printed with the following rules. a parameters are passed by value bi parameters...
I am creating a recursive function in c++ and have the following code so far. However, can you help me fix it so that it doesn't crash when negative numbers are entered. If negative numbers are entered i want to let the user know negative numbers are not accepted and to try again #include <iostream> using namespace std; int multiplication(int x, int y) { int sum = y; //if value of x is 1, then result of x*y will be...
PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...
PROBLEM 4: Consider the recursive C++ function below: void foo(unsigned int n) { if(n==0) cout << "tick" << endl; else { foo(n-1); foo(n-1); foo(n-1); } } 4.A: Complete the following table indicating how many “ticks” are printed for various parameters n. Unenforceable rule: derive your answers “by hand” -- not simply by writing a program calling the function. n number of ticks printed when foo(n) is called 0 1 2 3 4 4.B:...
Can someone hand trace this basic recursive function? Please hand-trace and show me all the steps. I do not understand how this works. Expected output is: The output is: Message 3 Message 2 Message 1 Message 0 Message 0 is returning. Message 1 is returning. Message 2 is returning. Message 3 is returning. int main() message(3); return e; void message (int times) cout <"Message<< times<.n" if (times >e) message(times 1); else cout << "Message "<< times<< "is returning. n"
Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) { p = new node; p->key=x; p->left=NULL; p->right=NULL; } else { //Cheak if the element to be inserted is smaller than the root. if (x < p->key) { //call the function itself with new parameters. insert(x,p->left); } //cheak if the alement to be inserted...
Consider the following code. It is valid. What is printed? public static void TestLabel() { int i = 0; MyLabel: for (i++; i<20; i++) { if (i 10) continue MyLabel; System.out.println(i); } nothing (doesn't work) 20 10
3) Consider the following recursive method, what will be the output for the following method calls? Explain. (25 pts.) public static void sample (int number) if (number < 0) { System.out.println( superwriteVertical (-number); System.out.println(number); superwriteVertical (number/ 10); else if (number 10) else ( System.out.print1n(number % 18); sample(-100); sample(50); sample(1024);
The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() { char board[SIZE]; int player, choice, win, count; char mark; count = 0; // number of boxes marked till now initBoard(board); // start the game player = 1; // default player mark = 'X'; // default mark do { displayBoard(board); cout << "Player " << player << "(" << mark...