Re: Steve Summit C notes , exercise



/* Steve Summit's C programming
*
* Section 3 :: exercise 2
*
* STATEMENT: Write a program to compute the average of the ten
numbers 1, 4,
* 9, ..., 81, 100, that is, the average of the squares of the
numbers from
* 1 to 10. (This will be a simple modification of Exercise 3 from
last
* week: instead of printing each square as it is computed, add it in
to a
* variable sum which keeps track of the sum of all the squares, and
then at
* the end, divide the sum variable by the number of numbers summed.)
*
* I wrote it in a way that was fun for me. YMMV.
*/

#include <stdio.h>
#include <math.h>
#include <assert.h>

double geometricMean(size_t count, double vector[])
{
double sum = vector[0];
size_t index;

if (count == 0)
return 0;
for (index = 1; index < count; index++)
sum *= vector[index];

return pow(sum, 1.0 / count);
}

/* Ref: http://mathworld.wolfram.com/PowerMean.html */
/* CANNOT be used on negative quantities! */
double generalizedMean(double power, size_t count, double
vector[])
{
size_t index;
double sum = 0;

if (count == 0)
return 0;
if (power == 0)
sum = geometricMean(count, vector);
else {
for (index = 0; index < count; index++) {
assert(vector[index] > 0);
sum += pow(vector[index], power);
}
sum /= count;
sum = pow(sum, 1.0 / power);
}
return sum;
}

double arithmeticMean(size_t count, double vector[])
{
double sum = 0;
size_t index;

if (count == 0)
return 0;
for (index = 0; index < count; index++)
sum += vector[index];

return sum / count;
}

double harmonicMean(size_t count, double vector[])
{
double sum = 0;
size_t index;

if (count == 0)
return 0;
for (index = 0; index < count; index++)
sum += 1.0 / vector[index];
sum /= count;
return 1.0 / sum;
}

double rmsMean(size_t count, double vector[])
{
double sum = 0;
size_t index;

if (count == 0)
return 0;
for (index = 0; index < count; index++)
sum += vector[index] * vector[index];

sum /= count;
return sqrt(sum);
}

int main()
{
int i;
double vector[10];

for (i = 1; i <= 10; ++i) {
vector[i - 1] = i * i;
}

printf("The Arithmetical average is: %.1f\n",
generalizedMean(1.0, 10, vector));
printf("Checking.....................: %.1f\n\n",
arithmeticMean(10, vector));
printf("The Harmonic average is: %.1f\n",
generalizedMean(-1.0, 10, vector));
printf("Checking.....................: %.1f\n\n", harmonicMean(10,
vector));
printf("The Geometric average is: %.1f\n",
generalizedMean(0.0, 10, vector));
printf("Checking.....................: %.1f\n\n",
geometricMean(10, vector));
printf("The RootMeanSquare average is: %.1f\n",
generalizedMean(2.0, 10, vector));
printf("Checking.....................: %.1f\n\n", rmsMean(10,
vector));

return 0;
}

/*
C:\tmp>cl average.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

average.c
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.

/out:average.exe
average.obj

C:\tmp>average
The Arithmetical average is: 38.5
Checking.....................: 38.5

The Harmonic average is: 6.5
Checking.....................: 6.5

The Geometric average is: 20.5
Checking.....................: 20.5

The RootMeanSquare average is: 50.3
Checking.....................: 50.3

*/

.



Relevant Pages

  • Re: Pythons simplicity philosophy
    ... Python will be aimed at aliens. ... Reasonable people on my world typically seem to realize that sum() is ... general-purpose programming language that aims for simplicity. ...
    (comp.lang.python)
  • RE: Automatically totaling a column in a table
    ... the Word Programming discussion group is full of VBA ... about a macro button. ... cells, and we're trying to keep it in Word to stay similar to our other ... If you did that, the Auto Sum feature would work, of course. ...
    (microsoft.public.word.tables)
  • Re: Pythons simplicity philosophy
    ... a built-in sum() function, since that's about 50% of what spreadsheets ... But general-purpose programming languages rarely have it. ... tasks) -- just that it's uncommon to build into the language a special ... APL was a favorite among ...
    (comp.lang.python)
  • Re: What I can to do with old PL/I code?
    ... Inpossibility to install compiler under Windows 64 bit edition. ... > Did the documentation say anywhere that it can be installed ... >>> cause is a programming error. ... > Then, if that is the case, you need to investigate why the sum is wrong. ...
    (comp.lang.pl1)
  • Re: reduce()--what is it good for? (was: Re: reduce() anomaly?)
    ... I have *never* used sum() that ammounts to sum). ... I don't care about speed all that much when using Python. ... special-purpose feature, while reduceis a very general feature that ... It was just like all the other myriad of programming ...
    (comp.lang.python)