ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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...
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).
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.
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.
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.
Read in the new number (or blank to stop)
if it is a valid number
add it to the running total
increment the count
perform a recursive call
if it is not a valid number
calculate the average
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.