473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

operation program using recursion

the aim is to obtain a particular
number using a subset of given values and four arithmetic operations. Note that you are allowed to use each
given value at most once. For example, suppose that you are given the following set of values {3, 6, 12,
24, 43} and you are asked to obtain 344. Using a subset of these values, 344 could be obtained after the
following operations ( 24 / 3 ) * 43. Here also note that the operations ( ( 24 / 12 ) + 6 ) * 43 can also be
used to obtain the same value.
In this question, you are going to write a C++ program that reads a subset of values as well as the
number that is to be obtained from the user and determines whether or not the number can be obtained by
using a subset of the given values and four arithmetic operations. If it is obtained, you should display the
operation sequence. Otherwise, you should display a message. Note that, unlike the contest, your program
will allow the user to enter a set of values with different sizes.
Think about solving this question by iteration. You will notice that it is much more difficult to write its
iterative solution compared to the recursive one.
Some sample outputs of the program are given below:
Enter the number of values: 5
Enter 5 values: 3 8 9 25 50
Enter the number: 564
( ( ( 9 + 25 ) * 50 ) - 8 ) / 3
Enter the number of values: 4
Enter 4 values: 2 7 17 48
Enter the number: 745
No result
Enter the number of values: 6
Enter 6 values: 3 7 9 12 27 62
Enter the number: 684
( ( ( 3 * 7 ) + 9 ) + 27 ) * 12
Enter the number of values: 4
Enter 4 values: 3 2 4 17
Enter the number: 18
( ( 3 + 2 ) - 4 ) + 17
Enter the number of values: 3
Enter 3 values: 3 2 4
Enter the number: 16
No result
Enter the number of values: 3
Enter 3 values: 4 7 13
Enter the number: 21
No result
Enter the number of values: 6
Enter 6 values: 12 87 7 2 9 15
Enter the number: 1023
( ( ( ( 87 - 7 ) - 2 ) - 9 ) * 15 ) - 12
2
Enter the number of values: 4
Enter 4 values: 4 12 34 78
Enter the number: 34
34
In your program:
• You can assume that only integers are given as the input.
• Remember that a value in a set could be used at most once.
• A number could be found in different sequence of operations; remember how we found 344 at the
beginning. Your program could output one of these correct sequences.
Nov 25 '06 #1
0 1338

Sign in to post your reply or Sign up for a free account.

Similar topics

92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
10
by: free2cric | last post by:
Hi, FOllowing is a program which is written by someone else. Its output is a wonderful poem. I dont understand how it works really. can anyone tell. thanks cric #include <stdio.h> main(int...
0
by: Chris Thomasson | last post by:
Here is a "simple" method for using the preprocessor to generate code via. recursion... What do you think of my experimental recursion implementation? Can you compile it? Any comments are...
8
by: Jothishankar | last post by:
Hi, I am new to c#. I am trying to build an application that does backup of files to an external hard disk. My application behaves strangely. When i run the application under debug mode (F5),...
66
by: prady | last post by:
hi all, could any one solve the following C program. If any one knows the answer please post it Ques: A C function that will print 1 to N one per each line on the stdout , where N is a int...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
4
by: lianne.ribeiro | last post by:
I am using Visual C++ 6 IDE,with 512MB RAM. I have coded a recursive function that has a correct end condition for recursion. There is no infinite recursion. For some input data,the recursive...
20
by: aarklon | last post by:
Hi all, can any one give a example program where recursive version is faster than iterative version ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.