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

11y ago

What else can I help you with?

Continue Learning about Engineering

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); }


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++


What is the name of the function which must be defined in a Java program?

The main function. Every program must have a main function and it must be declared static.


What is matrix programming in C programming?

C Examples on Matrix OperationsA matrix is a rectangular array of numbers or symbols arranged in rows and columns. The following section contains a list of C programs which perform the operations of Addition, Subtraction and Multiplication on the 2 matrices. The section also deals with evaluating the transpose of a given matrix. The transpose of a matrix is the interchange of rows and columns.The section also has programs on finding the trace of 2 matrices, calculating the sum and difference of two matrices. It also has a C program which is used to perform multiplication of a matrix using recursion.C Program to Calculate the Addition or Subtraction & Trace of 2 MatricesC Program to Find the Transpose of a given MatrixC Program to Compute the Product of Two MatricesC Program to Calculate the Sum & Difference of the MatricesC Program to Perform Matrix Multiplication using Recursion

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 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.


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.


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); }


Write a C program to find sum of 3 matrices?

matrix


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.


How do you write a program to performs all airthematic operation between two matrixs using array?

To write a program that performs arithmetic operations between two matrices using arrays, first define two 2D arrays to represent the matrices. Then, create functions for each arithmetic operation (addition, subtraction, multiplication, etc.) that iterate through the elements of the matrices, performing the operation element-wise. Ensure to handle cases where the matrices have different dimensions, as this would affect the validity of the operations. Finally, print the result matrix after each operation.


Where can one find a free promissory note template?

One is able to find a free promissory note template both online at promissory note template website, and on Microsoft where there is a program dedicated to letter and note making.