Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Bounded print all recursively

Solved!
Go to solution

hello,I had a C++ exam which I did well except the last problem.

the last problemSmiley Sad

 Let m,n,and sum be positive integers.In this problem we are interested in length-n arrays whose entries are integers in the set{1,2,....,m} such that the sum of the enteries of the array is less than or equal to sum.

  Write a recursive function,which given m,n,and sum,prints all such arrays.Your function should also compute the total number N of those arrys.

Examples:

 For m=3,n=3,and sum =4,the function should print

1  1  1

1  2  1

2  1  1

thus N=4

 

 For m=3,n=3,and sum=5,the function should print

1  1  1

1  1  2

1  1  3

1  2  1

1  2  2

1  3  1

2  1  1

2  1  2

2  2  1

3  1  1

thus N=10

 Form=3,n=4,and sum=6, the function should print

1  1  1  1

1  1  1  2

1  1  1  3

1  1  2  1

1  1  2  2

1  1  3  1

1  2  1  1 

1  2  1  2

1  2  2  1

1  3  1  1

2  1  1  1

2  1  2  1

2  2  1  1

3  1  1  1

thus N=15

 

The time of your function should be in the order of the output size N(and not m to power n).That is ,you are epected to avoid recursive branches which do not eventually lead to valid arrays.

"

Which way is the best way to solve this problem,I used a for loop,and placed inside it the same recursive function.What kinds of parameter should have I used(I used a pointer to an array,int,int ,itn)

I did not know the base case so I placed When I occurs final time.

0 Kudos
Message 1 of 4
(6,403 Views)

Hey Frodo,

 

nice question 😄 However, the answer you are asking for would be better to discuss with your proffesor or on some general programming or algoritmization forums, instead of measurement focused one.

 

 

Regards,

Stefo

Certified-LabVIEW-Developer_rgb.jpg

0 Kudos
Message 2 of 4
(6,357 Views)
I wish I can help you, but from the moral stand point. I rather not.
0 Kudos
Message 3 of 4
(6,342 Views)
Solution
Accepted by topic author Frodo Baggins
 Any body wants the solution,figured it out
0 Kudos
Message 4 of 4
(6,337 Views)