LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2006, 03:29 AM   #1
cdog
Member
 
Registered: Dec 2005
Posts: 65

Rep: Reputation: 15
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

Last edited by cdog; 03-17-2006 at 03:37 AM.
 
Old 03-17-2006, 05:51 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 03-17-2006 at 06:09 AM.
 
Old 03-17-2006, 08:21 AM   #3
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
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
 
Old 03-17-2006, 08:36 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
that must mean the function gets input too?
not typically classic recursion.

homework?
 
Old 03-17-2006, 10:28 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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?
 
Old 03-17-2006, 12:09 PM   #6
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
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.
 
Old 03-17-2006, 12:53 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Okay, but one more question, why don't you want to use an array?
 
Old 03-17-2006, 04:31 PM   #8
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
because I'm not alowed.
 
Old 03-17-2006, 07:47 PM   #9
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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...

Last edited by 95se; 03-17-2006 at 07:48 PM.
 
Old 03-17-2006, 08:12 PM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by cdog
because I'm not alowed.
What does that mean?
 
Old 03-18-2006, 03:24 AM   #11
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
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.
 
Old 03-18-2006, 09:18 AM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 03-18-2006 at 09:20 AM.
 
Old 03-18-2006, 09:21 AM   #13
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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.
 
Old 03-18-2006, 10:06 AM   #14
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 03-18-2006, 10:58 AM   #15
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Average American a Slave? NEC5 General 18 01-29-2006 07:31 AM
load average? ampex189 Linux - Newbie 2 03-06-2005 07:17 PM
Average Partitioning Time milnet Mandriva 1 12-25-2003 03:20 PM
Load average 1.0, 1.0, 1.0 ? belated Linux - Newbie 4 11-30-2003 03:49 PM
Average load Cyth Linux - General 1 01-22-2002 03:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration