Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ - Q1 . Write a recursive function count_ones(int N) that returns the number of 1 in the binary representation of N. Note : use

C++

-

Q1. Write a recursive function count_ones(int N) that returns the number of 1 in the binary representation of N.

Note: use the algorithm of converting from decimal number to binary one.

Test cases:

a. count_ones(9) 2

b. count_ones(4) 1

c. count_ones(15) 4

-

Q2. (20 pts) Write a recursive method power(base, exponent) that, when called, returns

image text in transcribed

For example, power(3,4) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than equal to 1.

Hint: The recursion step should use the relationship

image text in transcribed

and the terminating condition occurs when exponent is equal to 1, because

image text in transcribed

baseexponent

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions