Concetti Chiave
- Il programma ordina una serie di 20 numeri interi in ordine crescente utilizzando un algoritmo di ordinamento semplice.
- Calcola la somma dei 20 numeri inseriti per determinare la media aritmetica degli elementi.
- Utilizza la media calcolata per determinare la varianza dei numeri, che misura la dispersione dei valori rispetto alla media.
- Calcola lo scarto quadratico medio, che è la radice quadrata della varianza, per fornire una misura di dispersione nella stessa unità dei dati originali.
- Il programma richiede l'input dell'utente per ciascuno dei 20 numeri e stampa i risultati delle operazioni matematiche.
program ordinare_20_numeri_in_ordine_crescente;
uses crt;
(*ordinare in ordine crescente 20numeri.calcolare media,varianza,scarto quadratico medio*)
var A: array [1..20] of integer;
J,K,X:integer;
SOMMA,MEDIA,mX,VARIANZA,SQM:real;
begin
clrscr;
writeln (' ');
for J:=1 to 20 do
begin
write('Introdurre numero intero A',J,'=');
readln(A[J]);
end;
Ordering of numbers
for J:=1 to 19 do
for K:=J+1 to 20 do
if A[J]>A[K] then
begin
X:=A[K];
A[K]:=A[J];
A[J]:=X
end;
writeln ('i numeri in ordine crescente sono:');
for J:=1 to 20 do
write(A[J],' ');
writeln (' ');
SOMMA:=0;
for J:=1 to 20 do
SOMMA:=SOMMA+A[J];
MEDIA:=SOMMA/20;
WRITELN (' ');
writeln('La media degli elementi Š m=',MEDIA:5:2);
Calculation of variance and standard deviation
mX:=0;
for J:=1 to 20 do
begin
mX:=mX+sqr(A[J])
end;
VARIANZA:=(mX/20)-sqr(MEDIA);
SQM:=sqrt(VARIANZA);
writeln ('La varianza Š var=',VARIANZA:5:2);
writeln ('Lo scarto quadratico medio Š sqm=',SQM:5:2);
readln
end.
Domande da interrogazione
- How does the program sort the 20 numbers in ascending order?
- How is the average of the numbers calculated in the program?
- What method does the program use to calculate the variance and standard deviation?
The program uses a nested loop to compare each pair of numbers in the array. If a number is greater than the subsequent number, they are swapped. This process continues until all numbers are sorted in ascending order.
The program calculates the average by summing all the numbers in the array and then dividing the total by 20, which is the number of elements in the array.
The program calculates variance by first finding the mean of the squares of the numbers, then subtracting the square of the mean of the numbers. The standard deviation is then calculated as the square root of the variance.