answersLogoWhite

0

#include<iostream>

#include<algorithm>

#include<cassert>

unsigned multiply_recursive (unsigned a, unsigned b)

{

if (!a !b) return 0; // Note: not(a) or not (b)

if (a==1) return b;

if (b==1) return a;

return a + multiply_recursive (a, --b);

}

unsigned multiply_iterative (unsigned a, unsigned b)

{

int product = 0;

int x = std::min (a, b);

int y = std::max (b, a);

while (x--)

product += y;

return product;

}

int main()

{

unsigned x, y;

// Test all combinations with 0.

x = multiply_recursive (0, 0);

assert (x==0);

x = multiply_iterative (0, 0);

assert (x==0);

x = multiply_recursive (0, 1);

assert (x==0);

x = multiply_iterative (0, 1);

assert (x==0);

x = multiply_recursive (1, 0);

assert (x==0);

x = multiply_iterative (1, 0);

assert (x==0);

// Test non-zero values with 1.

x = multiply_recursive (1, 42); // inefficient: lowest value must be on right (fewer recursions).

y = multiply_iterative (1, 42);

assert (x==42 && y==42);

x = multiply_recursive (42, 1);

y = multiply_iterative (42, 1);

assert (x==42 && y==42);

// Test other non-zero values are commutative.

x = multiply_recursive (24, 42); // inefficient: lowest value must be on right (fewer recursions).

y = multiply_iterative (24, 42);

assert (x==1008 && y==1008);

x = multiply_recursive (42, 24);

y = multiply_iterative (42, 24);

assert (x==1008 && y==1008);

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

Program to display multiplication of two matrix?

The matrix multiplication in c language : c program is used to multiply matrices with two dimensional array. This program multiplies two matrices which will be entered by the user.


How do you write a program that will call a function to multiply 4 numbers and return the answer to main program?

In Windows, use notepad.exe; in linux, use program xedit.


How do you develop a JAVA program that computes matrices?

Matrices can't be "computed" as such; only operations like multiplication, transpose, addition, subtraction, etc., can be done. What can be computed are determinants. If you want to write a program that does operations such as these on matrices, I suggest using a two-dimensional array to store the values in the matrices, and use for-loops to iterate through the values.


Write a C program to find sum of 3 matrices?

matrix


Program to find biggest number among two numbers using template function?

template&lt;class T&gt; T&amp; max(const T&amp; x, const T&amp;y){return(x&gt;y?x:y); }


Is there any benefit in a C plus plus program to add two matrices?

No.


Write a c program using dynamic memory allocation function calloc to find sum of two matrices and also print the resultant matrix?

printf("%s",per&gt;50?:"pass",per&lt;50?:"fail");


How to write C plus plus Program to take 2 distant values as argument and return the larger value on the screen?

Use the following template function: template&lt;class T&gt; T&amp; max(T&amp; x, T&amp; y){return(y&lt;x?x:y;}


What program has a modifiable calendar template?

There are many online shops that sell calendar template. Amazon.com is one of online shop that sells calendar template for any kind of models. You can visit www.amazon.com


Where can one obtain a table of contents template?

Someone may obtain a table of contents template from Window's Microsoft Word program and other various Microsoft programs will carry this template aid as well.


Which function is supported by an application program?

which function is support by an application program


What are its program components?

the main() function,the#include Directive, the variable definition. function prototype, program statements, the function definition,program comments and braces are the components of program in C++