Sortowanie bąbelkowe PASCAL

evil17

Użytkownik
Dołączył
Październik 17, 2010
Posty
18
Witam, mam problem z sortowaniem bąbelkowym, mianowicie muszę posortować liczny z tablicy, początkowo program prosi nas o przypisanie wartości do tablicy.

PHP:
program hehetablica;
uses crt;
var
i,s,r :integer;
ile : integer;
p: char;
t: array [1..15] of integer;
begin

clrscr;
r:=1;
repeat
write('Ilu uczni˘w? ');
readln(ile);
for i:=1 to ile do begin
writeln('Podaj liczb© godzin dla ucznia ',i);
writeln;
readln(t[i]);
writeln;
end;
s:=0;
for i:=1 to ile do begin
s:=s+t[i];
writeln(t[i]);
end;
writeln;
Write('—rednia: ');
writeln(s/ile:2:0);
writeln('Powt˘rzy†? t/n ');
readln(p);
case p of
't': r:=1;
'n': r:=0;
else
writeln('Podaˆe˜ zˆĄ liter©');
r:=0;
readln;
end;
until(r=0);
readln
end.
 

D.F.

Były Moderator
Dołączył
Listopad 4, 2009
Posty
493
Na stronie http://pl.wikisource.org/wiki/Sortowanie_bąbelkowe/kod masz kod, na którym możesz się wzorować:

Kod:
Const
numbers = 6; // ilość sortowanych komórek w tablicy
 
Type
table = Array[1..numbers] Of Integer;
 
Var
counter : Integer;
liczby : table;
 
Procedure bubble_sort();
 
  Var
  check, memory, run : Integer; // ilość sprawdzeń w przebiegu, komórka pamięci, ilość przebiegów
 
  Begin
    For run := 1 To numbers - 1 Do
      Begin
      check := numbers - run;
        For counter := 1 To check Do
          If liczby[counter] > liczby[counter + 1]
          Then
            Begin
            memory := liczby[counter];
            liczby[counter] := liczby [counter + 1];
            liczby[counter + 1] := memory;
            End;
      End;
  End;
 

evil17

Użytkownik
Dołączył
Październik 17, 2010
Posty
18
Zrobiłem coś takiego, ale mam błąd exitcode :201

program hehetablica;
uses crt;
var
i,s,r :integer;
ile,tmp,j : integer;
p: char;
t: array [1..15] of integer;
begin
clrscr;
r:=1;
repeat
write('Ilu uczniów? ');
readln(ile);
for i:=1 to ile do begin
writeln('Podaj liczbę godzin dla ucznia ',i);
writeln;
readln(t);
writeln;
end;
s:=0;
for i:=1 to ile do begin
s:=s+t;
for j:=0 to ile do
if (t[j]>t[j+1]) then
begin
tmp := t[j];
t[j] := t[j+1];
t[j+1] := tmp;
end;
end;
writeln;
Write('Średnia: ');
writeln(s/ile:2:0);
writeln('Powtórzyć? t/n ');
readln(p);
case p of
't': r:=1;
'n': r:=0;
else
writeln('Podałeś złą literę');
r:=0;
readln;
end;
until(r=0);
readln
end.
 

D.F.

Były Moderator
Dołączył
Listopad 4, 2009
Posty
493
Nie wiem czy o to chodziło, ale napisałem Ci coś takiego:
PHP:
program Tablica;

uses CRT;

var
  i,suma,r :integer;
  ile,tmp,j, check : integer;
  p: char;
  t: array [1..15] of integer;
begin
  //clrscr;
  r := 1;
  repeat
    //pobieranie danych
    Write('Ilu uczniow? ');
    ReadLn(ile);
    for i := 1 to ile do
    begin
      WriteLn('Podaj liczbe godzin dla ucznia ',i);
      WriteLn;
      ReadLn(t[i]);
      WriteLn;
    end;

    //sumowanie i srednia
    suma := 0;
    for i := 1 to ile do
    begin
      suma := suma + t[i];
    end;
    Write('Srednia: ');
    WriteLn(suma/ile:2:0);

    //sortowanie od najmniejszych
    for i := 1 to ile - 1 do
    begin
    check := ile - i;
    for j := 1 to check do
      if (t[j]>t[j+1]) then
      begin
        tmp := t[j];
        t[j] := t[j+1];
        t[j+1] := tmp;
      end;
    end;

    //wypisywanie posortowanej tablicy
    for j := 1 to ile do
      WriteLn('t[',j,'] = ', t[j]);
    WriteLn;
    WriteLn('Powtorzyc? t/n ');
    ReadLn(p);
    case p of
      't': r := 1;
      'n': r := 0;
    else
      WriteLn('Podales zla litere');
      r := 0;
      ReadLn;
    end;
  until(r = 0);
  ReadLn;
end.
 
Do góry Bottom