Zpracování posloupnosti hodnot - příklady

1.     součet n hodnot

2.     počet záporných z n hodnot

3.     aritmetický průměr kladných hodnot z n hodnot

4.     největší z n hodnot

5.     nejmenší a největší z n hodnot

6.     2 největší hodnoty z n hodnot

7.     největší hodnota a počet jejích výskytů z n hodnot

8.     nejmenší hodnota a pořadí jejího prvního výskytu z n hodnot

9.     Známky 1-5, koncová hodnota 0. Určete počet jedniček.

10.  Kladné teploty zakončené nulou. Určete nejmenší teplotu.

Řešení

předpokládá se dostatečný počet hodnot

příklady 1-8 by byly přehlednější pomocí cyklu for (upravte)

================================================================================


Součet n hodnot

Vstup:

N ... počet hodnot ... integer; N>0

A ... hodnoty      ... real

Výstup:

S ... součet hodnot ... real

Pom. proměnná:

P ... počítadlo hodnot ... integer

program P1;

var

  N,P : integer;

  A,S : real;

begin

  read(N);

  S:=0;

  P:=0;

  while P<N do

    begin

      read(A);

      S:=S+A;

      P:=P+1

    end;

  write(S)

end.

Počet záporných z n hodnot

Vstup:

N ... počet hodnot ... integer; N>0

A ... hodnoty      ... real

Výstup:

PZ ... počet záporných ... integer

Pom. proměnná:

P ... počítadlo hodnot ... integer

program P2;

var

  N,P,PZ : integer;

  A : real;

begin

  read(N);

  PZ:=0;

  P:=0;

  while P<N do

    begin

      read(A);

      if A<0 then PZ:=PZ+1;

      P:=P+1

    end;

  write(PZ)

end.

Aritmetický průměr kladných hodnot z n hodnot

Vstup:

N ... počet hodnot ... integer; N>0

A ... hodnoty      ... real

Výstup:

APK ... ar. průměr kladných hodnot ... real

        neexistuje pro PK=0

Pom. proměnné:

P ... počítadlo hodnot ... integer

SK ... součet kladných ... real

PK ... počet kladných ... integer

program P3;

var

  N,P,PK : integer;

  A,APK,SK : real;

begin

  read(N);

  SK:=0;

  PK:=0;

  P:=0;

  while P<N do

    begin

      read(A);

      if A>0 then begin

                    SK:=SK+A;

                    PK:=PK+1

                  end;

      P:=P+1

    end;

  if PK>0 then begin

                 APK:=SK/PK;

                 write(APK)

               end

          else write ('Zadne kladne cislo')

end.

Největší z n hodnot

Vstup:

N ... počet hodnot ... integer; N>0

A ... hodnoty      ... real

Výstup:

Max ... největší hodnota ... real

Pom. proměnná:

P  ... počítadlo hodnot  ... integer

program P4;

var

  N,P : integer;

  A,Max : real;

begin

  read(N);

  read(A);

  Max:=A;

  P:=1;

  while P<N do

    begin

      read(A);

      if A>Max then Max:=A;

      P:=P+1

    end;

  write(Max)

end.

Nejmenší a největší z n hodnot

Vstup:

N ... počet hodnot ... integer; N>0

A ... hodnoty      ... real

Výstup:

Min ... nejmenší hodnota ... real

Max ... největší hodnota ... real

Pom. proměnná:

P   ... počítadlo hodnot ... integer

program P5;

var

  N,P : integer;

  A,Min,Max : real;

begin

  read(N);

  read(A);

  Min:=A;

  Max:=A;

  P:=1;

  while P<N do

    begin

      read(A);

      if A>Max then Max:=A

               else if A<Min then Min:=A;

      P:=P+1

    end;

  write(Min,Max)

end.

2 největší hodnoty z n hodnot

Vstup:

N ... počet hodnot ... integer; N>1

A ... hodnoty      ... real

Výstup:

Max1 ... největší hodnota    ... real

Max2 ... 2. největší hodnota ... real

Pom. proměnná:

P ... počítadlo hodnot ... integer

program P6;

var

  N,P : integer;

  A,Max1,Max2 : real;

begin

  read(N);

  read(A);

  Max1:=A;

  read(A);

  if A>Max1 then begin

                   Max2:=Max1;

                   Max1:=A

                 end

            else Max2:=A;

  P:=2;

  while P<N do

    begin

      read(A);

      if A>Max1 then begin

                       Max2:=Max1;

                       Max1:=A

                     end

                else if A>Max2 then Max2:=A;

      P:=P+1

    end;

  write(Max1,Max2)

end.

Největší hodnota a počet jejích výskytů z n hodnot

Vstup:

N ... počet hodnot ... integer; N>0

A ... hodnoty      ... real

Výstup:

Max ... největší hodnota          ... real

PV  ... počet výskytů hodnoty Max ... integer

Pom. proměnná:

P ... počítadlo hodnot ... integer

program P7;

var

  N,P,PV : integer;

  A,Max : real;

begin

  read(N);

  read(A);

  Max:=A;

  PV:=1;

  P:=1;

  while P<N do

    begin

      read(A);

      if A>Max then begin

                      Max:=A;

                      PV:=1

                    end

               else if A=Max then PV:=PV+1;

      P:=P+1

    end;

  write(Max,PV)

end.

Nejmenší hodnota a pořadí jejího prvního výskytu z n hodnot

Vstup:

N ... počet hodnot ... integer; N>0

A ... hodnoty      ... real

Výstup:

Min ... nejmenší hodnota      ... real

PV1 ... pořadí 1. výskytu Min ... integer

Pom. proměnná:

P ... počítadlo hodnot ... integer

program P7;

var

  N,P,PV1 : integer;

  A,Min : real;

begin

  read(N);

  read(A);

  Min:=A;

  PV1:=1;

  P:=1;

  while P<N do

    begin

      read(A);

      P:=P+1;

      if A>Min then begin

                      Min:=A;

                      PV1:=P

                    end

    end;

  write(Min,PV1)

end.

Známky 1-5, koncová hodnota 0. Určete počet jedniček.

Vstup:

Z ... známky 1-5 ... integer; 0=konc. hodnota

Výstup:

P1 ... počet jedniček ... integer

program P9;

var

  Z,P1 : integer;

begin

  P1:=0;

  read(Z);

  while Z<>0 do

    begin

      if Z=1 then P1:=P1+1;

      read(Z)

    end;

  write(P1)

end.

Kladné teploty zakončené nulou. Určete nejmenší teplotu.

Vstup:

T ... teploty ... real, T>=0, 0=konc. hodnota

Výstup:

TMin ... nejmenší teplota ... real

program P10;

var

  T,TMin : real;

begin

  read(T);

  TMin:=T;

  read(T);

  while T<>0 do

    begin

      if T<TMin then TMin:=T;

      read(T)

    end;

  write(TMin)

end.


================================================================================