PROBLEM: Write a recursive method named AddToN that adds up all the numbers between 1 and an integer N. Please add comments to every line of code.
EXISTING CODE:
void printStars (int n)
{
if (n==0 ) {
//base case
cout <<"";
}
else{
printStars(n-1);
cout << "";
}
}
}
int sumRange ( int min, int max)
{
if (min == max){
//base case
return min;
}
else{
//recursive case
return max + sumRange(min,max-1);
}
}
}
int sumDigits(int n)
{
int rightDigit = n%10;
int remainDigits = n / 10;
if(n < 10)
{
// Base case
return n;
}
else
{
return sumDigits(remainDigits) + rightDigit;
}
}
#include <iostream>
using namespace std;
int AddToN(int n) {
if (n == 0) { // base case
return 0;
} else { // recursive case
return n + AddToN(n-1); //make recursive call
}
}
int main() {
int n;
cout << "Enter a value for n: ";
cin >> n;
cout << "Sum of numbers from 1 to " << n << " is " << AddToN(n) << endl;
return 0;
}
PROBLEM: Write a recursive method named AddToN that adds up all the numbers between 1 and...
PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their sum. Please comment on every line of code. EXISTING CODE: int AddToN(int n) { if (n == 0) { // base case return 0; } else { // recursive case return n + AddToN(n-1); //make recursive call } } void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange (...
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: Write a recursive method named SumEvens that takes an integer X and returns the sum of even digits in X. Please comment on every line of code. EXISTING CODE: int numTwos(int n) { int right = n % 10; int remain = n / 10; if(n==0) { return 0 ; } else if(right ==2) { //rightmost digit is 2 return 1 + numTwos(remain); else { return 0 + numTwos(remain); } } int SumDigits(int X) { if (X == 0)...
in JAVA
24. Suppose that a class has an overloaded method named add with the following two implementations: double add (int x, double y) { return x + y; } double add (double x, int y) { return x + y + 1; } What, if anything, will be returned by the following method calls? A. add(3, 3.14) B. (3.14, 3) C. add (3, 3) D. add (3.14, 3.14) 29. Here is the code for a recursive method named mystery....
This code is based on creating a recursive funciton. You cannot alter the code besides adding your solution in the particular area. Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5: 5! = 5 * 4 * 3 * 2 * 1 = 120 #include void PrintFactorial(int factCounter, int factValue){ int nextCounter = 0; int nextValue = 0; if (factCounter == 0) { // Base case: 0! = 1 printf("1\n"); }...
C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1) Prod = M; //base case else Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...
Problem:
Write a function named
coinToss that simulates the tossing of a coin. When you call the
function, it should generate a random number in the range of 1
through 2. If the random number is 1, the function should display
“heads.” If the random number is 2, the function should display
“tails.” Demonstrate the function in a program that asks the user
how many times the coin should be tossed and then simulates the
tossing of the coin that...
C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...
C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== // bst.cpp #include <iostream> #include "bst.h" using namespace std; BinarySearchTree::BinarySearchTree() { root = nullptr; } void BinarySearchTree::insert(int key, string val) { Node* new_node = new Node; new_node->key = key; new_node->val = val; new_node->left = nullptr; new_node->right = nullptr; if (root == nullptr) { root = new_node; } else { insertHelper(root, new_node); } } void BinarySearchTree::insertHelper(Node* parent, Node* new_node) { if (new_node->key < parent->key) { if...
What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...