1. Use recursion to write a function count_ones that returns how many Is there are in a number n when represented in decimal (base 10). For example, 1231 has two 1s. You can assume that n is nonnegative and at most 9 digits long. Do not use global (or static) variables. In main perform at least three tests of count_ones and use assert to check that the returned value is correct. Your function should have the following prototype: // count_ones (n) returns the number of is in the decimal 77 representation of n // requires: 0 <= n < 10^9 int count_ones (int n);

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question
Midterm Practice Problems
1. Use recursion to write a function count_ones that returns how many Is there are in
a number n when represented in decimal (base 10). For example, 1231 has two 1s.
You can assume that n is nonnegative and at most 9 digits long. Do not use global
(or static) variables.
In main perform at least three tests of count_ones and use assert to check that the
returned value is correct. Your function should have the following prototype:
// count_ones (n) returns the number of is in the decimal
representation of n
// requires: 0 <= n < 10^9
int count_ones (int n);
Transcribed Image Text:Midterm Practice Problems 1. Use recursion to write a function count_ones that returns how many Is there are in a number n when represented in decimal (base 10). For example, 1231 has two 1s. You can assume that n is nonnegative and at most 9 digits long. Do not use global (or static) variables. In main perform at least three tests of count_ones and use assert to check that the returned value is correct. Your function should have the following prototype: // count_ones (n) returns the number of is in the decimal representation of n // requires: 0 <= n < 10^9 int count_ones (int n);
Expert Solution
steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning