What you need is meory dynamics (it's very simple...)
First, you get the number, say 7. Then you have this line in your code:
Code:
int *array;
array = new int[number_that_is_from_user];
(After you have finished with array you must destroy it since it's a pointer: delete [] array;
Now, when you want to have that array full of random numbers that equal to n, things get complicated. Statistically speaking the best way might be a loop:
Code:
int sum;
while(1)
{
sum = 0;
for(int i = 0; i < number_that_is_from_user; i++)
{
array[i] = rand() % max;
sum += array[i];
}
if(sum == what_we_want)
break;
}
Only problem is that as arrays get bigger, the time needed to break from that loop gets huge!
Other simple option is that you do something like:
Code:
int max = 50, sum = 0;
for(int i = 0; i < number_that_is_from_user; i++)
{
arrray[i] = rand() % (max-sum);
sum -= array[i];
}
Only problem is that array[0] has the biggest change to be biggest number, array[1] second biggest, etc. It wouldn't surprise me, that when array[0] = max then every other `random' number would be 0.
Hopefully someone else has better ideas... :-)