Question

This is not efficient, simplify this. if (n == 1) result = n + 2; if...

This is not efficient, simplify this.
 if (n == 1)
      result = n + 2;
    if (n == 2)
      result = n + n;
    if (n == 3)
      result = 3 * n;
    if (n <= 0)
      result = 0;
    if (n > 3)
      result = 1;

Homework Answers

Answer #1

We will use a simple if and else statement.

The if statement will control n>3, and the else part will use the switch statement.

We will use a switch statement for the case n == 1 , n == 2, n == 3, and the default case for n <=0.

Here is the simplification

if( n > 3)

{

result = 1;

}

else

{

switch(n)

{

case 1: result = n + 2;

              break ;

case 2: result = n + n;

              break ;

case 3: result = 3*n;

              break ;

default: result = 0;

           break;

}

}

Please don't forget to give a positive rating to the answer. Your rating motivates experts to help other students also. Thank you :)

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
1.Simplify (n+1)! / (n-1)! 2..A survey found that 10% of Americans believe that they have seen...
1.Simplify (n+1)! / (n-1)! 2..A survey found that 10% of Americans believe that they have seen a UFO. For a sample of 10 people, find the probability that exactly 5 people believe that they have seen a UFO. Use Table B or the binomial probability formula.
For the following sample of n =8 scores: 0, 1, 1/2, 0, 3, 1/2 , 0,...
For the following sample of n =8 scores: 0, 1, 1/2, 0, 3, 1/2 , 0, 1 a. Simplify the arithmetic by first multiplying each score by 2 to obtain a new sample. Then, compute the mean and standard deviation for the new sample. b. Starting with the values you obtained in part a, make the correction for having multiplied by 2 to obtain the values for the mean and standard deviation for the original sample
Simplify:          3/ x2 + 5x + 4 +      2/ x2 - 1
Simplify:          3/ x2 + 5x + 4 +      2/ x2 - 1
1. Find f''(x) F(x)= (x^2+2)^9 F''(x)= 2. Find t^(4)(n) for the function t(n)= 2n^-1/2+7n^3/2 t^(4)(n) =...
1. Find f''(x) F(x)= (x^2+2)^9 F''(x)= 2. Find t^(4)(n) for the function t(n)= 2n^-1/2+7n^3/2 t^(4)(n) = (Type an expression using n as the variable. Simplify your answer)
1. Find lim x-0. tan4x/sin5x 2. Find and simplify ?/(?) ?? ?(?) = 1-cos(?)/1+cos(?)
1. Find lim x-0. tan4x/sin5x 2. Find and simplify ?/(?) ?? ?(?) = 1-cos(?)/1+cos(?)
3) (i) How many primes are there below 2^{64}, approximately? To simplify calculations you may replace...
3) (i) How many primes are there below 2^{64}, approximately? To simplify calculations you may replace ln(2) by 0.5. (ii) You want to find n such that the proportion of primes among the first n positive integers is at least 1/100. Then n must not exceed which number? (iii) How many primes are there between 2^{128} and 2^{64}, approximately? To simplify calculations you may replace ln(2) by 0.5.
(4) 1. Simplify the following assignments/calculations if possible: a. a = 0; b = 0; c...
(4) 1. Simplify the following assignments/calculations if possible: a. a = 0; b = 0; c = 0; d = 0; _____________________________ b. n1 = n1 % n2; _____________________________ c. n3 += 1; _____________________________ d. n4 = n5 / n4; _____________________________ 2. Suppose that the statement x = 12; has already been done. What values will the following printf statements output assuming that the statements are done in the following order: a. printf("%d\n", ++x); __________ b. printf("%d\n", x); __________ c....
5. (10 pts) Let ?(?) = ?^2−1/ 2?^2+3?+1 a) Simplify the function and find all asymptotes...
5. (10 pts) Let ?(?) = ?^2−1/ 2?^2+3?+1 a) Simplify the function and find all asymptotes algebraically. b) Sketch the graph of ?(?) with the help of a calculator, indicate the asymptotes, the possible hole, and the x- and y-intercepts on your graph.
Prove directly from definition that the set {1/2^n | n=1, 2, 3, ...} is not compact...
Prove directly from definition that the set {1/2^n | n=1, 2, 3, ...} is not compact but {0, 1/2^n |n=1, 2, 3, ...} is compact.
Let ?(?, ?) = ?^2 sin(?^2 + 4?). Determine the following derivatives (no need to simplify)....
Let ?(?, ?) = ?^2 sin(?^2 + 4?). Determine the following derivatives (no need to simplify). 1. ??(?, ?) 2. ???(?, ?) 3. ?2? / ???x