Steve Summit C注意到,运动 [英] Steve Summit C notes , exercise
问题描述
这是我创建的程序,用于练习2,作业3在
http://www.eskimo.com/~scs/cclass/asgn.beg/PS2.html
它运行正常。我想知道它是否需要改进:
----------------- PROGRAM ---------- ------------------
/ * Steve Summit'的C编程
第3节: :练习2
声明:
编写一个程序来计算十个数字的平均值1,4,
9, ...,81,100,
即数字从1到10的平均值。(这个
将是一个
从上周开始对练习3进行简单修改:而不是打印
每个方格
计算时,将其添加到变量sum中,跟踪
所有
的平方和,然后在最后,将sum变量除以
总和的数字。)
* /
#include< stdio.h>
#define DIVISOR 10
int main()
{
int i;
double sum;
sum = 0;
for(i = 0 ; i< = 10; ++ i)
sum + = i * i;
printf(平均值为:%。1f \ n,sum / DIVISOR );
返回0;
}
-------- ---------------输出----------------------
[arch @ voodo steve-summit] $ gcc -std = c99 -pedantic -Wall -Wextra
assign-3_ex-2.c
[arch @ voodo steve-summit] $ ./ a.out
平均值是:38.5
[arch @ voodo steve-summit] $
this is the programme i created, for exercise 2, assignment 3 at
http://www.eskimo.com/~scs/cclass/asgn.beg/PS2.html
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. (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.)
*/
#include <stdio.h>
#define DIVISOR 10
int main()
{
int i;
double sum;
sum = 0;
for(i = 0; i <= 10; ++i)
sum += i*i;
printf("the average is: %.1f\n", sum / DIVISOR);
return 0;
}
----------------------- OUTPUT ----------------------
[arch@voodo steve-summit]$ gcc -std=c99 -pedantic -Wall -Wextra
assign-3_ex-2.c
[arch@voodo steve-summit]$ ./a.out
the average is: 38.5
[arch@voodo steve-summit]$
推荐答案
gcc -std = c99 -pedantic -Wall -Wextra
assign-3_ex-2.c
[arch @ voodo steve-summit]
gcc -std=c99 -pedantic -Wall -Wextra
assign-3_ex-2.c
[arch@voodo steve-summit]
./ a.out
平均值为:38.5
[arch @ voodo steve-summit]
./a.out
the average is: 38.5
[arch@voodo steve-summit]
这篇关于Steve Summit C注意到,运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!