LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Recursive average (https://www.linuxquestions.org/questions/programming-9/recursive-average-425673/)

cdog 03-17-2006 03:29 AM

Recursive average
 
I want to calculate the average using a recursive function and when I get to the end of input the average is already calculated.
What I mean is not using the algorithm of multiplying what the function returns with the size-1 ad the number and divide all by size: not like this (Func(parameters)*(size-1)+num)/size

jschiwal 03-17-2006 05:51 AM

That would be a very expensive way of calculating an average:
Code:


1:1 2:1 3:1 4:1 5:1 6:1 7:1 8:1 9:1 10:1
3:2    7:2    11:2    15:2    19:2
10:4          26:4          19:2
36:8                          19:2
55:10

You would have over twice as many addition operations.

cdog 03-17-2006 08:21 AM

Quote:

Originally Posted by jschiwal
That would be a very expensive way of calculating an average:
Code:


1:1 2:1 3:1 4:1 5:1 6:1 7:1 8:1 9:1 10:1
3:2    7:2    11:2    15:2    19:2
10:4          26:4          19:2
36:8                          19:2
55:10

You would have over twice as many addition operations.


So what you're sayin jschiwal is that the function should call itself at least twice. I can't do that because I'm also getting the input ( the numbers one by one). Do you have an example of C code? Thanks

bigearsbilly 03-17-2006 08:36 AM

that must mean the function gets input too?
not typically classic recursion.

homework?

graemef 03-17-2006 10:28 AM

Essentially the question is why recursive?

I think you need to provide a little more for everyone to go on. Sure it can be done, but why do you want to do it that way?

cdog 03-17-2006 12:09 PM

Quote:

Originally Posted by graemef
Essentially the question is why recursive?

I think you need to provide a little more for everyone to go on. Sure it can be done, but why do you want to do it that way?

because I want to compare the input with the average without using any array.

graemef 03-17-2006 12:53 PM

Okay, but one more question, why don't you want to use an array?

cdog 03-17-2006 04:31 PM

because I'm not alowed.

95se 03-17-2006 07:47 PM

some sort of pseudo code..

Code:

average(num,avg,&count) {
 count = count + 1
 return avg - (1/count) + num/count
}

Each time you get a new number, pass it in, along w/ the current average, and the number of items entered thus far, not including the current. And as was said before, this is an expensive way of doing it...

graemef 03-17-2006 08:12 PM

Quote:

Originally Posted by cdog
because I'm not alowed.

What does that mean?

cdog 03-18-2006 03:24 AM

Quote:

Originally Posted by 95se
some sort of pseudo code..

Code:

average(num,avg,&count) {
 count = count + 1
 return avg - (1/count) + num/count
}

Each time you get a new number, pass it in, along w/ the current average, and the number of items entered thus far, not including the current. And as was said before, this is an expensive way of doing it...

Thanks 95se, but I would have to initialize count and avg and that would mean on every recursion - of course if your function is handling everything (that's what I'm looking for - I don't want any helping function).

Quote:

Originally Posted by graemef
What does that mean?

It means this is some kind of homework.

jschiwal 03-18-2006 09:18 AM

Recursion would be called with an array, and would break up the array into two parts and call itself again until it has two adjacent elements and then would unwind, taking the results (total and number of elements) of two calls, until the main routine has the total and the number of elements. So using recursion wouldn't make sense for this problem.

Also, asking for c code for homework problems (the answer) is a no-no on this site. The problem you want to solve is how to keep a running average of numbers given one number at a time. So this is really a basic math problem and a test of your knowledge of different variable types in C.

95se 03-18-2006 09:21 AM

Average and count should start as 0. That's the basic algorithm for solving an average number entered by number entered. If it's a homework question though, you should say so. People will help you out here, but probably not give you actual code. This is more for your own benefit, than not wanting to do someone else's homework.

graemef 03-18-2006 10:06 AM

As already stated we are happy to help with homework, but few people will actually give you code, because in the long run it doesn't help. So please be honest and also try and explain what you have managed so far.

Your recursive function needs to do a couple of things.
  1. Read in the new number (or blank to stop)
  2. if it is a valid number
    1. add it to the running total
    2. increment the count
    3. perform a recursive call
  3. if it is not a valid number
    1. calculate the average
    2. return the average

From that have a go at coding it. Show us what you manage to achieve and we can give you more advice if necessary.

cdog 03-18-2006 10:58 AM

Quote:

Originally Posted by 95se
Average and count should start as 0. That's the basic algorithm for solving an average number entered by number entered. If it's a homework question though, you should say so. People will help you out here, but probably not give you actual code. This is more for your own benefit, than not wanting to do someone else's homework.

what difference does it make if it's homework or not. You want to tell me that if this wasn't homework you would have given me the code?
now the last question. I know that average and counter should be initialized at 0, that's why I asked, but if this is done inside the function it would be initialized at every recursion. if the function is called (inside the program) with average and counter at 0, than it's not a very effective function. thank you all for your input.


All times are GMT -5. The time now is 04:01 PM.