a) Thuật toán:
Bước 1: Nhập n
Bước 2: Nhập x[1..n]
Bước 3: s<-0;
Bước 4: i<-1 to n
s<-s+xi;
Bước 5: tbc<-s/n
Bước 6: Xuất tbc
---
Dự đoán kiểu dữ liệu và biến cần dùng:
X: array[1..1000] of extended;
n,i: longint;
s,tbc: extended;
b) Chương trình:
For ... do ...
Program FNG;
Uses crt;
Var X: array[1..1000] of longint;
n,i: longint;
s: extended;
Begin
Clrscr;
Write('Nhap n: '); Readln(n);
s:=0;
For i:=1 to n do
Begin
Write('X[',i,'] = ');
Readln(X[i]);
s:=s+X[i];
End;
tbc:=s/n;
Write('TBC = ',tbc:0:2);
Readln
End.
While ... do ...
Program FNG;
Uses crt;
Var X: array[1..1000] of longint;
n,i: longint;
s: extended;
Begin
Clrscr;
Write('Nhap n: '); Readln(n);
s:=0; i:=1;
While i<=n do
Begin
Write('X[',i,'] = ');
Readln(X[i]);
s:=s+X[i];
inc(i);
End;
tbc:=s/n;
Write('TBC = ',tbc:0:2);
Readln
End.