Joined: 27 Nov 2004 Posts: 1232 Location: www.parsx.com
Posted: Sun Jun 11, 2006 12:08 pm Post subject: برنامه محاسبه واریانس ،انحراف از معیار ، میانگین و مد .
برنامه ای بنویسید که واریانس ،انحراف از معیار ، میانگین و مد یک آرایه را محاسبه کند ؟
program f1_for;
CONST
MAXARY=10;
VAR
ARYX:ARRAY [1..MAXARY] OF INTEGER;
I,J,TEMP ,MOD1,MOD2,CountMod1,CountMod2,CountMod3:INTEGER;
SUM:LONGINT;
S,AVG:REAL;
begin
WRITELN('>> Enter ',MAXARY,' number <<');
WRITELN;
FOR I:=1 TO MAXARY DO
READLN(ARYX[I]);
(*------------- SORT -----------*)
FOR I:=1 TO MAXARY-1 DO
FOR J:=I+1 TO MAXARY DO
IF ARYX[I]>ARYX[J] THEN BEGIN
TEMP:=ARYX[I];
ARYX[I]:=ARYX[J];
ARYX[J]:=TEMP;
END;
(*------------- SHOW -----------*)
WRITELN;
WRITELN;WRITE(' The sort array is :');
FOR I:=1 TO MAXARY DO WRITE(' ' ,ARYX[I]);
(*------------- MIANGIN -----------*)
SUM:=0; WRITELN; WRITELN;
FOR I:=1 TO MAXARY DO
SUM:= SUM+ ARYX[I];
AVG:=SUM/MAXARY;
WRITELN(' The average of array is : ', AVG:0:2);
WRITELN;
(*------------- MOD -----------*)
TEMP:=ARYX[1];
FOR I:=1 TO MAXARY DO Begin
J:=0;
WHILE (ARYX[I]=TEMP) AND (I<>MAXARY) DO BEGIN
I:=I+1;
J:=J+1;
END; { WHILE }
IF J>COUNTMOD1 THEN BEGIN
COUNTMOD1:=J;
MOD1:=TEMP;
END;
TEMP:=ARYX[I];
END; { FOR }
TEMP:=ARYX[1];
FOR I:=1 TO MAXARY DO Begin
J:=0;
WHILE (ARYX[I]=TEMP) AND (I<>MAXARY) AND (ARYX[I]<>MOD1) DO BEGIN
I:=I+1;
J:=J+1;
END; { WHILE }
IF J>COUNTMOD2 THEN BEGIN
COUNTMOD2:=J;
MOD2:=TEMP;
END;
TEMP:=ARYX[I];
END; { FOR }
TEMP:=ARYX[1];
FOR I:=1 TO MAXARY DO Begin
J:=0;
WHILE (ARYX[I]=TEMP) AND (I<>MAXARY)
AND (ARYX[I]<>MOD1) AND (ARYX[I]<>MOD2)DO BEGIN
I:=I+1;
J:=J+1;
IF J>COUNTMOD3 THEN COUNTMOD3:=J;
END; { WHILE }
TEMP:=ARYX[I];
END; { FOR }
IF (COUNTMOD1<>COUNTMOD2)AND(COUNTMOD2<>COUNTMOD3)THEN
WRITELN(' MOD = ',MOD1,', The number of apear : ',COUNTMOD1+1)
ELSE IF (COUNTMOD3<>COUNTMOD2) THEN
WRITELN(' MOD = ',(MOD1+MOD2) / 2:0:4)
ELSE WRITELN(' This array haven''t any MOD');
(*------------- VARIANS -----------*)
WRITELN;
FOR I:=1 TO MAXARY DO
S:= SQR(ARYX[I]-AVG) + S;
S:=S/MAXARY;
WRITELN(' VARIANSE = ',S:0:4);
WRITELN;
WRITELN(' ENHERAFE MIARE = ',SQRT(S):0:4);
READLN;
end.
Joined: 27 Nov 2004 Posts: 1232 Location: www.parsx.com
Posted: Sun Jun 11, 2006 12:13 pm Post subject:
برنامه بالا به طریقی پیاده سازی کنید که فقط از حلقه while استفاده شود ؟
program f1_while;
CONST
MAXARY=10;
VAR
ARYX:ARRAY [1..MAXARY] OF INTEGER;
I,J,TEMP ,MOD1,MOD2,CountMod1,CountMod2,CountMod3:INTEGER;
SUM:LONGINT;
S,AVG:REAL;
begin
WRITELN('>> Enter ',MAXARY,' number <<');
WRITELN;
I:=1;
WHILE I<= MAXARY DO BEGIN
READLN(ARYX[I]);
I:=I+1;
END;
(*------------- SORT -----------*)
I:=1;
WHILE I<=MAXARY-1 DO BEGIN
J:=I+1;
WHILE J<=MAXARY DO BEGIN
IF ARYX[I]>ARYX[J] THEN BEGIN
TEMP:=ARYX[I];
ARYX[I]:=ARYX[J];
ARYX[J]:=TEMP;
END;
J:=J+1;
END;{ WHILE J}
I:=I+1;
END;{ WHILE }
(*------------- SHOW -----------*)
WRITELN;
WRITELN;WRITE(' The sort array is :');
I:=1;
WHILE I<=MAXARY DO BEGIN
WRITE(' ' ,ARYX[I]);
I:=I+1;
END;
(*------------- MIANGIN -----------*)
SUM:=0; WRITELN; WRITELN;
I:=1;
WHILE I<=MAXARY DO BEGIN
SUM:= SUM+ ARYX[I];
I:=I+1;
END;
AVG:=SUM/MAXARY;
WRITELN(' The average of array is : ', AVG:0:2);
WRITELN;
(*------------- MOD -----------*)
TEMP:=ARYX[1];
I:=1;
WHILE I<=MAXARY DO Begin
J:=0;
WHILE (ARYX[I]=TEMP) AND (I<>MAXARY) DO BEGIN
I:=I+1;
J:=J+1;
END; { WHILE }
IF J>COUNTMOD1 THEN BEGIN
COUNTMOD1:=J;
MOD1:=TEMP;
END;
TEMP:=ARYX[I];
I:=I+1;
END; { FOR }
TEMP:=ARYX[1];
I:=1;
WHILE I<=MAXARY DO Begin
J:=0;
WHILE (ARYX[I]=TEMP) AND (I<>MAXARY) AND (ARYX[I]<>MOD1) DO BEGIN
I:=I+1;
J:=J+1;
END; { WHILE }
IF J>COUNTMOD2 THEN BEGIN
COUNTMOD2:=J;
MOD2:=TEMP;
END;
TEMP:=ARYX[I];
I:=I+1;
END; { FOR }
TEMP:=ARYX[1];
I:=1;
WHILE I<=MAXARY DO Begin
J:=0;
WHILE (ARYX[I]=TEMP) AND (I<>MAXARY)
AND (ARYX[I]<>MOD1) AND (ARYX[I]<>MOD2)DO BEGIN
I:=I+1;
J:=J+1;
IF J>COUNTMOD3 THEN COUNTMOD3:=J;
END; { WHILE }
TEMP:=ARYX[I];
I:=I+1;
END; { FOR }
IF (COUNTMOD1<>COUNTMOD2)AND(COUNTMOD2<>COUNTMOD3)THEN
WRITELN(' MOD = ',MOD1,', The number of apear : ',COUNTMOD1+1)
ELSE IF (COUNTMOD3<>COUNTMOD2) THEN
WRITELN(' MOD = ',(MOD1+MOD2) /2 :0:4)
ELSE WRITELN(' This array haven''t any MOD');
(*------------- VARIANS -----------*)
WRITELN;
I:=1;
WHILE I<=MAXARY DO BEGIN
S:= SQR(ARYX[I]-AVG) + S;
I:=I+1;
END;
S:=S/MAXARY;
WRITELN(' VARIANSE = ',S:0:4);
WRITELN;
WRITELN(' ENHERAFE MIARE = ',SQRT(S):0:4);
READLN;
end.
لطفا اگه کسی از این برنامه ها استفاده کرد یه تشکر از نویسندشون بکنه ! همین !
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum