/** Computes the number of different groups of integers summing to value n
* @pre n is a non negative number.
* @post None.
* @param n The number specified by the user to calculate.
* @return The number of different groups of integers for the specified number. */
#include<iostream>
using namespace std;
int calculate (int n);
int main()
{
int n;
char y;
do
{
cout << "Enter a number to calculate the number of integer groups that sum to it: " << endl;
cin >> n;
if (n = 2)
{
cout << "With permutations: 1" << endl;
cout << "Without permutations: 1" << endl;
return 0;
}
if (n > 2)
{
calculate(n);
}
else
{
cerr << "Error: The number you entered is not valid." << endl;
return 1;
}
}while(n > 0);
return 0;
}
/** Computes the number of groups of integers for the specified number.
* @pre n must be greater than or equal to 2.
* @post None.
* @return The number of integer groups summing up to the value. */
int calculate (int n)
{
for (n = n, n-1 , --n)
{
}
return n;
}
cmel89
- 4 Contributors
- forum6 Replies
- 7 Views
- 9 Years Discussion Span
- comment Latest Post by Sky Diploma
VernonDozier 2,218
Code tags. Code looks terrible without them:
[code]
// paste code here
[/code]
What's the question? You pasted code with no explanation or question. Does it compile? Does it run? What happens?
firstPerson 761
Can you post a example of an input and output?
cmel89
sorry about that- i am new to this site and didn't understand how to use it at first. i just started in programming 2 so we are not doing anything "fancy" yet. the directions he gave me are:
Write a recursive function that returns the number of different groups of integers that sum to a given value of n. A sum must be involved, and 0 is not allowed as an operand. Therefore, if n=3, the answer is 3 (i.e., three groups of integers) if permutations are allowed. Your solution must be recursive. Your main program must loop, prompting for a value for n. While n≥2, compute the count for the "with" and "without" permutations cases (you may want to make two separate calls to your recursive function), print the answers in a clear fashion, and then prompt for the next value of n.
i understand the basics of how recursion works, just not how to develop the code for it. last time i tried compiling, it did compile but i haven't tried to input/output yet because my function is not complete yet.
VernonDozier 2,218
Can you/have you written it WITHOUT recursion? If you aren't familiar with recursion, often that's a good first step. And before that, you really need to sit down with a pencil and paper and go through a whole bunch of trials to nail down the process and the pattern in your head. Until then, it's not a recursion problem. It's an algorithm problem. If you can't do in with pencil and paper and explain it in detailed fashion, don't expect to be able to program it.
And you need some debugging statements in there. Have you checked to make sure your function is called?
Do you know how to write a for-loop? You must if you're onto recursion already. Have you tried compiling this program? Running it? Fixing the errors? Even this basic skeleton won't work, before any recursion is attempted.
cmel89
Ok, I've rewritten my code alot in the past couple days and I think I only need help developing the algorithm. Write a recursive function that returns the number of different groups of integers that sum to a given value of n. It does compile:
/** Computes the number of different groups of integers summing to value n
* @pre n is a non negative number.
* @post None.
* @param n The number specified by the user to calculate.
* @param perm The number of integer groups with permutations.
* @param no_perm The number of integer groups with no permutations.
* @return The number of different groups of integers for the specified number. */
#include<iostream>
using namespace std;
int p_calculate (int n, int perm);
int np_calculate (int n, int no_perm);
int main()
{
int n, perm, no_perm;
char ans, y, Y;
do
{
cout << "Enter a number to calculate the number of integer groups that sum to it: " << endl;
cin >> n;
if (n = 2)
{
cout << "With permutations: 1" << endl;
cout << "Without permutations: 1" << endl;
return 0;
}
if (n > 2)
{
p_calculate(n, perm);
cout << "With permutations: " << perm << endl;
np_calculate(n, no_perm);
cout << "Without permutations: " << no_perm << endl;
}
else
{
cerr << "Error: The number you entered is not valid." << endl;
return 1;
}
cout << "Enter another number for calculation? (Y/N): " << ans << endl;
}while((ans == Y) || (ans == y));
return 0;
}
/** Computes the number of groups of integers with permutations for the value n.
* @pre n must be greater than or equal to 2.
* @post None.
* @param n The value specified by the user.
* @param perm The number of integer groups with permutations summing to the value n.
* @return The number of integer groups summing up to the value. */
int p_calculate (int n, int perm)
{
for (n = n; n-1 ; --n)
{
if (n == 1) //base case
return 0; //no recursive call
else
{
//recursively calls to find integer groups with permutations
}
}
return perm;
}
/** Computes the number of groups of integers without permutations for the value n.
* @pre n must be greater than or equal to 2.
* @post None.
* @param n The value specified by the user.
* @param no_perm The number of integer groups without permutations summing to the value n.
* @return The number of integer groups without permutations. */
int np_calculate (int n, int no_perm)
{
for (n = n; n-1 ; --n)
{
if (n == 1) //base case
return 0; //no recursive call
else
{
//recursively calls to find integer groups without permutations
}
}
return no_perm;
}
Sky Diploma 571
if (n = 2)
shoudnt this be
if (n == 2)