Computer Science using C Problem Solving and Program Design Using C Sixth Editio
ID: 3638834 • Letter: C
Question
Computer Science using C
Problem Solving and Program Design Using C Sixth Edition
The MySum function.
The first integer must be smaller than
the second integer. If that is not the case, the function should return
-999. Otherwise, the function should return the sum of all odd
integers in the range. For example, if the first integer is 5 and the second
integer is 15, the function should return the sum of the numbers 5+7+9+11+13+15
which is 60. Another example, if the first number is 6 and the second number is
12, the function should return the sum of 7+9+11
To write this function you'll need to have a loop. Write two flavors of the
same program, one using the while loop and another using the for
loop. (so you need to submit TWO programs).
NOTE 1 I will NOT grade any assignments if you use global variables!
NOTE 2 You MUST format your source the same way the author of our book
does.
NOTE 3 You must use inline documentation (comments).
NOTE 4 The above 3 NOTEs will be enforced for the rest of the
semester.
NOTE 5 Don't get any help from anyone but me. Don't help anyone else
in the class. This is not a team based assignment!
in the numbers in main(), then from main you call the MySum function. The
main shows the result that the MySum function returns.
The MySum function.
The first integer must be smaller than
the second integer. If that is not the case, the function should return
-999. Otherwise, the function should return the sum of all odd
integers in the range. For example, if the first integer is 5 and the second
integer is 15, the function should return the sum of the numbers 5+7+9+11+13+15
which is 60. Another example, if the first number is 6 and the second number is
12, the function should return the sum of 7+9+11
To write this function you'll need to have a loop. Write two flavors of the
same program, one using the while loop and another using the for
loop. (so you need to submit TWO programs).
NOTE 1 I will NOT grade any assignments if you use global variables!
NOTE 2 You MUST format your source the same way the author of our book
does.
NOTE 3 You must use inline documentation (comments).
NOTE 4 The above 3 NOTEs will be enforced for the rest of the
semester.
NOTE 5 Don't get any help from anyone but me. Don't help anyone else
in the class. This is not a team based assignment!
Explanation / Answer
int MySum(int a, int b)
{
if(a > b) return -999;
int sum = 0,i;
if(a%2 == 0)
i = a+1;
else
i = a;
for(int k=i; k<b; k=k+2)
sum = sum + k;
return sum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.