Question

Write a C++ program to list all solutions of x1 + x2 + ... xn =...

Write a C++ program to list all solutions of x1 + x2 + ... xn = c.

x1 ... xn are non-negative integers.

Homework Answers

Answer #1

Hi,

Here is the code:

#include<iostream> 
using namespace std; 

int countSolutions(int n, int val) 
{ 
        int total = 0; 

        // Base Case if n = 1 and val >= 0 
        if (n == 1 && val >=0) 
                return 1; 

        // iterate the loop till equal the val 
        for (int i = 0; i <= val; i++){  
                total += countSolutions(n-1, val-i); 
                
        } 
        
        // return the total no possible solution 
        return total; 
} 

int main(){ 
        
        int n = 5; 
        int val = 20; 
        
        cout<<countSolutions(n, val); 
} 

The code outputs the number of solutions to a given equation.

Thanks :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Let n ≥ 2 and x1, x2, ..., xn > 0 be such that x1 +...
Let n ≥ 2 and x1, x2, ..., xn > 0 be such that x1 + x2 + · · · + xn = 1. Prove that √ x1 + √ x2 + · · · + √ xn /√ n − 1 ≤ x1/ √ 1 − x1 + x2/ √ 1 − x2 + · · · + xn/ √ 1 − xn
Consider the following program Min Z=-x1-x2 s.t 2x1+x2≤10 -x1+2x2≤10 X1, x2≥0 Suppose that the vector c=...
Consider the following program Min Z=-x1-x2 s.t 2x1+x2≤10 -x1+2x2≤10 X1, x2≥0 Suppose that the vector c= {-1,-1} is replaced by (-1,-1) +ʎ (2, 3) where ʎ is a real number Find optimal solutions for all values of ʎ      Z     X1      X2     S1   S2    RhS     Z      1      0       0    -0.6    -0.2     -8    X1      0      1       0     0.4    -0.2     10    X2     ...
Consider the following program Min Z=-x1-x2 s.t 2x1+x2≤10 -x1+2x2≤10 X1, x2≥0 Suppose that the vector c=...
Consider the following program Min Z=-x1-x2 s.t 2x1+x2≤10 -x1+2x2≤10 X1, x2≥0 Suppose that the vector c= {-1,-1} is replaced by (-1,-1) +ʎ (2, 3) where ʎ is a real number Find optimal solutions for all values of ʎ      Z     X1      X2     S1     S2    RhS     Z      1      0       0    -0.6    -0.2     -8    X1      0      1       0     0.4    -0.2      2    X2...
What is the generating function for the number of non-negative integer solutions to x1 + x2...
What is the generating function for the number of non-negative integer solutions to x1 + x2 + x3 + x4 + x5 = 50 if: 1.) There are no restrictions 2.) xi >= 2 for all i 3.) x1 <= 10 4.) xi <= 12 for all i 5.) if x1 is even
Let X1, X2, . . ., Xn be independent, but not identically distributed, samples. All these...
Let X1, X2, . . ., Xn be independent, but not identically distributed, samples. All these Xi ’s are assumed to be normally distributed with Xi ∼ N(θci , σ^2 ), i = 1, 2, . . ., n, where θ is an unknown parameter, σ^2 is known, and ci ’s are some known constants (not all ci ’s are zero). We wish to estimate θ. (a) Write down the likelihood function, i.e., the joint density function of (X1, ....
Suppose {x1,x2,...xn} is linear dependent and {x1,x2} is linear independent. Show that:  2 < than or equal...
Suppose {x1,x2,...xn} is linear dependent and {x1,x2} is linear independent. Show that:  2 < than or equal to (Span(x1,x2,...xn)) < n.
Find the number of solutions to x1+x2+x3+x4=16 with integers x1 ,x2, x3, x4 satisfying (a)  xj ≥...
Find the number of solutions to x1+x2+x3+x4=16 with integers x1 ,x2, x3, x4 satisfying (a)  xj ≥ 0, j = 1, 2, 3, 4; (b) x1 ≥ 2, x2 ≥ 3, x3 ≥ −3, and x4 ≥ 1; (c) 0 ≤ xj ≤ 6, j = 1, 2, 3, 4
Consider the sequence (xn)n given by x1 = 2, x2 = 2 and xn+1 = 2(xn...
Consider the sequence (xn)n given by x1 = 2, x2 = 2 and xn+1 = 2(xn + xn−1). (a) Let u, w be the solutions of the equation x 2 −2x−2 = 0, so that x 2 −2x−2 = (x−u)(x−w). Show that u + w = 2 and uw = −2. (b) Possibly using (a) to aid your calculations, show that xn = u^n + w^n .
Find the number of distinct solutions for x1 + x2 + x3 = 100 given that...
Find the number of distinct solutions for x1 + x2 + x3 = 100 given that x1, x2, x3 are nonnegative integers with at least one of them being more than 40. use
Choose the all correct things Sn=X1+X2+...+Xn 1. E[Sn]=2n, when RV X1,...,Xn is iid and E[X]=2 2....
Choose the all correct things Sn=X1+X2+...+Xn 1. E[Sn]=2n, when RV X1,...,Xn is iid and E[X]=2 2. VAR[Sn]=3n, when RV X1,...,Xn is iid and VAR[X]=3 3. VAR[Sn]=2n is always correct, when RV X1,...,Xn is identically distributed and VAR[X]=2 4 .Sn is an Exponential distribution with a mean of 2n and a variance of 3n ,when RV X1,...,Xn is iid and E[X]=2, VAR[X]=3 Exponential distribution