Re: Steve Summit C notes , exercise



arnuld wrote:
it runs fine. i wanted to know if it needs any improvement:

----------------- PROGRAMME ----------------------------
/* 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.

/* Since the sum of the squares of the integers 1 ... N is
N * (N + 1) * (2*N + 1) / 6,
and there are N integers in 1 ... N,
we have the following general function.
It could be a macro, instead.
Note the decimal points (there's one extra one
not needed). If the function is not required
to work except with N==10, replace the returned
expression with 38.5
*/

#include <stdio.h> /* not needed for the problem as stated, but
the actal program we will show the value
computed. */

inline double average_of_squares(int n)
{
return (n + 1.) * (2. * n + 1) / 6.;
}

/& To satisfy the requirements of the program, you need only :

int main(void)
{
average_of_squares(10);
return 0;
}

but showing the value computed is probably a good idea: */


int main(void)
{
printf("The average is %g\n", average_of_squares(10));
return 0;
}
.



Relevant Pages

  • Re: Steve Summit C notes , exercise
    ... Section 3:: exercise 2 ... we have the following general function. ... I believe for portable C, we don't want to use 'inline', unless you're using C99. ... int main ...
    (comp.lang.c)
  • Re: Add Method
    ... Do you mean to convert the number to a string? ... My guess is that this is an exercise for a 1st-year computer science course ... and it means to convert positive number (probably in an int) into a String ...
    (comp.lang.java.help)
  • Re: Direct computation of integer limits in K&R2?
    ... The original exercise text as provided by Richard Heathfield is: ... Write a program to determine the ranges of char, short, int, and long, both signed and unsigned, by printing appropriate values from standard headers and by direct computation. ... int INTMIN, INTMAX; ...
    (comp.lang.c)
  • Re: race condition during termination process
    ... There are 2 exercise at the last part of chapter 7.7(Threads ... But I couldn't find the cause of race condition and the solution. ... int request_index; ...
    (comp.programming.threads)
  • Re: pointer misunderstanding?
    ... Steve wrote: ... Here you set the value of the *pointer* tmp equal to v. ... The actual assignment that you need here is that the int that tmp refers ... a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq ...
    (alt.comp.lang.learn.c-cpp)