Question

Write a recursive function that accumulates the sum of values in an n element array and...

Write a recursive function that accumulates the sum of values in an n element array and prints out the contents of the array and the final sum and average.

In c language

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int sumArray(int arr[], int start, int size){
   if(start < size){
      return arr[start] + sumArray(arr, start + 1, size);
   }
   else{
      return 0;
   }
}

int main() {
       int arr[] = {12, 5, 7, 3, 8, 9, 4};
       int size = 7, i, s;
       
       for(i = 0;i<size;i++){
          printf("%d ",arr[i]);
   }
   printf("\n");
   
   s = sumArray(arr, 0, size);
       printf("Sum of the array: %d\nAverge = %f\n",s,(1.0*s/size));;
       
       return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a recursive function that accumulates the sum of values in an n element array and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT