Next, write another function, iterative_compute_sum, that also takes one integer
ID: 665482 • Letter: N
Question
Next, write another function, iterative_compute_sum, that also takes one integer argument and returns the sum of the first n integers. This function, however, does not use a recursive method but will instead compute the sum using a while loop and then returning the answer.
The program you turn in should have the following main program but should work for any values, not just 10:
int main(int argc, char* argv[])
{
printf("%d ", recursive_compute_sum(10)); printf("%d ", iterative_compute_sum(10));
return 0;
}
Explanation / Answer
int iterative_compute_sum(int n)
{
int sumi=0,current=1;
while(current<=n)
{
sumi+=current;
current++;
}
return sumi;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.