Finding Average of Array of integers using recursion

AMH9

I'm trying to find the average of integers elements in an array using recursion. I know how to do it using loops, but I have to do it by recursion for my assignment, so what I tried to do is to find the sum of elements using recursion and then divide the sum by the length of the array. I wrote this code but it gives me a wrong result:

public int findAvg(int a[], int n)
{
int sum,avg;
if(n==1)
 {

sum=a[0];
return sum;
}
else 
{
sum=a[n-1]+findAvg(a,n-1);
}

avg = sum/n;
return avg;}

The calling of findAvg method in main class:

public class main {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Recursive r = new Recursive ();
    int integersArr [] = {1,2,3,4,5};

    int max = r.findMax(integersArr,integersArr.length );
    int avg = r.findAvg(integersArr, integersArr.length);
    System.out.println("Maximum element = "+ max);
    System.out.println("Average value of elements = "+ avg);


 }

}

The console output:

Average value of elements = 1

Ashwani

First of all average of integers can be floating point. So make the return type of your function to float or double. Now,
If you have set of n numbers with average of x and you want to add one more number to the set (say b). New average will be ((n * x) + b) / (n + 1). Use the same trick in your code.

public float findAvg(int a[], int n)
{
    float sum,avg;
    if(n==1)
    {
        sum=a[0];
    }
    else 
    {
        // Calculate sum of n-1 numbers = (n-1) * (avg of n-1 numbers)
        // and add nth number to it ( i.e. a[n-1])
        sum= a[n-1]+ (n-1) * findAvg(a,n-1);
    }
    avg = sum/n;
    return avg;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding Average of Array of integers using recursion

From Dev

Finding max in an array using recursion

From Dev

Finding the average of an array using JS

From Dev

Finding Max value in an array using recursion

From Dev

finding minimum element of array using recursion

From Dev

Finding max value in array using recursion in PHP

From Dev

Finding the average from an array

From Dev

Finding the Mode of Integers in an Array

From Dev

Finding the average using mysql

From Dev

Using recursion to search all combinations of elements in an array of integers

From Dev

Finding Average by Replacing Text Values with Numbers Using Array and Not Let()

From Dev

Finding Average by Replacing Text Values with Numbers Using Array and Not Let()

From Dev

Finding the average of entered values stored in one variable, without using an array

From Dev

Finding Duplicate values in array of integers

From Dev

Finding unique combination using recursion

From Dev

Finding unique combination using recursion

From Java

query for finding average of average stored in mongodb using count of every average

From Dev

Recursion for finding the smallest path of numbers in an array

From Dev

Finding and printing the average of a square of values in a multidimensional array

From Dev

Finding if an array of positive integers form a closed polygon

From Dev

Finding complexities of space and time using recursion

From Dev

Finding if a number is a power of 2 using recursion

From Dev

Finding saddle point in a binary tree using recursion

From Dev

Finding complexities of space and time using recursion

From Dev

Python finding paths of trees using recursion

From Dev

Puzzle Given Squares With Integers; Solution Using Recursion

From Dev

How to sum an ArrayList<Integers> using recursion?

From Dev

Finding average of two numbers using only ints

From Dev

Finding the Average of each index using SQL

Related Related

  1. 1

    Finding Average of Array of integers using recursion

  2. 2

    Finding max in an array using recursion

  3. 3

    Finding the average of an array using JS

  4. 4

    Finding Max value in an array using recursion

  5. 5

    finding minimum element of array using recursion

  6. 6

    Finding max value in array using recursion in PHP

  7. 7

    Finding the average from an array

  8. 8

    Finding the Mode of Integers in an Array

  9. 9

    Finding the average using mysql

  10. 10

    Using recursion to search all combinations of elements in an array of integers

  11. 11

    Finding Average by Replacing Text Values with Numbers Using Array and Not Let()

  12. 12

    Finding Average by Replacing Text Values with Numbers Using Array and Not Let()

  13. 13

    Finding the average of entered values stored in one variable, without using an array

  14. 14

    Finding Duplicate values in array of integers

  15. 15

    Finding unique combination using recursion

  16. 16

    Finding unique combination using recursion

  17. 17

    query for finding average of average stored in mongodb using count of every average

  18. 18

    Recursion for finding the smallest path of numbers in an array

  19. 19

    Finding and printing the average of a square of values in a multidimensional array

  20. 20

    Finding if an array of positive integers form a closed polygon

  21. 21

    Finding complexities of space and time using recursion

  22. 22

    Finding if a number is a power of 2 using recursion

  23. 23

    Finding saddle point in a binary tree using recursion

  24. 24

    Finding complexities of space and time using recursion

  25. 25

    Python finding paths of trees using recursion

  26. 26

    Puzzle Given Squares With Integers; Solution Using Recursion

  27. 27

    How to sum an ArrayList<Integers> using recursion?

  28. 28

    Finding average of two numbers using only ints

  29. 29

    Finding the Average of each index using SQL

HotTag

Archive