473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

writing a function to calculate an average

hi guys,
a part of my program requires me to calculate an average of items that
are sold. the easiest way to do that would be writing a function, but
im having trouble making up the parameters. if you can point me which
elements to use when performing such a task, i would really appreciate
it. thanks

Apr 5 '07 #1
4 6514
gaga wrote:
hi guys,
a part of my program requires me to calculate an average of items that
are sold. the easiest way to do that would be writing a function, but
im having trouble making up the parameters. if you can point me which
elements to use when performing such a task, i would really appreciate
it. thanks
You can find some advice here:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
Apr 5 '07 #2
gaga wrote:
hi guys,
a part of my program requires me to calculate an average of items that
are sold. the easiest way to do that would be writing a function, but
im having trouble making up the parameters. if you can point me which
elements to use when performing such a task, i would really appreciate
it. thanks
Average is equal to the sum of a set of values divided by the number in
the set.

In your API, how are you presented with the set of values ? Are they in
an array, a file or there is no distinct set so your computation is
disjoint over the execution ?

Anyhow, once you have the sum and the count, you can easily compute the
average with a template like so:

template <typename T>
T average( const T & sum, unsigned long count )
{
return sum / count;
}

Apr 5 '07 #3
You can find some advice here:http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
You guys are funny... :)

Apr 6 '07 #4
On Apr 6, 1:04 am, Gianni Mariani <gi3nos...@mariani.wswrote:
gaga wrote:
hi guys,
a part of my program requires me to calculate an average of items that
are sold. the easiest way to do that would be writing a function, but
im having trouble making up the parameters. if you can point me which
elements to use when performing such a task, i would really appreciate
it. thanks
Average is equal to the sum of a set of values divided by the number in
the set.
In your API, how are you presented with the set of values ? Are they in
an array, a file or there is no distinct set so your computation is
disjoint over the execution ?
Anyhow, once you have the sum and the count, you can easily compute the
average with a template like so:
template <typename T>
T average( const T & sum, unsigned long count )
{
return sum / count;
}
You don't want to write a template unless you really have to.
On the other hand, this can be done in a single expression using
functions in the standard library:

average = std::accumulate( begin, end, 0.0 ) /
std::distance( begin, end ) ;

Of course, if the values are floating point, and there are more
than a very few, the results could easily be wrong. Getting an
even moderately accurate sum of a sequence of floating points is
non-trivial.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: tonyjeffs | last post by:
In the following code, what type is examplefile? I know that FILE is incorrect! Thanks Tony main () { char infile="test.it"; ifstream examplefile (infile); myFunction (examplefile);
3
by: Richard A. DeVenezia | last post by:
Can a function write another function that has a specific number of nested loops and then run it? i.e. function maker (N) { // does stuff that creates function doer() // invoke doer doer() }
2
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
1
by: Alex | last post by:
I have a comma separated text file in the form: 11/28/2004 12:00:00, 2.78655354 11/28/2004 12:00:10, 1.9978 11/28/2004 12:00:20, 0.978 11/29/2004 12:00:00, 1.98467 11/29/2004 12:00:10, 3.005 ...
1
by: dave | last post by:
Hi There Can anyone help to write permutation function for following formula.. nPr = n!/(n-r)! where n is length of given string and r is lengh of subset.. for example..if given string is "abcd"...
1
by: Sedigh | last post by:
Hi Everybody, I need to write a macro on my Excel sheet to calculate the average of cells for me. This is the code I have written but the average function is not working. Can you please let me...
7
by: jamesnkk | last post by:
Hi, Although this question may not directly link to Access, but I think it a common question to those developer.so hope you could suggest a solution. How do I get the average cost for Item sold,...
6
by: MDVking | last post by:
hello im very new in python and i want to write a function that will return the sum of all the integer in a list how do i do that? someone please help me thank you
2
by: julia1128 | last post by:
Hi all, I'm a newbie to perl, really need some help. It seemed to be a very simple question but I kept getting wrong answers... Thanks in advance!! The text file looks like this: 0.000004 ...
0
by: pratikm23 | last post by:
add runtime textboxes and input values into them to calculate the average my code to add the runtime textboxes is: Private Sub Command1_Click() Dim TextControl As TextBox ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.