Try the fastest way to create flashcards
Question

Write the following function. The call sum(g, 1, j) should returng(i) + ... +g (j).

int sum(int (*f)(int), int start, int end);

Solution

Verified
Answered 1 month ago
Answered 1 month ago
Step 1
1 of 2

A function that sums the return values of the passed function given a range of values:

int sum(int (*f)(int), int start, int end) 
{
   int sum = 0;
   while (start <= end) {

      // call the passed function
      // passing the values between
      // start and end
      sum += (*f)(start);
      start++;
   }

   return sum;
}

Create a free account to view solutions

Create a free account to view solutions

Recommended textbook solutions

Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe

Fundamentals of Database Systems

7th EditionISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
948 solutions
Introduction to Algorithms 3rd Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

3rd EditionISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
872 solutions
Introduction to Algorithms 4th Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

4th EditionISBN: 9780262046305Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
945 solutions
C Programming: A Modern Approach 2nd Edition by K N King

C Programming: A Modern Approach

2nd EditionISBN: 9780393979503K N King
495 solutions

More related questions

1/4

1/7