Re: Steve Summit C notes , exercise



Jason Curl wrote:
arnuld wrote:

#include <stdio.h>

#define DIVISOR 10

int main()
{
int i;
double sum;

You can probably make this an int. The square of an int is still an int (providing no overflows). It's generally faster using ints than doubles.

It certainly is sufficient for this particular exercise, but speed shouldn't be an issue here. I consider it a matter of style. I would use double here for this type of simple program so that I don't have to worry about overflow with a larger limit or truncation on division.

printf("the average is: %.1f\n", sum / DIVISOR);

if you take my advice above about using an int above, just be careful to promote sum to a float here, e.g. (float)sum / DIVISOR

That works, but I use double as the preferred floating point type. If I choose float (or long double), it is for a specific reason.

--
Thad
.



Relevant Pages

  • Re: Segmentation Fault....
    ... // - Sum of numbers squared ... double vsum(double x, int n){ ... float Result = 0; ... //Calculation for - Mean of the squares ...
    (comp.lang.c)
  • Segmentation Fault....
    ... // - Sum of numbers squared ... double vsum(double x, int n){ ... float Result = 0; ... //Calculation for - Mean of the squares ...
    (comp.lang.c)
  • Re: Help a beginner - function with pointer ...
    ... it has been converted to type float*. ... float Sum(float *A, int N, int M); ... It points to the first element of a 2-dimensional array ... the function Sum treats its ...
    (comp.lang.c)
  • representational error in C
    ... int main{ ... /* Add 0.1 to itself for 1000 times using float for all data would ... float sum = 0.0; ...
    (comp.lang.c)
  • Re: Algorithm
    ... Wont sum of all positive numbers will be the largest sub-array? ... int getint ... struct sofar *next; ... struct sofar *discard(struct sofar *trail) ...
    (comp.lang.c)