Re: Steve Summit C notes , exercise
- From: "user923005" <dcorbit@xxxxxxxxx>
- Date: 17 Mar 2007 01:50:41 -0700
/* 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
*/
.
- References:
- Steve Summit C notes , exercise
- From: arnuld
- Steve Summit C notes , exercise
- Prev by Date: Re: data type of size 64 bits on a 32 bit processor
- Next by Date: Steve Summit C Notes {Anticipating the next one}
- Previous by thread: Re: Steve Summit C notes , exercise
- Next by thread: Re: Steve Summit C notes , exercise
- Index(es):
Relevant Pages
|